Project

General

Profile

Patch #25115 » 0001-fixes-uploading-of-empty-files-v2.patch

extended patch with acts_as_attachable fix - Jens Krämer, 2017-02-19 08:02

View differences:

app/models/attachment.rb
85 85
  def file=(incoming_file)
86 86
    unless incoming_file.nil?
87 87
      @temp_file = incoming_file
88
      if @temp_file.size > 0
89 88
        if @temp_file.respond_to?(:original_filename)
90 89
          self.filename = @temp_file.original_filename
91 90
          self.filename.force_encoding("UTF-8")
......
94 93
          self.content_type = @temp_file.content_type.to_s.chomp
95 94
        end
96 95
        self.filesize = @temp_file.size
97
      end
98 96
    end
99 97
  end
100 98

  
......
110 108
  # Copies the temporary file to its final location
111 109
  # and computes its MD5 hash
112 110
  def files_to_final_location
113
    if @temp_file && (@temp_file.size > 0)
111
    if @temp_file
114 112
      self.disk_directory = target_directory
115 113
      self.disk_filename = Attachment.disk_filename(filename, disk_directory)
116 114
      logger.info("Saving attachment '#{self.diskfile}' (#{@temp_file.size} bytes)") if logger
lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb
89 89
              next unless attachment.is_a?(Hash)
90 90
              a = nil
91 91
              if file = attachment['file']
92
                next unless file.size > 0
93 92
                a = Attachment.create(:file => file, :author => author)
94 93
              elsif token = attachment['token'].presence
95 94
                a = Attachment.find_by_token(token)
test/integration/api_test/attachments_test.rb
197 197
      end
198 198
    end
199 199
  end
200

  
201
  test "POST /uploads.json should create an empty file and return a valid token" do
202
    set_tmp_attachments_directory
203
    assert_difference 'Attachment.count' do
204
      post '/uploads.json', '', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
205
      assert_response :created
206

  
207
    end
208

  
209
    json = ActiveSupport::JSON.decode(response.body)
210
    assert_kind_of Hash, json['upload']
211
    token = json['upload']['token']
212
    assert token.present?
213

  
214
    assert attachment = Attachment.find_by_token(token)
215
    assert_equal 0, attachment.filesize
216
    assert attachment.digest.present?
217
    assert File.exist? attachment.diskfile
218
  end
200 219
end
(2-2/2)