|
|
@ -23,15 +23,32 @@ require 'mime/types'
|
|
|
|
class MediaAttachment < ApplicationRecord
|
|
|
|
class MediaAttachment < ApplicationRecord
|
|
|
|
self.inheritance_column = nil
|
|
|
|
self.inheritance_column = nil
|
|
|
|
|
|
|
|
|
|
|
|
enum type: [:image, :gifv, :video, :unknown]
|
|
|
|
enum type: [:image, :gifv, :video, :audio, :unknown]
|
|
|
|
|
|
|
|
|
|
|
|
IMAGE_FILE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif'].freeze
|
|
|
|
IMAGE_FILE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif'].freeze
|
|
|
|
VIDEO_FILE_EXTENSIONS = ['.webm', '.mp4', '.m4v'].freeze
|
|
|
|
VIDEO_FILE_EXTENSIONS = ['.webm', '.mp4', '.m4v'].freeze
|
|
|
|
|
|
|
|
AUDIO_FILE_EXTENSIONS = ['.mp3', '.m4a', '.wav', '.ogg'].freeze
|
|
|
|
|
|
|
|
|
|
|
|
IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
|
|
|
|
IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
|
|
|
|
VIDEO_MIME_TYPES = ['video/webm', 'video/mp4'].freeze
|
|
|
|
VIDEO_MIME_TYPES = ['video/webm', 'video/mp4'].freeze
|
|
|
|
|
|
|
|
AUDIO_MIME_TYPES = ['audio/mpeg', 'audio/mp4', 'audio/vnd.wav', 'audio/wav', 'audio/x-wav', 'audio/x-wave', 'audio/ogg',].freeze
|
|
|
|
|
|
|
|
|
|
|
|
IMAGE_STYLES = { original: '1280x1280>', small: '400x400>' }.freeze
|
|
|
|
IMAGE_STYLES = { original: '1280x1280>', small: '400x400>' }.freeze
|
|
|
|
|
|
|
|
AUDIO_STYLES = {
|
|
|
|
|
|
|
|
original: {
|
|
|
|
|
|
|
|
format: 'mp4',
|
|
|
|
|
|
|
|
convert_options: {
|
|
|
|
|
|
|
|
output: {
|
|
|
|
|
|
|
|
filter_complex: '"[0:a]compand,showwaves=s=640x360:mode=line,format=yuv420p[v]"',
|
|
|
|
|
|
|
|
map: '"[v]" -map 0:a',
|
|
|
|
|
|
|
|
threads: 2,
|
|
|
|
|
|
|
|
vcodec: 'libx264',
|
|
|
|
|
|
|
|
acodec: 'aac',
|
|
|
|
|
|
|
|
movflags: '+faststart',
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}.freeze
|
|
|
|
VIDEO_STYLES = {
|
|
|
|
VIDEO_STYLES = {
|
|
|
|
small: {
|
|
|
|
small: {
|
|
|
|
convert_options: {
|
|
|
|
convert_options: {
|
|
|
@ -54,7 +71,7 @@ class MediaAttachment < ApplicationRecord
|
|
|
|
|
|
|
|
|
|
|
|
include Remotable
|
|
|
|
include Remotable
|
|
|
|
|
|
|
|
|
|
|
|
validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES
|
|
|
|
validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES + AUDIO_MIME_TYPES
|
|
|
|
validates_attachment_size :file, less_than: 8.megabytes
|
|
|
|
validates_attachment_size :file, less_than: 8.megabytes
|
|
|
|
|
|
|
|
|
|
|
|
validates :account, presence: true
|
|
|
|
validates :account, presence: true
|
|
|
@ -107,6 +124,8 @@ class MediaAttachment < ApplicationRecord
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif IMAGE_MIME_TYPES.include? f.instance.file_content_type
|
|
|
|
elsif IMAGE_MIME_TYPES.include? f.instance.file_content_type
|
|
|
|
IMAGE_STYLES
|
|
|
|
IMAGE_STYLES
|
|
|
|
|
|
|
|
elsif AUDIO_MIME_TYPES.include? f.instance.file_content_type
|
|
|
|
|
|
|
|
AUDIO_STYLES
|
|
|
|
else
|
|
|
|
else
|
|
|
|
VIDEO_STYLES
|
|
|
|
VIDEO_STYLES
|
|
|
|
end
|
|
|
|
end
|
|
|
@ -117,6 +136,8 @@ class MediaAttachment < ApplicationRecord
|
|
|
|
[:gif_transcoder]
|
|
|
|
[:gif_transcoder]
|
|
|
|
elsif VIDEO_MIME_TYPES.include? f.file_content_type
|
|
|
|
elsif VIDEO_MIME_TYPES.include? f.file_content_type
|
|
|
|
[:video_transcoder]
|
|
|
|
[:video_transcoder]
|
|
|
|
|
|
|
|
elsif AUDIO_MIME_TYPES.include? f.file_content_type
|
|
|
|
|
|
|
|
[:audio_transcoder]
|
|
|
|
else
|
|
|
|
else
|
|
|
|
[:thumbnail]
|
|
|
|
[:thumbnail]
|
|
|
|
end
|
|
|
|
end
|
|
|
@ -137,8 +158,8 @@ class MediaAttachment < ApplicationRecord
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def set_type_and_extension
|
|
|
|
def set_type_and_extension
|
|
|
|
self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : :image
|
|
|
|
self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : AUDIO_MIME_TYPES.include?(file_content_type) ? :audio : :image
|
|
|
|
extension = appropriate_extension
|
|
|
|
extension = AUDIO_MIME_TYPES.include?(file_content_type) ? '.mp4' : appropriate_extension
|
|
|
|
basename = Paperclip::Interpolations.basename(file, :original)
|
|
|
|
basename = Paperclip::Interpolations.basename(file, :original)
|
|
|
|
file.instance_write :file_name, [basename, extension].delete_if(&:blank?).join('.')
|
|
|
|
file.instance_write :file_name, [basename, extension].delete_if(&:blank?).join('.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|