From 8aaedd20e32e0619f038743e8b631574eb2f06c1 Mon Sep 17 00:00:00 2001 From: Jens Kraemer Date: Tue, 30 Aug 2016 18:03:23 +0800 Subject: [PATCH] allow upload of zero byte files as attachments --- app/models/attachment.rb | 18 ++++++++---------- test/integration/attachments_test.rb | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 4bc674f..2f34b4c 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -82,16 +82,14 @@ class Attachment < ActiveRecord::Base def file=(incoming_file) unless incoming_file.nil? @temp_file = incoming_file - if @temp_file.size > 0 - if @temp_file.respond_to?(:original_filename) - self.filename = @temp_file.original_filename - self.filename.force_encoding("UTF-8") - end - if @temp_file.respond_to?(:content_type) - self.content_type = @temp_file.content_type.to_s.chomp - end - self.filesize = @temp_file.size + if @temp_file.respond_to?(:original_filename) + self.filename = @temp_file.original_filename + self.filename.force_encoding("UTF-8") end + if @temp_file.respond_to?(:content_type) + self.content_type = @temp_file.content_type.to_s.chomp + end + self.filesize = @temp_file.size end end @@ -107,7 +105,7 @@ class Attachment < ActiveRecord::Base # Copies the temporary file to its final location # and computes its MD5 hash def files_to_final_location - if @temp_file && (@temp_file.size > 0) + if @temp_file self.disk_directory = target_directory self.disk_filename = Attachment.disk_filename(filename, disk_directory) logger.info("Saving attachment '#{self.diskfile}' (#{@temp_file.size} bytes)") if logger diff --git a/test/integration/attachments_test.rb b/test/integration/attachments_test.rb index 114e47a..37a146b 100644 --- a/test/integration/attachments_test.rb +++ b/test/integration/attachments_test.rb @@ -136,6 +136,26 @@ class AttachmentsTest < Redmine::IntegrationTest assert_include "$('#attachments_1').remove();", response.body end + def test_upload_zero_byte_file + log_user('jsmith', 'jsmith') + + ajax_upload('empty.txt', '') + + attachment = Attachment.order('id DESC').first + attachment_path = "/attachments/#{attachment.id}.js?attachment_id=1" + assert_include "href: '#{attachment_path}'", response.body, "Path to attachment: #{attachment_path} not found in response:\n#{response.body}" + + assert File.readable? attachment.diskfile + assert_equal 0, File.size(attachment.diskfile) + + assert_difference 'Attachment.count', -1 do + delete attachment_path + assert_response :success + end + + assert_include "$('#attachments_1').remove();", response.body + end + private def ajax_upload(filename, content, attachment_id=1) -- 2.1.4