PaperclipとPaperclip FFMPEGを使って写真も動画も同じカラムでアップロードとサムネイルの作成を行いたい場合の書き方です。
has_attached_file
の styles
と processors
を以下のような感じで書く。
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