Project

General

Profile

Feature #44362 » 0001-Extract-the-fallback-for-undetermined-content-types-.patch

Go MAEDA, 2026-08-20 13:53

View differences:

app/controllers/attachments_controller.rb
end
def detect_content_type(attachment, is_thumb = false)
content_type = attachment.content_type
if content_type.blank? || content_type == "application/octet-stream"
content_type =
Redmine::MimeType.of(attachment.filename).presence ||
"application/octet-stream"
end
content_type = Redmine::MimeType.infer_type(attachment.content_type, attachment.filename)
if is_thumb && !content_type.start_with?("image/")
# Thumbnails of non-image files are stored in PNG format
app/helpers/icons_helper.rb
end
def icon_for_mime_type(mime, filename = nil)
# Content type detection falls back to application/octet-stream when it
# cannot tell the type from the file contents. In that case only, guess
# the type from the filename, as AttachmentsController#detect_content_type
# does.
if filename.present? && (mime.blank? || mime == 'application/octet-stream')
mime = Redmine::MimeType.of(filename)
end
mime = Redmine::MimeType.infer_type(mime, filename)
MIME_TYPE_ICONS[mime] ||
MIME_TYPE_ICONS[mime.to_s.split('/').first] ||
lib/redmine/mime_type.rb
end
end
# Returns content_type, or the type guessed from name if content_type is
# undetermined, ie. blank or application/octet-stream
def self.infer_type(content_type, name)
if content_type.blank? || content_type == 'application/octet-stream'
of(name).presence || 'application/octet-stream'
else
content_type
end
end
# Returns the css class associated to
# the mime type of name
def self.css_class_of(name)
test/unit/lib/redmine/mime_type_test.rb
assert_nil Redmine::MimeType.of('test.unk')
end
def test_infer_type
to_test = {
# A determined type is returned as is, even if the filename suggests
# another type
['text/html', 'not-really-an-image.png'] => 'text/html',
# An undetermined type is resolved from the filename
['application/octet-stream', 'test.pdf'] => 'application/pdf',
['', 'test.pdf'] => 'application/pdf',
[nil, 'test.pdf'] => 'application/pdf',
# application/octet-stream is kept when the filename does not help
['application/octet-stream', 'test.unk'] => 'application/octet-stream',
[nil, 'test.unk'] => 'application/octet-stream',
}
to_test.each do |args, expected|
assert_equal expected, Redmine::MimeType.infer_type(*args)
end
end
def test_css_class_of
to_test = {
'test.txt' => 'text-plain',
(1-1/5)