Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails - Paperclip file name

using rails with Paperclip, I can use the following to get the filename during a before_create:

extension = File.extname(photo_file_name).downcase

How do I get JUST the file name.. Right now I have photo_file_name which provides the entire file, titlename.pdf

i need just titlename without the .pdf

Thanks

Updating with code:

photo.rb:

  before_create :obfuscate_file_name

  #Paperclip for photo
  has_attached_file :photo,
......


private

  def obfuscate_file_name
    extension = File.extname(photo_file_name).downcase
    fileNameOnly = File.basename(photo_file_name).downcase
    self.photo.instance_write(:file_name, "#{fileNameOnly}_#{ActiveSupport::SecureRandom.hex(32)}#{extension}")
  end
like image 490
AnApprentice Avatar asked Nov 29 '10 18:11

AnApprentice


2 Answers

Use File.basename with the optional suffix argument like this:

file_name = File.basename(photo_file_name, File.extname(photo_file_name));

Works on my machine:

alt text

like image 106
Jacob Relkin Avatar answered Oct 07 '22 16:10

Jacob Relkin


Paperclip attachment has the 'original_filename' method for this.

like image 34
user3056122 Avatar answered Oct 07 '22 15:10

user3056122