Paperclip how to change basename (filename)?

前端 未结 4 2713
粉色の甜心
粉色の甜心 2021-02-20 15:31

I am trying to change the basename (filename) of photos:

In my model I have:

  attr_accessor :image_url, :basename

  has_attached_file :image,
                  


        
4条回答
  •  悲哀的现实
    2021-02-20 15:58

    I wanted to avoid having to add a before_create callback to every model with an attachment. I had a look at the source and at the time of this writing it looked sth like:

    module Paperclip
      class Attachment
      ...
        def assign_file_information
          instance_write(:file_name, cleanup_filename(@file.original_filename))
          instance_write(:content_type, @file.content_type.to_s.strip)
          instance_write(:file_size, @file.size)
         end
    

    So you could just patch cleanup_filename.

    config/initializers/paperclip.rb

    module Paperclip
     class Attachment
       def cleanup_filename(filename)
         "HALLLO"
       end
      end
     end
    

提交回复
热议问题