camaleon-cms icon indicating copy to clipboard operation
camaleon-cms copied to clipboard

Troubles with SVGs on AWS (again)

Open brian-kephart opened this issue 6 years ago • 3 comments

Recap:

  • I discovered SVGs weren't being correctly resized by imagemagick (#787), causing SVG uploads to fail. This was fixed by converting to JPG when resizing (#788).
  • I then discovered that the fix was only valid locally, and AWS uploads were still failing. Fixed in #789.

Now, I find that even though files upload correctly, Amazon S3 does not supply the correct Content-Type header for SVG files. This makes them unusable in <img> tags.

There is an option to suppy a Content-Type when uploading to S3. I can make it work using two hooks, like so:

def on_uploader(args)
  args[:aws_settings][:aws_file_upload_settings] = lambda { |settings|
    @suppress_content_type ||= false
    settings[:content_type]  = params.dig("file_upload")&.content_type unless @suppress_content_type
    settings
  }
end

# This is needed to prevent using 'svg+xml' content type being applied to resized images, which are JPGs
def uploader_aws_before_upload(_args)
  @suppress_content_type = true unless _args[:key].match? /\.svgz?\z/
end

But that still leaves SVG use through AWS not working by default. Plus, if SVGs need a Content-Type applied when uploading to S3, it seems likely that some other file types will need one as well. I think this option should be passed by default.

Since my last two PRs about this haven't completely solved the problem, I'm filing this issue to get comments before attempting another one. Any thoughts on how this should be handled?

brian-kephart avatar Apr 04 '18 22:04 brian-kephart

Thanks, @brian-kephart , works like a charm - https://github.com/texpert/florsan/commit/924424ee1d23cc1ed6111efac5932c8f051eae1b

Sure this options should be passed by default!

texpert avatar Oct 17 '20 20:10 texpert

@texpert I'm not so sure. So far I've only had this problem with SVGs, but it could theoretically happen with other file types I haven't tried. (Probably not with images though, since the image formats used in browsers don't change much.) So we could end up with additional hacks for other file types if we put this in.

Then again, I'm probably worrying too much about hypotheticals, since you and I are the only ones seeing this issue after 2.5 years :)

brian-kephart avatar Oct 21 '20 07:10 brian-kephart

Yes, @brian-kephart , ideally the Content-Type should be always applied, both for the main files, and also for the thumbnails. But your solution will be good for now!

As for the future, it would be good to implement into Camaleon a 3rd party file attachment toolkit solution. Despite of of the Active Storage being a Rails component, I would prefer Shrine, as a more flexible, mature and lightweight solution.

texpert avatar Oct 21 '20 08:10 texpert