LoginSignup
17
17

More than 5 years have passed since last update.

Paperclipで写真も動画も同じカラムを使いアップロードとサムネイルの作成をする

Posted at

PaperclipPaperclip FFMPEGを使って写真も動画も同じカラムでアップロードとサムネイルの作成を行いたい場合の書き方です。

has_attached_filestylesprocessors を以下のような感じで書く。

has_attached_file :attachment,
  styles: lambda { |file|
    if file.instance.is_video?
      {
        medium:    { geometry: '240x240#', format: 'jpg', time: 0, auto_rotate: true },
        thumbnail: { geometry: '64x64#', format: 'jpg', time: 0, auto_rotate: true },
      }
    else
      { medium: '240x240#', thumbnail: '64x64#' }
    end
  },
  storage: :filesystem,
  path: ":attachment/:id/:style/:filename",
  processors: lambda { |file| file.is_video? ? [ :ffmpeg ] : [ :thumbnail ] }

def is_video?
  attachment.instance.attachment_content_type =~ %r(video) ? true : false
end
17
17
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
17
17