Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CarrierWave sets wrong content type on XLSX files #1995

Closed
resistorsoftware opened this issue Aug 5, 2016 · 15 comments
Closed

CarrierWave sets wrong content type on XLSX files #1995

resistorsoftware opened this issue Aug 5, 2016 · 15 comments

Comments

@resistorsoftware
Copy link

My incoming upload has the right content type. A before store action shows the right content type. But the actual stored file on S3 has the wrong content type!! How to correct that?

incoming file:
2016-08-05T15:02:16.358472+00:00 app[web.1]: new_rates.xlsx
2016-08-05T15:02:16.358473+00:00 app[web.1]: file
2016-08-05T15:02:16.358474+00:00 app[web.1]: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

2016-08-05T15:02:16.401995+00:00 app[web.1]: BEFORE STORE: {:filename=>"new_rates.xlsx", :type=>"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :name=>"file", :tempfile=>#<Tempfile:/tmp/RackMultipart20160805-6-cdv09y.xlsx>, :head=>"Content-Disposition: form-data; name=\"file\"; filename=\"new_rates.xlsx\"\r\nContent-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n"}
2016-08-05T15:02:16.707430+00:00 app[web.1]: Uploaded file content type: application/zip
2016-08-05T15:02:16.707446+00:00 app[web.1]: File Type is:application/zip
@bemosior
Copy link

@resistorsoftware here's an extremely terrible hack I'm using in the meantime:

class AttachmentUploader < CarrierWave::Uploader::Base

# ...

  process :override_content_type

  def override_content_type
    if File.extname(file.file).delete('.').to_sym == :xlsx
      file.content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    end
  end

#...

end

@bemosior
Copy link

If I'm following everything correctly...

The documentation here states:

As of v0.11.0, the mime-types gem is a runtime dependency and the content type is set automatically. You no longer need to do this manually

The mime-types gem appears to be: https://github.com/mime-types/ruby-mime-types

And it's seems these sorts of files are not accommodated. I could not find XLSX or similar files in the supported types, so I believe this to be an issue in that upstream dependency.

@bemosior
Copy link

I became hopeful when I read the Contributing documentation:

The mime-types registry is no longer contained in mime-types, but in mime-types-data. Please see that project for contributions there.

The latest versions of mime-types-data appear to support XLSX and others, so I'm not sure what's happening here.

@bemosior
Copy link

bemosior commented Aug 22, 2016

I forked one of @mehlah's content-type test scripts, hacked in a quick content-type check on an XLSX doc, and it correctly detected application/vnd.openxmlformats-officedocument.spreadsheetml.sheet: https://gist.github.com/bemosior/d341b2c4423f7b8347b0267387e15eb1

@bemosior
Copy link

@resistorsoftware: In my case, I've determined that there's a disconnect between the content-type sent by our front-end and that actually delivered to Carrierwave. It's definitely an issue somewhere within our own code. I suggest getting a handle on all the code layers in your control between upload in the browser and the Carrierwave library and verifying the content-type perceived in each.

@bemosior
Copy link

The resolution to my issue ended up looking like this:

def attachment
  #Added this line to resolve what turned out to be an attribute mapping issue:
  file[:content_type] = file[:type]

  @attachment ||= Attachment.new(file: file)
end

Hope this helps.

@resistorsoftware
Copy link
Author

Thanks so much for the explanations. I validated using Pry that the gem should've been able to detect the proper content type using the extension as is, which troubled me. Why was the content type mangled? So at that point I made a hack on the result, and used a really dumb update on the S3 file to force its content type to be correct.

I will follow through this thread and see if I can get CarrierWave to work thus eliminating the crazy S3 hack!

Thanks again!

@arseny-emchik
Copy link

The same problems for me

@p8
Copy link
Contributor

p8 commented Nov 24, 2016

Mimemagic comes with extra magic you can overlay on top of the defaults to correctly detect these file types. Enable it like this:

    require 'mimemagic/overlay'
    MimeMagic.by_magic(File.open('test.xlsx'))

See: https://github.com/minad/mimemagic#extra-magic-overlay

@mshibuya
Copy link
Member

#1934 has made MimeMagic as default way to detect MIME types, so this should be no longer an issue.

@andyrue
Copy link

andyrue commented Oct 14, 2020

I recently upgraded from 1.3.1 to the latest 2.1.0 and I am now experiencing this same problem with XLSX and DOCX files. The uploader detects things properly, but after saving the file the content_type is application/zip. Rolling back to 1.3.1 fixes it.

@nathancolgate
Copy link

We experienced the same issue and fix as @andyrue on a Rails 5.2 app.

@tmr08c
Copy link

tmr08c commented Mar 16, 2021

It looks like this may have been fixed again in #2447 which was included in the 2.2.0 release 🎉

@phansch
Copy link

phansch commented Oct 5, 2021

We are running into a similar problem with .apk files, which should be application/vnd.android.package-archive but are interpreted as application/zip. Is there a way to override this from our side, or does that require a fix here?

@phansch
Copy link

phansch commented Oct 5, 2021

Currently using this temporary workaround for setting the content type of the file manually:

class SomeUploader < CarrierWave::Uploader::Base
  process :apk_content_type

  def apk_content_type
    if current_path.end_with?('.apk')
      file.content_type = 'application/vnd.android.package-archive'
    end
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants