carrierwaveuploader / carrierwave

Classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks
https://github.com/carrierwaveuploader/carrierwave
8.78k stars 1.65k forks source link

Wrong Content-Type for Adobe Illustrator and Microsoft Office files #2704

Closed shunichi closed 7 months ago

shunichi commented 9 months ago

Some uploaded files have wrong content-type with carrierwave 3.0.3.

With carrierwave 3.0.3:

With carrierwave 2.2.4:

I know *.ai is a PDF with specific metadata and *.xlsx is a ZIP. But *.ai should be downloaded as Adobe Illustrator file.

I think this commit cause the issue. https://github.com/carrierwaveuploader/carrierwave/commit/a2ca59cbe67046ba7818c64849a9a4ffa90306db

It may be fixed if replcacing this line https://github.com/carrierwaveuploader/carrierwave/blob/8815592942f6c768d432dc6398a441f0c4f6118b/lib/carrierwave/sanitized_file.rb#L324 with:

Marcel::MimeType.for io, name: original_filename, declared_type: declared_content_type

ActiveStorage identifies files in the same way. https://github.com/rails/rails/blob/b5d63b9b16c6f251eaa08f61086f21ff3ef3292f/activestorage/app/models/active_storage/blob.rb#L353-L355

But I don't know it is a correct way to fix the issue. It slightly changes the behavior in guessed_safe_content_type case. (in a case where Marcel can't detect the type from the file content and declared_content_type is nil)

Juju-62q commented 9 months ago

I have similar problem for with *.csv files.

With carrierwave 3.0.3:

With carrierwave 2.2.4:

With https://github.com/carrierwaveuploader/carrierwave/pull/2705 branch, *.csv files are now identified as text/csv.

rajyan commented 8 months ago

@mshibuya Do you have some time looking into this issue? This might be a blocking issue for some people upgrading to 3.x.

mshibuya commented 7 months ago

Closed by #2705.