about summary refs log tree commit diff
path: root/lib/paperclip
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2019-06-20 10:52:36 +0200
committerGitHub <noreply@github.com>2019-06-20 10:52:36 +0200
commit8f23726918fd8ded18ce9e55e6199df87abcc7cf (patch)
tree136a343e791b418fd35354cebc00bca4aecef50f /lib/paperclip
parent7696f77245c2302787d239da50248385b3292a5e (diff)
Fix converted media being saved with original extension and mime type (#11130)
Diffstat (limited to 'lib/paperclip')
-rw-r--r--lib/paperclip/type_corrector.rb19
1 files changed, 19 insertions, 0 deletions
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