Getting a files mime-type

D

Daniel Greig

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
 
K

khaines

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
 
D

Daniel Greig

Daniel said:
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...
 
E

Eero Saynatkari

Daniel said:
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
 
A

Austin Ziegler

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 Users's Society (CRUSERS) is working on this in hack
sessions. I don't know if they're anywhere near release yet.

-austin
 
T

Thomas Nichols

unknown said:
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."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top