about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2019-06-20 10:52:36 +0200
committermultiple creatures <dev@multiple-creature.party>2020-02-21 03:42:05 -0600
commit2dacf79bcc58094b58f1689189ce130fb34b2c8f (patch)
treef39a9740405d1738ae06d319699c9910bf1b37ac
parent480a83e843b92cc1fa4378b576bfd173024f3f8c (diff)
port tootsuite#11130 to monsterfork: Fix converted media being saved with original extension and mime type
-rw-r--r--app/models/concerns/attachmentable.rb2
-rw-r--r--app/models/media_attachment.rb8
-rw-r--r--config/application.rb2
-rw-r--r--lib/paperclip/type_corrector.rb19
4 files changed, 26 insertions, 5 deletions
diff --git a/app/models/concerns/attachmentable.rb b/app/models/concerns/attachmentable.rb
index dd11e0814..08af107c7 100644
--- a/app/models/concerns/attachmentable.rb
+++ b/app/models/concerns/attachmentable.rb
@@ -1,6 +1,6 @@
 # frozen_string_literal: true
 
-require 'mime/types'
+require 'mime/types/columnar'
 
 module Attachmentable
   extend ActiveSupport::Concern
diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb
index 3ef163f46..ce3549539 100644
--- a/app/models/media_attachment.rb
+++ b/app/models/media_attachment.rb
@@ -87,12 +87,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',
@@ -228,11 +230,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
diff --git a/config/application.rb b/config/application.rb
index 59bcc5664..27118342a 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -10,7 +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/audio_transcoder'
+require_relative '../lib/paperclip/type_corrector'
 require_relative '../lib/mastodon/snowflake'
 require_relative '../lib/mastodon/version'
 require_relative '../lib/devise/ldap_authenticatable'
diff --git a/lib/paperclip/type_corrector.rb b/lib/paperclip/type_corrector.rb
new file mode 100644
index 000000000..0b0c10a56
--- /dev/null
+++ b/lib/paperclip/type_corrector.rb
@@ -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