about summary refs log tree commit diff
path: root/lib/paperclip/type_corrector.rb
blob: 17e2fc5daaa147779c57742ce7ad774a4f995581 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# frozen_string_literal: true

require 'mime/types/columnar'

module Paperclip
  class TypeCorrector < Paperclip::Processor
    def make
      return @file unless options[:format]

      target_extension = '.' + options[:format]
      extension        = File.extname(attachment.instance_read(:file_name))

      return @file unless options[:style] == :original && target_extension && extension != target_extension

      attachment.instance_write(:content_type, options[:content_type] || attachment.instance_read(:content_type))
      attachment.instance_write(:file_name, File.basename(attachment.instance_read(:file_name), '.*') + target_extension)

      @file
    end
  end
end