Fix converted media being saved with original extension and mime type (#11130)
This commit is contained in:
		
							parent
							
								
									7696f77245
								
							
						
					
					
						commit
						8f23726918
					
				
					 4 changed files with 26 additions and 4 deletions
				
			
		| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
require 'mime/types'
 | 
			
		||||
require 'mime/types/columnar'
 | 
			
		||||
 | 
			
		||||
module Attachmentable
 | 
			
		||||
  extend ActiveSupport::Concern
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
									
								
								lib/paperclip/type_corrector.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								lib/paperclip/type_corrector.rb
									
										
									
									
									
										Normal 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…
	
	Add table
		
		Reference in a new issue