In short, your file is being detected as JPEG because it starts with the same signature as this type of file.
Fixing this behavior in Discourse is possible, but it requires a modification. (see at the end)
Here some technical stuff.
This issue starts here:
FastImage library opens the file and determines the type and size.
As you expect, it returns a type of JPEG.
If you look at what is JPEG signature, it looks like this:
Now, if you look at how FastImage detects a JPEG, you can see here:
However, you can’t extract any image information since not all the required bytes are there.
How do we fix this issue in Discourse?
Looking at FastImage code, there is a valuable option you can pass.
By using this option, any error ( SizeNotFound, ImageFetchFailure, CannotParseImage, UnknownImageType, BadImageURI) will return no image info ; and your file won’t be detected as an image.
@image_info =
begin
FastImage.new(@file, :raise_on_failure=>true)
rescue StandardError
nil
end
...
is_image ||= @image_info && FileHelper.is_supported_image?("test.#{@image_info.type}")