Detect MIME type of uploaded file in Ruby

后端 未结 7 1004
刺人心
刺人心 2020-12-02 09:45

Is there a bullet proof way to detect MIME type of uploaded file in Ruby or Ruby on Rails? I\'m uploading JPEGs and PNGs using SWFupload and content_type is alw

7条回答
  •  庸人自扰
    2020-12-02 10:32

    mimemagic gem will also do it

    https://github.com/minad/mimemagic

    from the oficial documentation

    MimeMagic is a library to detect the mime type of a file by extension or by content. It uses the mime database provided by freedesktop.org (see http://freedesktop.org/wiki/Software/shared-mime-info/).

    require 'mimemagic'
    MimeMagic.by_extension('html').text?
    MimeMagic.by_extension('.html').child_of? 'text/plain'
    MimeMagic.by_path('filename.txt')
    MimeMagic.by_magic(File.open('test.html'))
    # etc...
    

提交回复
热议问题