Browse Source

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

mp-additions
Eugen Rochko 5 months ago
parent
commit
8f23726918
No account linked to committer's email address

+ 1
- 1
app/models/concerns/attachmentable.rb View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'mime/types'
require 'mime/types/columnar'

module Attachmentable
extend ActiveSupport::Concern

+ 5
- 3
app/models/media_attachment.rb 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

+ 1
- 0
config/application.rb 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'

+ 19
- 0
lib/paperclip/type_corrector.rb 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

Loading…
Cancel
Save