1
0
mirror of https://github.com/funamitech/mastodon synced 2024-11-30 07:49:04 +09:00

Merge pull request #161 from DJSundog/audio-uploads-feature

Audio uploads feature
This commit is contained in:
beatrix 2017-10-07 20:13:53 -04:00 committed by GitHub
commit 7cc0da756d
4 changed files with 48 additions and 5 deletions

View File

@ -23,15 +23,32 @@ require 'mime/types'
class MediaAttachment < ApplicationRecord
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
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
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
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 = {
small: {
convert_options: {
@ -54,7 +71,7 @@ class MediaAttachment < ApplicationRecord
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 :account, presence: true
@ -107,6 +124,8 @@ class MediaAttachment < ApplicationRecord
}
elsif IMAGE_MIME_TYPES.include? f.instance.file_content_type
IMAGE_STYLES
elsif AUDIO_MIME_TYPES.include? f.instance.file_content_type
AUDIO_STYLES
else
VIDEO_STYLES
end
@ -117,6 +136,8 @@ class MediaAttachment < ApplicationRecord
[:gif_transcoder]
elsif VIDEO_MIME_TYPES.include? f.file_content_type
[:video_transcoder]
elsif AUDIO_MIME_TYPES.include? f.file_content_type
[:audio_transcoder]
else
[:thumbnail]
end
@ -137,8 +158,8 @@ class MediaAttachment < ApplicationRecord
end
def set_type_and_extension
self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : :image
extension = appropriate_extension
self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : AUDIO_MIME_TYPES.include?(file_content_type) ? :audio : :image
extension = AUDIO_MIME_TYPES.include?(file_content_type) ? '.mp4' : appropriate_extension
basename = Paperclip::Interpolations.basename(file, :original)
file.instance_write :file_name, [basename, extension].delete_if(&:blank?).join('.')
end

View File

@ -52,6 +52,6 @@ class InitialStateSerializer < ActiveModel::Serializer
end
def media_attachments
{ accept_content_types: MediaAttachment::IMAGE_FILE_EXTENSIONS + MediaAttachment::VIDEO_FILE_EXTENSIONS + MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES }
{ accept_content_types: MediaAttachment::IMAGE_FILE_EXTENSIONS + MediaAttachment::VIDEO_FILE_EXTENSIONS + MediaAttachment::AUDIO_FILE_EXTENSIONS + MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES + MediaAttachment::AUDIO_MIME_TYPES }
end
end

View File

@ -9,6 +9,7 @@ Bundler.require(*Rails.groups)
require_relative '../app/lib/exceptions'
require_relative '../lib/paperclip/gif_transcoder'
require_relative '../lib/paperclip/video_transcoder'
require_relative '../lib/paperclip/audio_transcoder'
require_relative '../lib/mastodon/version'
Dotenv::Railtie.load

View File

@ -0,0 +1,21 @@
# frozen_string_literal: true
module Paperclip
class AudioTranscoder < Paperclip::Processor
def make
meta = ::Av.cli.identify(@file.path)
# {:length=>"0:00:02.14", :duration=>2.14, :audio_encode=>"mp3", :audio_bitrate=>"44100 Hz", :audio_channels=>"mono"}
if meta[:duration] > 60.0
raise Mastodon::ValidationError, "Audio uploads must be less than 60 seconds in length."
end
final_file = Paperclip::Transcoder.make(file, options, attachment)
attachment.instance.file_file_name = 'media.mp4'
attachment.instance.file_content_type = 'video/mp4'
attachment.instance.type = MediaAttachment.types[:video]
final_file
end
end
end