Project

General

Profile

Patch #23718 » 0001-allow-upload-of-zero-byte-files-as-attachments.patch

Jens Krämer, 2016-08-30 12:05

View differences:

app/models/attachment.rb
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
......
# 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
test/integration/attachments_test.rb
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)
    (1-1/1)