-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Incorrect content type for files. #284
Comments
I'm assuming you mean when it's uploading to S3? There's an open issue related to this, actually: #254 I think you can make a new CarrierWave::SanitizedFile to set everything manually. See how that works here: https://github.com/jnicklas/carrierwave/blob/master/lib/carrierwave/uploader/cache.rb#L72 |
FWIW: In fog if you don't specify a content-type it uses the content-type gem to try and guess one. The guess is based entirely on file extension if I remember correctly. So if you have the proper file extensions it should all 'just work'. Barring that, explicitly passing one ought to work (soo also those other tickets though, sounds like there may be some related bugs). |
So if i install 'content_type' it will work? |
That part should already be done due to dependency resolution actually. If you change the filenames of the things you are uploading it should probably do the right thing (or at least raw fog ought to, if that value doesn't correctly make its way through carrierwave then we have other issues). |
Well, I don't have 'content_type' gem, in fact, it fails to build because I don't have libmagic (so, this extension determinate content-type based on "magic number"). The problem is — there is no .content_type method for File provided... |
Oh, sorry I was wrong. Sorry, I mis-spoke it is the 'mime-types' gem that does this. And fog requires it, so if you have fog you should also have mime-types. |
I have that gem, but content-type is wrong. I also use fog to upload my jekyll website, and all files has correct content-type. I forget to specified, that I'm using ruby 1.9.2p180. |
Please see my initial comment. I think you'll have to use CarrierWave::SanitizedFile to set the content_type. |
I actually don't get what you suggested ... |
I had this problem, so I put this in my PageUploader (i.e. the particular uploader I created):
Obviously, I'm thinking this would be a little better if the However, that aside, now I'm getting this error:
EDIT: The good news at least is that I inspected the request sent by Fog, and it does indeed include |
Woo, figured it out! It looks like my above solution actually does work, but for one small change. I needed to add
To make it:
And that did the trick. Now my content_types are uploading and getting set properly in my S3 bucket. |
I will try that. Thx. |
@JangoSteve - thanks for the suggestion, but unfortunately that method stops working as soon as you have something like this in in your uploader: process :convert => 'png' This is due to the fact that the temp file retains to original file name including the original extension, which the mime-type gem uses to determine the content type. Even if you have a filename() method that renames the file to |
@chrisbloom7, I'm confused. If you know you're converting the file to a png, why not just set the
|
That is what I ended up doing. I was just pointing out that you can't do what you suggested in cases like that, just in case anyone else should try it and experience the same problem I did. |
Hrm... perhaps worthwhile adding something to the wiki? Or maybe it's possible to fix in Carrierwave itself...? |
I could probably submit a patch to carrierwave to make this much easier, if you'd like. |
If anyone is interested, I've submitted a pull request which actually adds |
Hey guys. This problem's biting me. Is there a better solution than what @JangoSteve suggested above on May 15? I see that a couple of commits were made to try to fix it, but at least one (#254) was reverted. |
After stumbling upon lib/carrierwave/processing/mime_types.rb , I figured out how to get the correct content-type set on files and their versions:
My question now is: Why is this not done by default? |
I meant to write up a quick post on the new The reason it's not done by default, and why I built it as a processor, is because it requires a dependency on the mime-types gem in order to provide more in-depth file-type guessing than most people who use carrierwave probably need. The goal is to keep carrierwave small, modular, and having as few required dependencies as possible. |
@JangoSteve Ah, that makes sense re: minimizing CarrierWave's dependencies. I've just submitted pull request #440, which adds a section to the README that explains how to use CarrierWave::MimeTypes to set more-specific content-type values. |
I cannot get this to work using the latest carrierwave-mongoid. I've set the set_content_type directive, the Content-Type will always be set to the original file rather than the converted format. |
@fbjork is this a problem with carriewwave or carrierwave-mongoid? Can you make a failing test case and open an issue where appropriate? Thanks! |
@nickhoffman to answer your question, it's because this is a giant hairball with a broken model of one output file plus a bunch of "versions" for each input upload. Now of course you want to upload them all to e.g. s3 with the correct types for each version, and that just doesn't fit well in this model.... to say nothing of the remarkable! coding techniques used. You might find that simply using rack, tempfile, rmagick and fog directly is a much more expedient and less painful way to go. |
I put "process :set_content_type" at the top of the uploader and while it fixes the problem for the main image, it does not solve the problem for the different versions. Here is my code: Process files as they are uploaded:process :convert => 'png' version :product do version :storefront do |
Like bvishny, I can't make it works for version with different types. version :preprocessed do |
Every time when I'm trying to upload a file from irb, file has content-type 'binary/octet-stream'. Is there any possible way to force change of content-type to correct one before saving the file?
The text was updated successfully, but these errors were encountered: