GithubHelp home page GithubHelp logo

Comments (16)

gzigzigzeo avatar gzigzigzeo commented on June 28, 2024

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.

bndn avatar bndn commented on June 28, 2024

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.

gzigzigzeo avatar gzigzigzeo commented on June 28, 2024

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.

ndemoreau avatar ndemoreau commented on June 28, 2024

Hi, I try this but the problem is still there. ;-)

from carrierwave-meta.

manzhikov avatar manzhikov commented on June 28, 2024

Hi,
I have same problem.
Do you have any updates?

from carrierwave-meta.

ndemoreau avatar ndemoreau commented on June 28, 2024

No. I rolled back to Paperclip...

from carrierwave-meta.

manzhikov avatar manzhikov commented on June 28, 2024

I have solved. But it is monkey patch and works for me:

  1. 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
  1. 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
  1. And forked gem here:
    https://github.com/IldarManzhikov/carrierwave-meta

from carrierwave-meta.

clemens avatar clemens commented on June 28, 2024

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.

evie404 avatar evie404 commented on June 28, 2024

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.

geemus avatar geemus commented on June 28, 2024

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.

glebm avatar glebm commented on June 28, 2024

@clemens Thanks for the monkey-patch!

from carrierwave-meta.

gzigzigzeo avatar gzigzigzeo commented on June 28, 2024

Fixed, monkey-patch does not needed anymore.

from carrierwave-meta.

jpayne avatar jpayne commented on June 28, 2024

Still happening to me in 0.0.5.

from carrierwave-meta.

nfriend21 avatar nfriend21 commented on June 28, 2024

still happening to me too.

from carrierwave-meta.

torstenb avatar torstenb commented on June 28, 2024

Also just ran into this issue.

from carrierwave-meta.

elliot-nelson avatar elliot-nelson commented on June 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.