views:

77

answers:

1

i started using nginx upload module (which creates upload files like /tmp/000121545) but i need paperclip to use original filename while saving files (like /public/avatars/LuckyLuke.jpg)

previously in the parameters Rails were passing just

 "avatar"=>#<File:/tmp/RackMultipart20100413-6151-t3ecq0-0> 

no original filename as well, so i am wondering where from does it come in paperclip? i tried looking through plugin code but it's currently a bit too complex for me.

+3  A: 

The browser sends a http header with the file name. ("Content-Disposition: filename=original_file.jpg")

Rails makes this available as a instance method of the temp file object: params[:avatar].original_filename, and paperclip uses that.

In detail, Rack parses the multipart form in Rack::Utils::Multipart::UploadedFile and puts a hash in the parameters that includes :tempfile and :filename. Then ActionDispatch::Http::Upload comes along and replaces that hash by the File object (value of :tempfile), extending it with the module ActionDispatch::Http::UploadedFile, which adds a instance variable for original_path and the method original_filename.

mckeed
Yap, it's Content-Disposition: form-data; name="files"; filename="file1.txt"From: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2
Vlad Zloteanu
hm, i believe it is so, but cannot find any trace of "Content-Disposition" in the paperclip code while grepping.
Pavel K.
Ah, sorry. I see what you're asking now. I'll edit the answer to include how rails gets the filename.
mckeed