Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

ContentTypeDetector false positive from Mime #2708

Open
daniel-gato opened this issue May 3, 2023 · 1 comment
Open

ContentTypeDetector false positive from Mime #2708

daniel-gato opened this issue May 3, 2023 · 1 comment

Comments

@daniel-gato
Copy link

We ran into a bug. We have a gltf binary buffer (.bin) file that gets detected as image/jpeg. This crashes our Model because we do some identify stuff based on the type for images.

Essentially the 3 method where the content type could come from are:

  • type_from_mime_magic this returns image/jpeg
  • type_from_file_command this returns application/octet-stream
  • calculated_type_matches this returns a application/octet-stream

While looking at the detect method:

    def detect
      if blank_name?
        SENSIBLE_DEFAULT
      elsif empty_file?
        EMPTY_TYPE
      elsif calculated_type_matches.any?
        calculated_type_matches.first
      else
        type_from_file_contents || SENSIBLE_DEFAULT
      end.to_s
    end

we realise that type_from_file_contents is type_from_mime_magic || type_from_file_command so it will always return the wrong one.

and then, calculated_type_matches uses type_from_file_contents to select a value. That means even if type_from_file_command is present and correct, as long as type_from_mime_magicis present it will be wrong.

to make it work, we change the method to this:

module Paperclip
  class ContentTypeDetector
    def calculated_type_matches
      possible_types.select do |content_type|
        [type_from_mime_magic, type_from_file_command].include?(content_type)
      end
    end
  end
@sarahraqueld
Copy link

sarahraqueld commented Jul 5, 2023

Hi @daniel-gato,

From the README: Paperclip is deprecated. Kreeti is maintaining kt-paperclip, an ongoing fork of Paperclip, and all bug reports should go to kt-paperclip.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants