LoginSignup
6
5

More than 5 years have passed since last update.

shared-mime-infoでファイルタイプを判別する

Posted at

今更ながらに使ってみたのでメモしておく
shared-mime-infoは拡張子/ファイルの中身を見てファイルタイプを判別してくれる

jpeg_fileは拡張子を削ったjpegファイル

sample1.rb
require "shared-mime-info"
MIME.check "jpeg_file"        #=> image/jpeg
MIME.check_globs "jpeg_file"  #=> nil
MIME.check_magics "jpeg_file" #=> image/jpeg

sample1で使ったファイルに拡張子png(拡張子偽装)してみた結果
MIME.checkだと、拡張子があればそちらが優先される様子

sample2.rb
require "shared-mime-info"
MIME.check "jpeg_file.png"        #=> image/png
MIME.check_globs "jpeg_file.png"  #=> image/png
MIME.check_magics "jpeg_file.png" #=> image/jpeg
6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5