about summary refs log tree commit diff
path: root/app/models/concerns
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2019-06-22 16:54:06 +0200
committerGitHub <noreply@github.com>2019-06-22 16:54:06 +0200
commitb927bb3f07b88d0a7bc39a7661d26dd1c9fc05d4 (patch)
treef5ae10f6c2cb65d6a560b0f849d4d1d8614c3a15 /app/models/concerns
parent6eb5241099357fd75a19c2d51245c197b517d363 (diff)
Fix audio-only OGG and WebM files not being processed as such (#11151)
Also, because Chrome sends audio/mp3 instead of audio/mpeg as it's
supposed to, we need to whitelist that mime type as well
Diffstat (limited to 'app/models/concerns')
-rw-r--r--app/models/concerns/attachmentable.rb17
1 files changed, 17 insertions, 0 deletions
diff --git a/app/models/concerns/attachmentable.rb b/app/models/concerns/attachmentable.rb
index f4e37f1e6..24f5968de 100644
--- a/app/models/concerns/attachmentable.rb
+++ b/app/models/concerns/attachmentable.rb
@@ -10,10 +10,21 @@ module Attachmentable
   included do
     before_post_process :set_file_extensions
     before_post_process :check_image_dimensions
+    before_post_process :set_file_content_type
   end
 
   private
 
+  def set_file_content_type
+    self.class.attachment_definitions.each_key do |attachment_name|
+      attachment = send(attachment_name)
+
+      next if attachment.blank? || attachment.queued_for_write[:original].blank?
+
+      attachment.instance_write :content_type, calculated_content_type(attachment)
+    end
+  end
+
   def set_file_extensions
     self.class.attachment_definitions.each_key do |attachment_name|
       attachment = send(attachment_name)
@@ -47,4 +58,10 @@ module Attachmentable
 
     extension
   end
+
+  def calculated_content_type(attachment)
+    Paperclip.run('file', '-b --mime :file', file: attachment.queued_for_write[:original].path).split(/[:;\s]+/).first.chomp
+  rescue Terrapin::CommandLineError
+    ''
+  end
 end