Carrierwave: Saving Original Filename not working

1.7k views Asked by At

I am using the latest Carrierwave (master branch) in Rails 4.2.1. I am needing to save the original filename (before sanitization) of the uploaded file. I found a section in Carrierwave Wiki about how to do it (https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Create-random-and-unique-filenames-for-all-versioned-files#saving-the-original-filename). The relevant piece of code that goes in the uploader is this (according to the wiki entry):

# in `class PhotoUploader`
before :cache, :save_original_filename
def save_original_filename(file)
  model.original_filename ||= file.original_filename if file.respond_to?(:original_filename)
end

But it's not working for me. I have a column called 'original_filename' in my database table. And the filename is being saved in that column, but its not original filename, it's actually sanitized filename.

Any idea where to hook this method in order to save original filename?

Thanks.

1

There are 1 answers

0
sarojkh On

Apparently, there are a lot of people, including myself, who have come across this issue. For instance, this issue (https://github.com/carrierwaveuploader/carrierwave/issues/1835) has an elaboration to why this doesn't work as expected.

A workaround I have come across is explicitly setting the original_filename using the file instance in incoming parameter.

Something like the following.

<Model>.create({file: params[:file], original_filename: params[:file]&.original_filename]})