Trouble with webm mime types

160 views
Skip to first unread message

b...@dee.mx

unread,
Sep 6, 2017, 9:46:41 PM9/6/17
to Shrine
Hello, looking for some insight on this one

I first had troubles understanding why webm's files were not accepted, using with the following list of mime-types 

VIDEO_FORMATS = [ 'video/mp4','video/webm','application/mp4', 'video/mp4', 'video/quicktime', 'video/avi', 'video/mpeg', 'video/x-mpeg', 'video/x-msvideo', 'video/m4v', 'video/x-m4v', 'video/vnd.objectvideo'].freeze

using

Attacher.validate do
   validate_mime_type_inclusion [*IMAGE_FORMATS,*VIDEO_FORMATS,*AUDIO_FORMATS,*DOC_FORMATS]
end

I've added some tracing to debug, at different points :

plugin :determine_mime_type, analyzer: -> (io, analyzers) do
    mime_type = analyzers[:file].call(io)
    mime_type = analyzers[:mime_types].call(io) if mime_type == "text/plain"
    Rails.logger.info mime_type
    mime_type
end

and

add_metadata do |io, context|
  Rails.logger.info MIME::Types.type_for(io.path)
  ...
end

Turns out that :
- determine_mime_type that returns application/octet-stream
MIME::Types.type_for returns and array [#<MIME::Type: audio/webm>, #<MIME::Type: video/webm>]

I expected shrine receiving video/webm only
You might be aware of, Shrine validates the first one, I add to include application/octet-stream in that accepted mime types

My problem is that, laking of enough knowledge in file types, it feels quite bad to declare an application/octet-stream to accept videos uploads on the app
Am i right to feel such setup unsafe ?
Thx for you help on that one


Janko Marohnić

unread,
Sep 7, 2017, 3:41:15 AM9/7/17
to b...@dee.mx, Shrine
I'm not able to reproduce this behaviour. For me the `file` utility correctly detects the web MIME type:

$ file --mime-type --brief Volcano_Lava_Sample.webm
video/webm

I also executed the following self-contained script:


require "shrine"
require "shrine/storage/file_system"
require "tmpdir"
require "fileutils"

Shrine.storages[:storage] = Shrine::Storage::FileSystem.new("#{Dir.tmpdir}/shrine")
at_exit { FileUtils.rm_rf Shrine.storages[:storage].directory }

class MyUploader < Shrine
  plugin :determine_mime_type, analyzer: -> (io, analyzers) do
    mime_type = analyzers[:file].call(io)
    mime_type = analyzers[:mime_types].call(io) if mime_type == "text/plain"
    mime_type
  end
end

file          = File.open("/Users/janko/Downloads/Volcano_Lava_Sample.webm")
uploaded_file = MyUploader.new(:storage).upload(file)

puts uploaded_file.mime_type


And it also correctly prints "video/webm". Can you execute this script on your machine and tell me what it prints out? It might be that you're having an older version of the "magic" database that the file utility uses.

Kind regards,
Janko

--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine+unsubscribe@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/ebf3e460-35e3-4872-bb47-b6377ed1e2ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ben Bonnet

unread,
Sep 7, 2017, 5:32:54 AM9/7/17
to Shrine
I'm on another machine, and all works fine. Sorry for disturbing , indeed it is due to my setup :/
Do have the same result with  your script, also within the app
I'll look into that, in case you have time, can you tell me what you call "magic" database ? meaning i should reinstall imagemagick with other specific extensions ?

In any case thx a lot for your time checking my first question;
best

To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.

Janko Marohnić

unread,
Sep 7, 2017, 6:29:33 AM9/7/17
to Ben Bonnet, Shrine
I'll look into that, in case you have time, can you tell me what you call "magic" database ? meaning i should reinstall imagemagick with other specific extensions ?

The "magic" database isn't related to ImageMagick (even though it might sound like they are), it comes with the `file` comand package, and as far as I know it's only used by the `file` command.

As you might already know, the `file` command determines the MIME type from the content of the file. This is because most file types have something in their content that uniquely identifies their type (the most obvious exceptions are text-based files like CSV). The "magic" database is a file (or a group of files) that specify for which MIME type to look into which part of the file for which sequence of bytes.

The easiest way to understand it is to look at the MimeMagic gem, which has its own "magic" table written in Ruby. MimeMagic just iterates through the whole table, and looks for the "magic headers" at the specified byte range. If it finds the bytes "\211PNG" at the start of the file, it knows that it's an "image/png". If it find the bytes "WEBP" starting from the 9th byte, it knows that it's an "image/webp". And so on.

I think if you update the `file` command package, it will also update its "magic" database.

Kind regards,
Janko

To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine+unsubscribe@googlegroups.com.

To post to this group, send email to ruby-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages