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

Daniel G. 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…

Daniel G. 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

On Fri, 11 Aug 2006, Daniel G. 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 H.

On 8/11/06, [email protected] [email protected] 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.

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

-austin

unknown wrote:

On Fri, 11 Aug 2006, Daniel G. 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 H.

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.”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs