Getting a files mime-type

Hi,

Can anyone tell me if there is a simple way to get the mime-type of a
file?
(I've seen the Mime::Type library, but haven't figured out how to use
it).
I'm trying to check the type before processing a bunch of files on disk
(and ignoring files that I'm not interested in. Is there a better way
than using the file extension?

thanks,
Dan

···

--
Posted via http://www.ruby-forum.com/.

MIME::Types.type_for(filename)

Using a file extension is fast. It's what MIME::Types does. It obviously doesn't work if the extension doesn't match the file type, though.

The alternative is to use a heuristic that identifies distinct characteristics of different file types in order to guess at the type. This can identify the type of a file when the extension doesn't match the type, but it isn't necessarily 100% certain, either.

If you have Apache, look in the conf directory for a 'magic' file. mod_mime_magic uses that to perform type guessing. I am unaware of a mime magic library for Ruby at this time, but if anyone knows of one, I'd love to be pointed towards it.

Kirk Haines

···

On Fri, 11 Aug 2006, Daniel Greig wrote:

Can anyone tell me if there is a simple way to get the mime-type of a
file?
(I've seen the Mime::Type library, but haven't figured out how to use
it).
I'm trying to check the type before processing a bunch of files on disk
(and ignoring files that I'm not interested in. Is there a better way
than using the file extension?

Daniel Greig wrote:

Hi,

Can anyone tell me if there is a simple way to get the mime-type of a
file?
(I've seen the Mime::Type library, but haven't figured out how to use
it).
I'm trying to check the type before processing a bunch of files on disk
(and ignoring files that I'm not interested in. Is there a better way
than using the file extension?

thanks,
Dan

oops, content_type/mime_type is available when a file is transfered via
the web, but it seems not from disk. It was a rails function I was
thinking of (content_type) for uploaded files.
my bad...

···

--
Posted via http://www.ruby-forum.com/\.

Daniel Greig wrote:

Hi,

Can anyone tell me if there is a simple way to get the mime-type of a
file?
(I've seen the Mime::Type library, but haven't figured out how to use
it).
I'm trying to check the type before processing a bunch of files on disk
(and ignoring files that I'm not interested in. Is there a better way
than using the file extension?

On *x systems, you can use the 'file' utility.

$ file filename.ext

···

thanks,
Dan

--
Posted via http://www.ruby-forum.com/\.

The Calgary Ruby Users's Society (CRUSERS) is working on this in hack
sessions. I don't know if they're anywhere near release yet.

-austin

···

On 8/11/06, khaines@enigo.com <khaines@enigo.com> wrote:

If you have Apache, look in the conf directory for a 'magic' file.
mod_mime_magic uses that to perform type guessing. I am unaware of a mime
magic library for Ruby at this time, but if anyone knows of one, I'd love
to be pointed towards it.

--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca

unknown wrote:

···

On Fri, 11 Aug 2006, Daniel Greig wrote:

Can anyone tell me if there is a simple way to get the mime-type of a
file?
(I've seen the Mime::Type library, but haven't figured out how to use
it).
I'm trying to check the type before processing a bunch of files on disk
(and ignoring files that I'm not interested in. Is there a better way
than using the file extension?

MIME::Types.type_for(filename)

Using a file extension is fast. It's what MIME::Types does. It
obviously
doesn't work if the extension doesn't match the file type, though.

The alternative is to use a heuristic that identifies distinct
characteristics of different file types in order to guess at the type.
This can identify the type of a file when the extension doesn't match
the
type, but it isn't necessarily 100% certain, either.

If you have Apache, look in the conf directory for a 'magic' file.
mod_mime_magic uses that to perform type guessing. I am unaware of a
mime
magic library for Ruby at this time, but if anyone knows of one, I'd
love
to be pointed towards it.

Kirk Haines

I was looking for the same thing, found
http://shared-mime.rubyforge.org/ :

"shared-mime-info is a pure Ruby library for accessing the MIME info
database provided by Freedesktop on Standards/shared-mime-info-spec.
This provides a way to guess the mime type of a file by doing both
filename lookups and magic file checks."

--
Posted via http://www.ruby-forum.com/\.