Sha256: 6f69583fc034345b408379949e1697212390c984993b91dca70ea257b304d082

Contents?: true

Size: 1.89 KB

Versions: 20

Compression:

Stored size: 1.89 KB

Contents

class Shrine
  module Plugins
    # Custom kithe logic for determining mime type, using the shrine mime_type plugin.
    #
    # We start out using the `marcel` analyzer.
    # Marcel analyzer is pure-ruby and fast. It's from Basecamp and is what
    # ActiveStorage uses. It is very similar to :mimemagic (and uses mimemagic
    # under the hood), but mimemagic seems not to be maintained with up to date
    # magic db? https://github.com/minad/mimemagic/pull/66
    #
    # But marcel is not able to catch some of our MP3s as audio/mpeg. The
    # `mediainfo` CLI is, and is one of the tools Harvard FITS uses.
    # If marcel came up blank, AND we are configured to use mediainfo CLI
    # (which by default we will be if it's available), we will try
    # shelling out to mediainfo command line.
    #
    # https://github.com/MediaArea/MediaInfo
    #
    # Ensure that if mime-type can't be otherwise determined, it is assigned
    # "application/octet-stream", basically the type for generic binary.
    class KitheDetermineMimeType
      # marcel version 1.0 says audio/x-flac, whereas previous versions
      # said audio/flac, which we prefer. Let's fix it.
      RPELACE_CONTENT_TYPES = {
        "audio/x-flac" => "audio/flac"
      }

      def self.load_dependencies(uploader, *)
        uploader.plugin :determine_mime_type, analyzer: -> (io, analyzers) do
          mime_type = analyzers[:marcel].call(io)


          if Kithe.use_mediainfo && mime_type == "application/octet-stream" || mime_type.blank?
            mime_type = Kithe::MediainfoAnalyzer.new.call(io)
          end

          mime_type = "application/octet-stream" if mime_type.blank?

          # Are there any we prefer an alternate spelling of?
          mime_type = RPELACE_CONTENT_TYPES.fetch(mime_type, mime_type)

          mime_type
        end
      end
    end
    register_plugin(:kithe_determine_mime_type, KitheDetermineMimeType)
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
kithe-2.16.0 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.15.1 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.15.0 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.14.0 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.13.0 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.12.0 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.11.0 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.10.0 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.9.1 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.9.0 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.8.0 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.7.1 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.7.0 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.6.1 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.6.0 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.5.0 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.4.0 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.3.0 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.2.0 lib/shrine/plugins/kithe_determine_mime_type.rb
kithe-2.1.0 lib/shrine/plugins/kithe_determine_mime_type.rb