[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Getting a files mime-type

Daniel Greig

8/11/2006 3:04:00 AM

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

5 Answers

khaines

8/11/2006 7:01:00 AM

0

Daniel Greig

8/11/2006 7:38:00 AM

0

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

e

8/11/2006 5:57:00 PM

0

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

Austin Ziegler

8/12/2006 1:36:00 PM

0

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.

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
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Thomas Nichols

6/20/2007 3:24:00 PM

0

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.ruby... :

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