Browse Source

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

pull/4/head
Eugen Rochko 4 years ago
committed by GitHub
parent
commit
8f23726918
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 4 deletions
  1. +1
    -1
      app/models/concerns/attachmentable.rb
  2. +5
    -3
      app/models/media_attachment.rb
  3. +1
    -0
      config/application.rb
  4. +19
    -0
      lib/paperclip/type_corrector.rb

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

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

+ 5
- 3
app/models/media_attachment.rb View File

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