Cristi Cristi - 5 months ago 27
Ruby Question

carrierwave does not save content_type on Uploader

I have followed the instructions from https://github.com/carrierwaveuploader/carrierwave#setting-the-content-type

so I got:

require 'carrierwave/processing/mime_types'
class AssetFileUploader < CarrierWave::Uploader::Base
include CarrierWave::MimeTypes
process :set_content_type
...


but after a file upload the content_type is not updated.

I have a AssetInfo model with a uploader AssetFileUploader mounted on

:file attribute
class AssetInfo < ActiveRecord::Base
mount_uploader :file, AssetFileUploader
...


The params contain:

... "file"=># @headers="Content-Disposition: form-data; name=\"asset[asset_info_attributes][file]\"; filename=\"img.jpg\"\r\nContent-Type: image/jpeg\r\n"> ...


After the file upload I try in the console:


AssetInfo.last.file.file
=> < CarrierWave::SanitizedFile:0x34b1c @original_filename=nil @content_type=nil @file="/home/reea/projects/ruby/learning_cms/public/uploads/asset_info/file/20/img.jpg">

AssetInfo.last.file.file.content_type
=> nil

Answer

Carrierewave says they will deprecate this feature in the future and advise to use the Carrierwave::SanitizedFile instead

https://github.com/carrierwaveuploader/carrierwave/blob/v0.11.2/lib/carrierwave/processing/mime_types.rb

There's also an override method attached to this method

https://github.com/carrierwaveuploader/carrierwave/blob/v0.11.2/lib/carrierwave/processing/mime_types.rb#L56

Maybe that can give you direction.