Paperclip how to change basename (filename)?
Asked Answered
W

4

11

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

In my model I have:

  attr_accessor :image_url, :basename

  has_attached_file :image,
          :styles => { :original => ["300x250>", :png], :small => ["165x138>", :png] },
          :url  => "/images/lille/:style/:id/:basename.:extension",
          :path => ":rails_root/public/images/lille/:style/:id/:basename.:extension"
before_save :basename
private

  def basename
  self.basename = "HALLLO"
  end

But the filename is not changed at all.

Weir answered 7/3, 2012 at 18:7 Comment(0)
N
6

If you are assigning the file directly you can do this:

photo.image = the_file
photo.image.instance_write(:file_name, "the_desired_filename.png")
photo.save
Navicert answered 9/2, 2017 at 22:38 Comment(0)
G
3

Im doing this to strip whitespaces:

before_post_process :transliterate_file_name

private
def transliterate_file_name
  self.instance_variable_get(:@_paperclip_attachments).keys.each do |attachment|
    attachment_file_name = (attachment.to_s + '_file_name').to_sym
    if self.send(attachment_file_name)
      self.send(attachment).instance_write(:file_name, self.send(attachment_file_name).gsub(/ /,'_'))
    end
  end
end

I hope this will help you.

edit:

In your example:

def basename
  self.image_file_name = "foobar"
end

Should do the job. (but might rename the method ;) )

Gherkin answered 7/3, 2012 at 18:36 Comment(0)
M
3

Paperclip now allows you to pass in a FilenameCleaner object when setting up has_attached_file.

Your FilenameCleaner object must respond to call with filename as the only parameter. The default FilenameCleaner removes invalid characters if restricted_characters option is supplied when setting up has_attached_file.

So it'll look something like:

has_attached_file :image,
                  filename_cleaner: MyRandomFilenameCleaner.new
                  styles: { thumbnail: '100x100' }

And MyRandomFilenameCleaner will be:

class MyRandomFilenameCleaner
  def call(filename)
    extension = File.extname(filename).downcase
    "#{Digest::SHA1.hexdigest(filename + Time.current.to_s).slice(0..10)}#{extension}"
  end
end

You could get away with passing in a class that has a self.call method rather than an object but this conforms to Paperclip's documentation in Attachment.rb.

Misfortune answered 7/7, 2016 at 15:12 Comment(0)
W
1

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
Wigwag answered 2/10, 2015 at 13:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.