LoginSignup
0
0

More than 1 year has passed since last update.

Tempfile オブジェクトから Content-Type(mime type) を取得する方法

Posted at

やりたいこと

Tempfile のオブジェクトを作成後、そこから Content-Type(mime type) を取得したい。

解決策

marcelMarcel::Magic.by_magic を使うと取得できる。

> tempfile = # PDF を Tempfile オブジェクトに変換
> Marcel::Magic.by_magic(tempfile).type
=> "application/pdf"

実装 を読むと io を引数に取れるみたいなので、 File クラスへの Delegator の Tempfile だけでなく StringIO もいけるかも(未検証)
ちなみに activestorage が依存してる gem なので、 rails の gem を入れたら一緒に付いてくるのでそのまま使える。便利。

確認バージョン

marcel: 1.0.2
rails: 6.1.4.4

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