Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 282 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rails - Paperclip file name

#1
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


Reply

#2
Another option is set to default, work for all upload.

This example change name file to 'name default' for web, example: `test áé.jpg` to `test_ae_www.foo.com.jpg`

**helper/application_helper.rb**

def sanitize_filename(filename)
fn = filename.split /(?<=.)\.(?=[^.])(?!.*\.[^.])/m
fn[0] = fn[0].parameterize
return fn.join '.'
end


Create **config/initializers/paperclip_defaults.rb**

include ApplicationHelper

Paperclip::Attachment.default_options.update({
:path => ":rails_root/public/system/:class/:attachment/:id/:style/:parameterize_file_name",
:url => "/system/:class/:attachment/:id/:style/:parameterize_file_name",
})

Paperclip.interpolates :parameterize_file_name do |attachment, style|
"#{sanitize_filename(attachment.original_filename)}_www.foo.com"
end

Need restart, after put this code

I hope it help! ;)
Reply

#3
user.logo.original_filename
=> 'test.jpg'
Reply

#4
Paperclip attachment has the 'original_filename' method for this.
Reply

#5
Use [`File.basename`][1] 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][2]



[1]:

[To see links please register here]

[2]:
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through