Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where from does paperclip get the name of original file?

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.

like image 961
Pavel K. Avatar asked Apr 13 '10 15:04

Pavel K.


1 Answers

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.

like image 194
mckeed Avatar answered Nov 11 '22 16:11

mckeed