Chaos
Chaos

Reputation: 11

Content Type not being set Carrierwave S3

I'm having an issue with setting content_type when I'm trying to save a file using remote_file_url (I think that's the culprit at least, that's the only thing that differs from uploading via frontend).

I realized that previews/embeds for some pdf files don't work (browser instead tries to download them), when I've checked the broken files I've realized they have content_type set as binary/octet-stream.

I tried to force the app to set the correct one using content_type (and later target_filetype for a good measure), and while the object in DB has proper content_type, it's still being saved as binary/octet in S3.

Got following code for saving the file:

      params = {
        filetype: 'draft',
        original_filename: @filename
      }

      params.merge!(remote_file_url: file)
      params.merge!(content_type: 'application/pdf') if type == :pdf
      params.merge!(target_filetype: 'application/pdf') if type == :pdf

      attachment = order.attachments.new(params)

      attachment.save!

If I download said PDF and upload it via frontend however, its content_type is being set correctly as pdf and the preview works again.

I've confirmed that S3 stores the original file as binary and that it serves it as such, thus no embedding.

I'm using following storage gems:

gem 'aws-sdk-s3', '~> 1.119'
gem 'carrierwave', '~> 2.2', '>= 2.2.3'
gem 'copy_carrierwave_file', '~> 1.3'
gem 'file_validators', '~> 3.0'
gem 'fog-aws', '~> 3.17'

when I probe the object directly from the console:

[9] pry(#<xxx::SaveFile>)> attachment.file.content_type
=> "binary/octet-stream"
[10] pry(#<xxx::SaveFile>)> attachment.content_type
=> "application/pdf"

I've also checked the frontend upload controller and uploader, can't see anything about changing/setting content_type in there..

And also tried adding Mime::Type.register 'application/pdf', :pdf into mime_types.rb and restarting, but to no avail.

I've seen similar looking issues, but they are about old carrierwave versions, which required setting the content_type manually, which is no longer a thing since 0.11 according to wiki

I'm missing something, but I have no clue what, would really appreciate some help, thanks!

EDIT

I guess I found a solution, but it's extremely limited, had to ask devs of the service which was generating the PDFs to set the right content-type on their side, but it still seems silly that I was unable to force my own code to set it right, thus I still wonder if there's anything I can do on my side, or maybe it's just some bug?

Upvotes: 1

Views: 127

Answers (0)

Related Questions