]> source.dussan.org Git - redmine.git/commitdiff
Fixes uploading of empty files (#25115).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 28 Feb 2017 18:05:17 +0000 (18:05 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 28 Feb 2017 18:05:17 +0000 (18:05 +0000)
- prevents creation of attachment records without existing diskfile and empty
  digest
- adds test case to check file upload API response
- also removes the file size check in ActsAsAttachable which still prevented
  attachment of zero size attachments to containers but only for clients
  without Javascript (where save_attachments is called with the actual file
  upload).

Patch by Jens Kraemer.

git-svn-id: http://svn.redmine.org/redmine/trunk@16341 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/attachment.rb
lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb
test/integration/api_test/attachments_test.rb

index d0384372a6a3f8d52d75cdc6bd6523079f0ac6e6..52c78252121a5905d85bd113b63444a5b6d9d47b 100644 (file)
@@ -85,7 +85,6 @@ 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")
@@ -94,7 +93,6 @@ class Attachment < ActiveRecord::Base
           self.content_type = @temp_file.content_type.to_s.chomp
         end
         self.filesize = @temp_file.size
-      end
     end
   end
 
@@ -110,7 +108,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
index 2e5fc841cd0c0800ed3fb20b92e696d30ac780e9..45fa72b2d56122d3ed127cf9821acc3a4fd036e3 100644 (file)
@@ -89,7 +89,6 @@ module Redmine
               next unless attachment.is_a?(Hash)
               a = nil
               if file = attachment['file']
-                next unless file.size > 0
                 a = Attachment.create(:file => file, :author => author)
               elsif token = attachment['token'].presence
                 a = Attachment.find_by_token(token)
index 4188d71161a62e984834e8ac739c2b1f66c7d481..641dccabf5a178b183aede855fc87f99051b53e0 100644 (file)
@@ -197,4 +197,23 @@ class Redmine::ApiTest::AttachmentsTest < Redmine::ApiTest::Base
       end
     end
   end
+
+  test "POST /uploads.json should create an empty file and return a valid token" do
+    set_tmp_attachments_directory
+    assert_difference 'Attachment.count' do
+      post '/uploads.json', '', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
+      assert_response :created
+
+    end
+
+    json = ActiveSupport::JSON.decode(response.body)
+    assert_kind_of Hash, json['upload']
+    token = json['upload']['token']
+    assert token.present?
+
+    assert attachment = Attachment.find_by_token(token)
+    assert_equal 0, attachment.filesize
+    assert attachment.digest.present?
+    assert File.exist? attachment.diskfile
+  end
 end