Comments (16)
This method is called because File instance of the retreived file in some cases contains wrong #content_type property value, so it had to be reassigned with #set_content_type call. Caching of content-type could be done only if uploader is attached to a model. #set_content_type uses the Mime::Type gem. This gem locates content type by file's extension. #original_filename is used to get original file's extension. I think S3 have other way to identify file's content-type even #original_filename is missing or blank. But, in general, it may be CarrierWave's fail.
Could you provide the failing spec?
from carrierwave-meta.
I didn't write tests. The few lines I run in the rails console are :
> file = File.open "file.png"
> f = Fichier.new :fichier => file
> f.save
> f.fichier.url
NoMethodError: undefined method `original_filename' for #<CarrierWave::Storage::Fog::File:0xab4134c>
from /var/lib/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/processing/mime_types.rb:46:in `set_content_type'
from /var/lib/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/callbacks.rb:18:in `block in with_callbacks'
from /var/lib/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/callbacks.rb:18:in `each'
from /var/lib/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/callbacks.rb:18:in `with_callbacks'
from /var/lib/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/store.rb:96:in `retrieve_from_store!'
from /var/lib/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/versions.rb:232:in `block in retrieve_versions_from_store!'
from /var/lib/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/versions.rb:232:in `each'
from /var/lib/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/versions.rb:232:in `retrieve_versions_from_store!'
from /var/lib/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/callbacks.rb:18:in `block in with_callbacks'
from /var/lib/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/callbacks.rb:18:in `each'
from /var/lib/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/callbacks.rb:18:in `with_callbacks'
from /var/lib/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/store.rb:96:in `retrieve_from_store!'
from /var/lib/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/mount.rb:311:in `uploader'
from /var/lib/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/mount.rb:169:in `fichier'
from /var/lib/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/mount.rb:157:in `fichier'
from (irb):5
from /var/lib/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:45:in `start'
from /var/lib/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:8:in `start'
from /var/lib/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
The weird behavior I noticed is that if I run the same line another time, I have the good result without error :
> f.fichier.url
=> "https://my-bucket.s3.amazonaws.com/uploads/file.png"
I see a method content_type
in the S3::File module which use the HTTP header to define the file's content-type. So can I use this to solve my problem ? and how ?
Thank you :)
from carrierwave-meta.
At carrierwave/lib/carrierwave/processing/mime_types.rb:46 you can see this lines of code:
if override || file.content_type.blank? || file.content_type == 'application/octet-stream'
new_content_type = ::MIME::Types.type_for(file.original_filename).first.to_s
I call set_content_type with override set to true. It could be that if S3 storage is used there is no need to override retreived content type. I do not really remember why did it called with override flag. You could make a fork, change line 8 of lib/carrierwave-meta/meta.rb from set_content_type(true) to set_content_type without args and run specs to see that everything is okay with Filesystem storage.
I think, it may solve your problem. I can try this thing for you and write specs for S3 storage at the weekend. You may try this thing yourself, or wait if you can.
from carrierwave-meta.
Hi, I try this but the problem is still there. ;-)
from carrierwave-meta.
Hi,
I have same problem.
Do you have any updates?
from carrierwave-meta.
No. I rolled back to Paperclip...
from carrierwave-meta.
I have solved. But it is monkey patch and works for me:
- Migration:
class AddContentTypeAndFileSizeToAvatars < ActiveRecord::Migration
def change
add_column :avatars, :content_type, :string, default: '', null: false
add_column :avatars, :file_size, :string, default: '0', null: false
end
end
- Added callback to model:
before_save :update_image_attributes
private
def update_image_attributes
if image.present? && image_changed?
self.content_type = image.file.content_type
self.file_size = image.file.size
end
end
- And forked gem here:
https://github.com/IldarManzhikov/carrierwave-meta
from carrierwave-meta.
I've also just run into this issue. The simplest fix could probably be done in the CarrierWave gem itself by adding an original_filename
method to Fog::File
:
def original_filename
::File.basename(path)
end
from carrierwave-meta.
I just ran into this, and the way I solved this was by skipping the content type step, since my app was only needed width and height information.
My fork is here in case anyone is interested:
https://github.com/rickypai/carrierwave-meta
from carrierwave-meta.
I'm not familiar enough with this gem to comment too specifically, but the issue appears to be that the content_type check is happening on the wrong object. Carrierwave internally expects that you would be doing this on a CarrierWave::SanitizedFile
object, not a CarrierWave::Storage::Fog::File
. SanitizedFile defines the method you want/need so I think that is the right fix here. I wonder if perhaps the delegator stuff is what is causing it to refer to the wrong object type? Hope that helps.
from carrierwave-meta.
@clemens Thanks for the monkey-patch!
from carrierwave-meta.
Fixed, monkey-patch does not needed anymore.
from carrierwave-meta.
Still happening to me in 0.0.5.
from carrierwave-meta.
still happening to me too.
from carrierwave-meta.
Also just ran into this issue.
from carrierwave-meta.
Also experiencing this issue running fog 1.28.0, carrierwave 0.10.0, and carrierwave-meta 0.0.5.
It looks like this round of the issue is actually in carrierwave -- pulling from the master branch fixes the issue. I'm sure this will come out as the stable tagged version in the near future, but in the meantime, just edit your Gemfile as follows:
gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'
from carrierwave-meta.
Related Issues (10)
- image_size doesn't come back as array HOT 1
- recreate_versions! does not update meta HOT 1
- License missing from gemspec HOT 3
- Doesn't work with Cloudinary HOT 2
- Fallback meta to original image if you don't have one of the processed ones.
- Clarity: What should happen when I fetch a record from the DB? HOT 1
- Can it only save the meta in one single column just like paperclip-meta HOT 6
- Jcrop example needs some updating HOT 1
- Should Use "Type" By Default HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from carrierwave-meta.