diff --git a/Gemfile b/Gemfile index 0ec948420..b0e848dae 100644 --- a/Gemfile +++ b/Gemfile @@ -17,6 +17,7 @@ gem "nokogiri", "~> 1.10.0" gem 'i18n', '~> 1.8.2' gem "rbpdf", "~> 1.20.0" gem 'addressable' +gem 'rubyzip', (RUBY_VERSION < '2.4' ? '~> 1.3.0' : '~> 2.2.0') # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :x64_mingw, :mswin] diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index db90b55da..25a2c6689 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -19,6 +19,8 @@ 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_downloadable_attachments, :only => :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 @@ -132,6 +134,20 @@ class AttachmentsController < ApplicationController render :action => 'edit_all' end + def download_all + Tempfile.create('attachments_zip', Rails.root.join('tmp')) do |tempfile| + zip_file = Attachment.archive_attachments(tempfile, @attachments) + if zip_file + send_data( + File.read(zip_file.path), + :type => 'application/zip', + :filename => "#{@container.class.to_s.downcase}-#{@container.id}-attachments.zip") + else + render_404 + end + end + end + def update @attachment.safe_attributes = params[:attachment] saved = @attachment.save @@ -195,6 +211,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 @@ -206,15 +227,24 @@ 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 + def find_downloadable_attachments + @attachments = @container.attachments.select{|a| File.readable?(a.diskfile) } + bulk_download_max_size = Setting.bulk_download_max_size.to_i.kilobytes + if @attachments.sum(&:filesize) > bulk_download_max_size + flash[:error] = l(:error_bulk_download_size_too_big, + :max_size => bulk_download_max_size.to_i.kilobytes) + redirect_to back_url + return + end + end + # Checks that the file exists and is readable def file_readable if @attachment.readable? diff --git a/app/helpers/attachments_helper.rb b/app/helpers/attachments_helper.rb index 47e5ea78a..e496958bf 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 6b8c75cc4..17539f604 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -19,6 +19,7 @@ require "digest" require "fileutils" +require "zip" class Attachment < ActiveRecord::Base include Redmine::SafeAttributes @@ -345,6 +346,30 @@ class Attachment < ActiveRecord::Base Attachment.where("created_on < ? AND (container_type IS NULL OR container_type = '')", Time.now - age).destroy_all end + def self.archive_attachments(out_file, attachments) + attachments = attachments.select{|attachment| File.readable?(attachment.diskfile) } + return nil if attachments.blank? + + Zip.unicode_names = true + archived_file_names = [] + Zip::File.open(out_file.path, Zip::File::CREATE) do |zip| + attachments.each do |attachment| + filename = attachment.filename + # Change the file name if a file with the same name already exists + dup_count = 0 + while archived_file_names.include?(filename) + dup_count += 1 + basename = File.basename(attachment.filename, '.*') + extname = File.extname(attachment.filename) + filename = "#{basename}(#{dup_count})#{extname}" + end + zip.add(filename, attachment.diskfile) + archived_file_names << filename + end + end + out_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 0a9f5e3eb..25d022029 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_all_attachments), + container_attachments_download_path(container), + :title => l(:label_download_all_attachments), + :class => 'icon-only icon-download' + ) if attachments.size > 1 %>