arjun
arjun

Reputation: 1614

Paperclip rename uploaded files by user

Is it possible to allow the user to rename the uploaded file?

If there is a share link, will it be automatically updated. I am not able to do this since i cant first figure out how to rename the file.

Upvotes: 3

Views: 2181

Answers (1)

S. A.
S. A.

Reputation: 3754

You can rename the files and then change the record file name. For instance, based on this answer, you can do:

(record.image.styles.keys+[:original]).each do |style|
    path = record.image.path(style)
    FileUtils.move(path, File.join(File.dirname(path), new_file_name))
end

record.image_file_name = new_file_name
record.save

If you're using Amazon S3, you can do:

AWS::S3::S3Object.move_to record.image.path(style), new_file_path, record.image.bucket_name

Check this out: Paperclip renaming files after they're saved

Upvotes: 8

Related Questions