I cant figure out how to update/rename a file uploaded/managed with Carrierwave-mongoid in rails 3.2.6. I want to rename the file in the db as well as on the filesystem.
Something like this maybe...
def rename( id , new_name )
f = UploadedFile.find(id)
if f.update_attributes({ f.file.original_filename: new_name }) # this is WRONG, what is right???
new_path = File.join( File.dirname( f.file.current_path ) , new_name ))
FileUtils.mv( f.file.current_path , new_path )
end
return f
end
Let me add this is after it has been uploaded already.
I used this rake task for reprocessing uploaded images after modifying version settings (filename and image size) in my uploader file:
Notes:
Based on @user892583, I worked on it and came up with a simpler solution:
The most efficient way to do this is to just move the existing S3 object (assuming your storage layer is S3):
This is using the
aws-sdk-s3
gem.I did this with this way:
I think this is only right way to rename file.
I was able to get the following working, although I'm sure there is a more elegant way. I'd appreciate any comments on the following
*add this to app/uploaders/file_uploader.rb
Thanks!