Project

General

Profile

Feature #43484 » 0004-Detect-attachment-content-type-from-file-contents-in.patch

Go MAEDA, 2026-03-04 10:03

View differences:

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
saved = @attachment.save
respond_to do |format|
app/models/attachment.rb
cattr_accessor :thumbnails_storage_path
@@thumbnails_storage_path = File.join(Rails.root, "tmp", "thumbnails")
before_create :files_to_final_location
before_create :set_content_type, :files_to_final_location
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)
self.content_type = Marcel::MimeType.for(io, name: filename)
@temp_file.rewind if @temp_file.respond_to?(:rewind)
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
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?
# Re-evaluate MIME type after rename. Marcel uses the filename as a hint
if a.filename_changed?
File.open(a.diskfile, 'rb') do |io|
a.content_type = Marcel::MimeType.for(io, name: a.filename)
end
end
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_given_content_type_attribute
# Text file upload with a long bogus content type
a = Attachment.new(:container => Issue.find(1),
:file => uploaded_test_file("testfile.txt", "text/plain"),
:file => uploaded_test_file("testfile.txt", nil),
:author => User.find(1),
:content_type => 'a'*300)
assert a.save
a.reload
assert_nil a.content_type
assert_equal 'text/plain', a.content_type
end
def test_create_should_not_trust_client_declared_content_type
# PDF file uploaded with a wrong content type "text/plain"
a = Attachment.new(:container => Issue.find(1),
:file => uploaded_test_file("hello.pdf", "text/plain"),
:author => User.find(1))
assert a.save
a.reload
assert_equal 'application/pdf', a.content_type
end
def test_shorted_filename_if_too_long
......
assert_equal 'text/plain', attachment.content_type
end
test "Attachment.attach_files should recalculate the content_type when filename changes" do
@project = Project.find(1)
attachment = Attachment.create!(
:file => mock_file_with_options(:original_filename => "test.bin", :content => "\x00\x01\x02".b),
:author_id => 1,
:created_on => 2.days.ago
)
assert_equal 'application/octet-stream', attachment.content_type
Attachment.attach_files(@project, {'1' => {'token' => attachment.token, 'filename' => 'renamed.txt'}})
attachment.reload
assert_equal 'text/plain', attachment.content_type
end
def test_update_digest_to_sha256_should_update_digest
set_fixtures_attachments_directory
attachment = Attachment.find 6
(4-4/10)