diff --git a/Gemfile b/Gemfile index bfa5a17e43..2a40776e07 100644 --- a/Gemfile +++ b/Gemfile @@ -14,6 +14,7 @@ gem "roadie", "~> 3.2.1" gem "mimemagic" gem "mail", "~> 2.6.4" gem "csv", "~> 1.0.2" if RUBY_VERSION >= "2.3" +gem "rubyzip", "~> 1.2.1" gem "nokogiri", "~> 1.8.0" gem "i18n", "~> 0.7.0" diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 9c0bf15baa..a7d9a93a53 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -17,6 +17,7 @@ class AttachmentsController < ApplicationController before_action :find_attachment, :only => [:show, :download, :thumbnail, :update, :destroy] + before_action :find_container, :only => [:edit_all, :update_all, :download_all] before_action :find_editable_attachments, :only => [:edit_all, :update_all] before_action :file_readable, :read_authorize, :only => [:show, :download, :thumbnail] before_action :update_authorize, :only => :update @@ -129,6 +130,20 @@ class AttachmentsController < ApplicationController render :action => 'edit_all' end + def download_all + begin + temp_file = Attachment.attachments_to_zip(@container.attachments) + render_404 and return if temp_file.nil? + send_data(File.read(temp_file.path), :type => 'application/zip', :filename => "#{@container.class.to_s.downcase}-#{@container.id}-attachments.zip") + ensure + # Close and delete the temp file + unless temp_file.nil? + temp_file.close + temp_file.unlink + end + end + end + def update @attachment.safe_attributes = params[:attachment] saved = @attachment.save @@ -190,6 +205,11 @@ class AttachmentsController < ApplicationController end def find_editable_attachments + @attachments = @container.attachments.select(&:editable?) + render_404 if @attachments.empty? + end + + def find_container klass = params[:object_type].to_s.singularize.classify.constantize rescue nil unless klass && klass.reflect_on_association(:attachments) render_404 @@ -201,11 +221,9 @@ class AttachmentsController < ApplicationController render_403 return end - @attachments = @container.attachments.select(&:editable?) if @container.respond_to?(:project) @project = @container.project end - render_404 if @attachments.empty? rescue ActiveRecord::RecordNotFound render_404 end diff --git a/app/helpers/attachments_helper.rb b/app/helpers/attachments_helper.rb index 385f3b8a4f..0c2b5869bb 100644 --- a/app/helpers/attachments_helper.rb +++ b/app/helpers/attachments_helper.rb @@ -27,6 +27,10 @@ module AttachmentsHelper object_attachments_path container.class.name.underscore.pluralize, container.id end + def container_attachments_download_path(container) + object_attachments_download_path container.class.name.underscore.pluralize, container.id + end + # Displays view/delete links to the attachments of the given object # Options: # :author -- author names are not displayed if set to false diff --git a/app/models/attachment.rb b/app/models/attachment.rb index cc9b5f930f..b790755a37 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -17,6 +17,7 @@ require "digest" require "fileutils" +require "zip" class Attachment < ActiveRecord::Base include Redmine::SafeAttributes @@ -333,6 +334,26 @@ class Attachment < ActiveRecord::Base Attachment.where("created_on < ? AND (container_type IS NULL OR container_type = '')", Time.now - age).destroy_all end + def self.attachments_to_zip(attachments) + attachments = attachments.select{|attachment| File.readable?(attachment.diskfile) } + return nil if attachments.blank? + temp_file = Tempfile.new('attachments_zip', Rails.root.join(Attachment.storage_path)) + Zip.unicode_names = true + existing_file_names = [] + Zip::File.open(temp_file.path, Zip::File::CREATE) do |zip| + attachments.each do |attachment| + filename = attachment.filename + existing_file_names << filename + # If a file with the same name already exists, change the file name. + if existing_file_names.count(filename) > 1 + filename = "#{File.basename(filename, ".*")}(#{existing_file_names.count(filename)})#{File.extname(filename)}" + end + zip.add(filename, attachment.diskfile) + end + end + temp_file + end + # Moves an existing attachment to its target directory def move_to_target_directory! return unless !new_record? & readable? diff --git a/app/views/attachments/_links.html.erb b/app/views/attachments/_links.html.erb index 0a9f5e3ebf..88593f990a 100644 --- a/app/views/attachments/_links.html.erb +++ b/app/views/attachments/_links.html.erb @@ -5,6 +5,11 @@ :title => l(:label_edit_attachments), :class => 'icon-only icon-edit' ) if options[:editable] %> + <%= link_to(l(:label_download_attachments), + container_attachments_download_path(container), + :title => l(:label_download_attachments), + :class => 'icon-only icon-download' + ) %>