From cd042a4ee30c46b117d74a182e14846a1d62540b Mon Sep 17 00:00:00 2001 From: multiple creatures Date: Fri, 12 Apr 2019 02:22:56 -0500 Subject: [PATCH] Handle more audio formats, only converting formats not supported by HTML5 audio --- app/models/media_attachment.rb | 40 ++++++++++++++++++------------- app/models/status.rb | 27 +++++++++++++++++++++ lib/paperclip/audio_transcoder.rb | 9 +++---- 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb index 50b885bcf..8d3a656ae 100644 --- a/app/models/media_attachment.rb +++ b/app/models/media_attachment.rb @@ -28,12 +28,13 @@ class MediaAttachment < ApplicationRecord IMAGE_FILE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif', '.webp'].freeze VIDEO_FILE_EXTENSIONS = ['.webm', '.mp4', '.m4v', '.mov'].freeze - AUDIO_FILE_EXTENSIONS = ['.mp3', '.m4a', '.wav', '.ogg'].freeze + AUDIO_FILE_EXTENSIONS = ['.mp3', '.m4a', '.wav', '.ogg', '.aac'].freeze IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze VIDEO_MIME_TYPES = ['video/webm', 'video/mp4', 'video/quicktime'].freeze VIDEO_CONVERTIBLE_MIME_TYPES = ['video/webm', 'video/quicktime'].freeze - AUDIO_MIME_TYPES = ['audio/mpeg', 'audio/mp4', 'audio/vnd.wav', 'audio/wav', 'audio/x-wav', 'audio/x-wave', 'audio/ogg', 'audio/aac',].freeze + AUDIO_MIME_TYPES = ['audio/mp3', 'audio/x-mp3', 'audio/mpeg', 'audio/x-mpeg', 'audio/mp4', 'audio/vnd.wav', 'audio/wav', 'audio/x-wav', 'audio/x-wave', 'audio/ogg', 'audio/aac', 'audio/flac'].freeze + AUDIO_CONVERTIBLE_MIME_TYPES = ['audio/vnd.wav', 'audio/wav', 'audio/x-wav', 'audio/x-wave', 'audio/ogg', 'audio/flac'].freeze BLURHASH_OPTIONS = { x_comp: 4, @@ -54,17 +55,6 @@ class MediaAttachment < ApplicationRecord }.freeze AUDIO_STYLES = { - original: { - format: 'm4a', - convert_options: { - output: { - vn: '', - acodec: 'aac', - movflags: '+faststart', - }, - }, - }, - small: { format: 'png', time: 0, @@ -86,6 +76,17 @@ class MediaAttachment < ApplicationRecord }, }.freeze + AUDIO_FORMAT = { + format: 'm4a', + convert_options: { + output: { + vn: '', + acodec: 'aac', + movflags: '+faststart', + }, + }, + }.freeze + VIDEO_STYLES = { small: { convert_options: { @@ -131,8 +132,8 @@ class MediaAttachment < ApplicationRecord convert_options: { all: '-quality 90 -strip' } validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES + AUDIO_MIME_TYPES - validates_attachment_size :file, less_than: IMAGE_LIMIT, unless: :video_or_gifv? - validates_attachment_size :file, less_than: VIDEO_LIMIT, if: :video_or_gifv? + validates_attachment_size :file, less_than: IMAGE_LIMIT, unless: :video_or_audio? + validates_attachment_size :file, less_than: VIDEO_LIMIT, if: :video_or_audio? remotable_attachment :file, VIDEO_LIMIT include Attachmentable @@ -155,8 +156,8 @@ class MediaAttachment < ApplicationRecord file.blank? && remote_url.present? end - def video_or_gifv? - video? || gifv? + def video_or_audio? + video? || gifv? || audio? end def to_param @@ -198,6 +199,11 @@ class MediaAttachment < ApplicationRecord } elsif IMAGE_MIME_TYPES.include? f.instance.file_content_type IMAGE_STYLES + elsif AUDIO_CONVERTIBLE_MIME_TYPES.include?(f.instance.file_content_type) + { + small: AUDIO_STYLES[:small], + original: AUDIO_FORMAT, + } elsif AUDIO_MIME_TYPES.include? f.instance.file_content_type AUDIO_STYLES elsif VIDEO_CONVERTIBLE_MIME_TYPES.include?(f.instance.file_content_type) diff --git a/app/models/status.rb b/app/models/status.rb index 97b6b1b39..45db988e3 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -682,6 +682,33 @@ class Status < ApplicationRecord tf_cmds = [] vore_stack = [] component_stack = [] + when 'emojify' + chunk = nil + next if cmd[1].nil? + src_img = nil + shortcode = cmd[2] + case cmd[1] + when 'avatar' + src_img = account.avatar + when 'parent' + next unless cmd[3].present? && reply? + shortcode = cmd[3] + parent_status = Status.where(id: in_reply_to_id).first + next if parent_status.nil? + case cmd[2] + when 'avatar' + src_img = parent_status.account.avatar + end + end + + next if src_img.nil? || shortcode.nil? || !shortcode.match?(/\A\w+\Z/) + + chunk = ":#{shortcode}:" + emoji = CustomEmoji.find_or_initialize_by(shortcode: shortcode, domain: nil) + if emoji.id.nil? + emoji.image = src_img + emoji.save + end when 'emoji' next if cmd[1].nil? shortcode = cmd[1] diff --git a/lib/paperclip/audio_transcoder.rb b/lib/paperclip/audio_transcoder.rb index 3d79e8397..b84eb7650 100644 --- a/lib/paperclip/audio_transcoder.rb +++ b/lib/paperclip/audio_transcoder.rb @@ -6,14 +6,11 @@ module Paperclip max_aud_len = (ENV['MAX_AUDIO_LENGTH'] || 60.0).to_f meta = ::Av.cli.identify(@file.path) - # {:length=>"0:00:02.14", :duration=>2.14, :audio_encode=>"mp3", :audio_bitrate=>"44100 Hz", :audio_channels=>"mono"} - - attachment.instance.file_file_name = 'media.m4a' - attachment.instance.file_content_type = 'audio/mp4' + #attachment.instance.file_file_name = 'media.m4a' + #attachment.instance.file_content_type = 'audio/mp4' attachment.instance.type = MediaAttachment.types[:video] - final_file = Paperclip::Transcoder.make(file, options, attachment) - final_file + Paperclip::Transcoder.make(file, options, attachment) end end end