about summary refs log tree commit diff
path: root/app/models/concerns/attachmentable.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2020-06-30 23:58:02 +0200
committerGitHub <noreply@github.com>2020-06-30 23:58:02 +0200
commit7aaf2b44ec698fd4f20b927fcac7edc0394a2647 (patch)
tree036b96777e1f525b2b76aab36204dee30cfa3345 /app/models/concerns/attachmentable.rb
parent65506bac3f3fe233b5b7b3241020bd74eb5c9259 (diff)
Fix remote files not using Content-Type header, streaming (#14184)
Diffstat (limited to 'app/models/concerns/attachmentable.rb')
-rw-r--r--app/models/concerns/attachmentable.rb17
1 files changed, 13 insertions, 4 deletions
diff --git a/app/models/concerns/attachmentable.rb b/app/models/concerns/attachmentable.rb
index 18b872c1e..c5febb828 100644
--- a/app/models/concerns/attachmentable.rb
+++ b/app/models/concerns/attachmentable.rb
@@ -8,6 +8,17 @@ module Attachmentable
   MAX_MATRIX_LIMIT = 16_777_216 # 4096x4096px or approx. 16MB
   GIF_MATRIX_LIMIT = 921_600    # 1280x720px
 
+  # For some file extensions, there exist different content
+  # type variants, and browsers often send the wrong one,
+  # for example, sending an audio .ogg file as video/ogg,
+  # likewise, MimeMagic also misreports them as such. For
+  # those files, it is necessary to use the output of the
+  # `file` utility instead
+  INCORRECT_CONTENT_TYPES = %w(
+    video/ogg
+    video/webm
+  ).freeze
+
   included do
     before_post_process :obfuscate_file_name
     before_post_process :set_file_extensions
@@ -21,7 +32,7 @@ module Attachmentable
     self.class.attachment_definitions.each_key do |attachment_name|
       attachment = send(attachment_name)
 
-      next if attachment.blank? || attachment.queued_for_write[:original].blank?
+      next if attachment.blank? || attachment.queued_for_write[:original].blank? || !INCORRECT_CONTENT_TYPES.include?(attachment.instance_read(:content_type))
 
       attachment.instance_write :content_type, calculated_content_type(attachment)
     end
@@ -63,9 +74,7 @@ module Attachmentable
   end
 
   def calculated_content_type(attachment)
-    content_type = Paperclip.run('file', '-b --mime :file', file: attachment.queued_for_write[:original].path).split(/[:;\s]+/).first.chomp
-    content_type = 'video/mp4' if content_type == 'video/x-m4v'
-    content_type
+    Paperclip.run('file', '-b --mime :file', file: attachment.queued_for_write[:original].path).split(/[:;\s]+/).first.chomp
   rescue Terrapin::CommandLineError
     ''
   end