Skip to content

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

@shunichi

Description

@shunichi

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

With carrierwave 3.0.3:

  • Adobe Illustrator files (*.ai) are identified as application/pdf.
  • Some Excel files (*.xlsx) are identified as application/zip. (Some Excel files are identified correctly)

With carrierwave 2.2.4:

  • Adobe Illustrator files (*.ai) are identified as application/illustrator.
  • Excel files (*.xlsx) are identified as application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.

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.
a2ca59c

It may be fixed if replcacing this line

Marcel::Magic.by_magic(io).try(:type)

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions