Feature #43484 » 0005-Detect-attachment-content-type-from-file-contents-in.patch
| app/assets/javascripts/attachments.js | ||
|---|---|---|
|
uploadUrl = uploadUrl + '?attachment_id=' + attachmentId;
|
||
|
if (blob instanceof window.Blob) {
|
||
|
uploadUrl += '&filename=' + encodeURIComponent(blob.name);
|
||
|
uploadUrl += '&content_type=' + encodeURIComponent(blob.type);
|
||
|
}
|
||
|
return $.ajax(uploadUrl, {
|
||
| app/controllers/attachments_controller.rb | ||
|---|---|---|
|
@attachment = Attachment.new(:file => raw_request_body)
|
||
|
@attachment.author = User.current
|
||
|
@attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
|
||
|
@attachment.content_type = params[:content_type].presence
|
||
|
# The content type is detected from the file contents on save,
|
||
|
# so params[:content_type] is ignored
|
||
|
saved = @attachment.save
|
||
|
respond_to do |format|
|
||
| app/models/attachment.rb | ||
|---|---|---|
|
File.join(storage_path, 'derived_cache', 'markdownized_previews')
|
||
|
end
|
||
|
before_create :files_to_final_location
|
||
|
before_create :set_content_type, :files_to_final_location
|
||
|
before_update :reset_content_type, :if => :filename_changed?
|
||
|
after_commit :delete_from_disk, :on => :destroy
|
||
|
after_commit :reuse_existing_file_if_possible, :on => :create
|
||
|
after_rollback :delete_from_disk, :on => :create
|
||
|
safe_attributes 'filename', 'content_type', 'description'
|
||
|
safe_attributes 'filename', 'description'
|
||
|
# Returns an unsaved copy of the attachment
|
||
|
def copy(attributes=nil)
|
||
| ... | ... | |
|
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
|
||
| ... | ... | |
|
filename
|
||
|
end
|
||
|
def set_content_type
|
||
|
return unless @temp_file
|
||
|
io = @temp_file.respond_to?(:read) ? @temp_file : StringIO.new(@temp_file)
|
||
|
if io.respond_to?(:rewind)
|
||
|
io.rewind
|
||
|
else
|
||
|
# Marcel rewinds the io while reading it, which a plain reader may not
|
||
|
# support; detect the type from the filename alone in that case
|
||
|
io = nil
|
||
|
end
|
||
|
# The filename is passed as a hint because many formats, plain text and
|
||
|
# Markdown among them, cannot be identified from their contents alone.
|
||
|
# Without it they would all be detected as application/octet-stream.
|
||
|
self.content_type = Marcel::MimeType.for(io, name: filename)
|
||
|
@temp_file.rewind if @temp_file.respond_to?(:rewind)
|
||
|
end
|
||
|
# Re-detects the content type from the file on disk. Needed after a rename
|
||
|
# because the filename is used as a hint for contents that cannot be
|
||
|
# identified on their own.
|
||
|
def reset_content_type
|
||
|
return unless readable?
|
||
|
File.open(diskfile, 'rb') do |io|
|
||
|
self.content_type = Marcel::MimeType.for(io, name: filename)
|
||
|
end
|
||
|
end
|
||
|
# Copies the temporary file to its final location
|
||
|
# and computes its hash
|
||
|
def files_to_final_location
|
||
| ... | ... | |
|
self.digest = sha.hexdigest
|
||
|
end
|
||
|
@temp_file = nil
|
||
|
if content_type.blank? && filename.present?
|
||
|
self.content_type = Redmine::MimeType.of(filename)
|
||
|
end
|
||
|
# Don't save the content type if it's longer than the authorized length
|
||
|
if self.content_type && self.content_type.length > 255
|
||
|
self.content_type = nil
|
||
|
end
|
||
|
end
|
||
|
# Deletes the file from the file system if it's not referenced by other attachments
|
||
| app/models/mail_handler.rb | ||
|---|---|---|
|
obj.attachments << Attachment.create(:container => obj,
|
||
|
:file => attachment.body.decoded,
|
||
|
:filename => attachment.filename,
|
||
|
:author => user,
|
||
|
:content_type => attachment.mime_type)
|
||
|
:author => user)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
| lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb | ||
|---|---|---|
|
next
|
||
|
end
|
||
|
a.filename = attachment['filename'] unless attachment['filename'].blank?
|
||
|
a.content_type = attachment['content_type'] unless attachment['content_type'].blank?
|
||
|
end
|
||
|
next unless a
|
||
|
a.description = attachment['description'].to_s.strip
|
||
| test/integration/api_test/attachments_test.rb | ||
|---|---|---|
|
assert_nil attachment.container
|
||
|
assert_equal 2, attachment.author_id
|
||
|
assert_equal 'File content'.size, attachment.filesize
|
||
|
assert attachment.content_type.blank?
|
||
|
assert_equal 'application/octet-stream', attachment.content_type
|
||
|
assert attachment.filename.present?
|
||
|
assert_match %r{\d+_[0-9a-z]+}, attachment.diskfile
|
||
|
assert File.exist?(attachment.diskfile)
|
||
| test/integration/api_test/files_test.rb | ||
|---|---|---|
|
assert_response :bad_request
|
||
|
end
|
||
|
test "POST /projects/:project_id/files.json should accept :filename, :description, :content_type as optional parameters" do
|
||
|
test "POST /projects/:project_id/files.json should accept :filename, :description as optional parameters" do
|
||
|
set_tmp_attachments_directory
|
||
|
post(
|
||
|
'/uploads.xml',
|
||
| ... | ... | |
|
{ "file": {
|
||
|
"filename": "New filename",
|
||
|
"description": "New description",
|
||
|
"content_type": "application/txt",
|
||
|
"token": "#{token}"
|
||
|
}
|
||
|
}
|
||
| ... | ... | |
|
assert_response :success
|
||
|
assert_equal "New filename", Attachment.last.filename
|
||
|
assert_equal "New description", Attachment.last.description
|
||
|
assert_equal "application/txt", Attachment.last.content_type
|
||
|
end
|
||
|
test "POST /projects/:project_id/files.json should accept :version_id to attach the files to a version" do
|
||
| test/integration/attachments_test.rb | ||
|---|---|---|
|
assert_equal 'text/plain', attachment.content_type
|
||
|
end
|
||
|
def test_upload_should_accept_content_type_param
|
||
|
def test_upload_should_detect_file_content_type
|
||
|
log_user('jsmith', 'jsmith')
|
||
|
assert_difference 'Attachment.count' do
|
||
|
post(
|
||
|
"/uploads.js?attachment_id=1&filename=foo&content_type=image/jpeg",
|
||
|
:params => "File content",
|
||
|
"/uploads.js?attachment_id=1&filename=foo",
|
||
|
:params => "\xCA\xFE\xBA\xBE", # Java class file magic bytes
|
||
|
:headers => {"CONTENT_TYPE" => 'application/octet-stream'})
|
||
|
assert_response :success
|
||
|
end
|
||
|
attachment = Attachment.order(:id => :desc).first
|
||
|
assert_equal 'image/jpeg', attachment.content_type
|
||
|
assert_equal 'application/java-vm', attachment.content_type
|
||
|
end
|
||
|
def test_upload_as_js_and_attach_to_an_issue
|
||
| test/unit/attachment_test.rb | ||
|---|---|---|
|
assert_equal 59, File.size(a.diskfile)
|
||
|
end
|
||
|
def test_create_should_clear_content_type_if_too_long
|
||
|
def test_create_should_not_trust_client_declared_content_type
|
||
|
# PDF file uploaded with a wrong content type "text/plain"
|
||
|
# and a long bogus content_type attribute
|
||
|
a = Attachment.new(:container => Issue.find(1),
|
||
|
:file => uploaded_test_file("testfile.txt", "text/plain"),
|
||
|
:file => uploaded_test_file("hello.pdf", "text/plain"),
|
||
|
:author => User.find(1),
|
||
|
:content_type => 'a'*300)
|
||
|
assert a.save
|
||
|
a.reload
|
||
|
assert_nil a.content_type
|
||
|
assert_equal 'application/pdf', a.content_type
|
||
|
end
|
||
|
def test_create_should_accept_a_file_that_cannot_be_rewound
|
||
|
# A minimal reader like the ones plugins may pass to Attachment#file=
|
||
|
reader = Class.new do
|
||
|
def initialize(content)
|
||
|
@content = content
|
||
|
end
|
||
|
def size
|
||
|
@content.bytesize
|
||
|
end
|
||
|
def read(*args)
|
||
|
if @eof
|
||
|
false
|
||
|
else
|
||
|
@eof = true
|
||
|
@content
|
||
|
end
|
||
|
end
|
||
|
end.new("hello world\n")
|
||
|
a = Attachment.new(:file => reader, :filename => 'note.txt', :author_id => 1)
|
||
|
assert a.save
|
||
|
# The contents cannot be inspected without consuming the reader,
|
||
|
# so the content type is detected from the filename
|
||
|
assert_equal 'text/plain', a.content_type
|
||
|
assert_equal "hello world\n", File.read(a.diskfile)
|
||
|
end
|
||
|
def test_rename_should_recalculate_the_content_type
|
||
|
a = Attachment.create!(
|
||
|
:file => mock_file_with_options(:original_filename => 'test.bin', :content => "\x00\x01\x02".b),
|
||
|
:author_id => 1
|
||
|
)
|
||
|
assert_equal 'application/octet-stream', a.content_type
|
||
|
assert a.update(:filename => 'renamed.txt')
|
||
|
assert_equal 'text/plain', a.reload.content_type
|
||
|
end
|
||
|
def test_rename_should_not_fail_when_the_file_is_missing
|
||
|
a = Attachment.create!(
|
||
|
:file => mock_file_with_options(:original_filename => 'test.bin', :content => "\x00\x01\x02".b),
|
||
|
:author_id => 1
|
||
|
)
|
||
|
File.delete(a.diskfile)
|
||
|
assert a.update(:filename => 'renamed.txt')
|
||
|
end
|
||
|
def test_shorted_filename_if_too_long
|
||