Fix converted media being saved with original extension and mime type (#11130)

This commit is contained in:
Eugen Rochko 2019-06-20 10:52:36 +02:00 committed by GitHub
parent 7696f77245
commit 8f23726918
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 4 deletions

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true
require 'mime/types'
require 'mime/types/columnar'
module Attachmentable
extend ActiveSupport::Concern

View file

@ -70,12 +70,14 @@ class MediaAttachment < ApplicationRecord
AUDIO_STYLES = {
original: {
format: 'ogg',
content_type: 'audio/ogg',
convert_options: {},
},
}.freeze
VIDEO_FORMAT = {
format: 'mp4',
content_type: 'video/mp4',
convert_options: {
output: {
'loglevel' => 'fatal',
@ -189,11 +191,11 @@ class MediaAttachment < ApplicationRecord
if f.file_content_type == 'image/gif'
[:gif_transcoder, :blurhash_transcoder]
elsif VIDEO_MIME_TYPES.include?(f.file_content_type)
[:video_transcoder, :blurhash_transcoder]
[:video_transcoder, :blurhash_transcoder, :type_corrector]
elsif AUDIO_MIME_TYPES.include?(f.file_content_type)
[:transcoder]
[:transcoder, :type_corrector]
else
[:lazy_thumbnail, :blurhash_transcoder]
[:lazy_thumbnail, :blurhash_transcoder, :type_corrector]
end
end
end

View file

@ -10,6 +10,7 @@ require_relative '../app/lib/exceptions'
require_relative '../lib/paperclip/lazy_thumbnail'
require_relative '../lib/paperclip/gif_transcoder'
require_relative '../lib/paperclip/video_transcoder'
require_relative '../lib/paperclip/type_corrector'
require_relative '../lib/mastodon/snowflake'
require_relative '../lib/mastodon/version'
require_relative '../lib/devise/ldap_authenticatable'

View file

@ -0,0 +1,19 @@
# frozen_string_literal: true
require 'mime/types/columnar'
module Paperclip
class TypeCorrector < Paperclip::Processor
def make
target_extension = options[:format]
extension = File.extname(attachment.instance.file_file_name)
return @file unless options[:style] == :original && target_extension && extension != target_extension
attachment.instance.file_content_type = options[:content_type] || attachment.instance.file_content_type
attachment.instance.file_file_name = File.basename(attachment.instance.file_file_name, '.*') + '.' + target_extension
@file
end
end
end