linojon
linojon

Reputation: 1082

What method does Paperclip use to clean up file name?

Thoughtbot's Paperclip gem (v 3.2.1) will clean up filenames, replacing spaces and other special chars with underscores. I am reimporting data and need to check whether attachments have already been uploaded, but the original file name may not match the attached file name in Paperclip. What method does Paperclip use for the clean up?

Upvotes: 0

Views: 612

Answers (1)

linojon
linojon

Reputation: 1082

Found the answer digging into the source code. It's a private instance method Paperclip::Attachment#cleanup_filename . (Since this is a short lived (one off) import task I dont mind the (slight) risk of using an unpublished method.)

Thus my code looks something like this (Post has_many :attachments; Attachment has_attached_file :attached )

   if @post.attachments.present?
      cleaned_filename = @post.attachments.first.attached.send :cleanup_filename, filename 
      if @post.attachments.map(&:attached_file_name).include? cleaned_filename
        puts "already attached: #{filename}"
        return
      end
    end
    puts "attaching upload: #{filename}"

Upvotes: 1

Related Questions