refinery / refinerycms

An extendable Ruby on Rails CMS that supports Rails 6.0+
https://www.refinerycms.com/
MIT License
3.9k stars 1.26k forks source link

Mime types bug in refinerycms-images and/or refinerycms-dragonfly? #3532

Open evenreven opened 11 months ago

evenreven commented 11 months ago

Context: it seems image/webp is not part of the standard hash of extensions to mime types in Dragonfly, and browsers read the content type as "octet-stream". Dragonfly.app.mime_types in the Rails console returns everything under the sun except image/webp. So whitelisting it in the refinery initializers doesn't work because Dragonfly won't find it to begin with (change the extension to almost anything else, and it will work).

I assume it's this issue that the array of additional mime type in the initializer (config/initializers/refinery/dragonfly.rb) is meant to solve?

But I can't find a way to register mime types that will actually be picked up by refinerycms-images. I've tried putting a Dragonfly compatible hash inside the refinery dragonfly initializers, and although it seems like it's picked up by the Refinery::Dragonfly module, it still doesn't work.

This is what I'm trying to do:

diff --git a/config/initializers/refinery/dragonfly.rb b/config/initializers/refinery/dragonfly.rb
index 0920e0ce..daec5121 100644
--- a/config/initializers/refinery/dragonfly.rb
+++ b/config/initializers/refinery/dragonfly.rb
@@ -21,7 +21,7 @@ Refinery::Dragonfly.configure do |config|
   # config.fetch_file_whitelist = nil
   # config.fetch_url_whitelist  = nil
   # config.generators           = []
-  # config.mime_types           = []
+  config.mime_types           =  [".webp" => "image/webp"]
   # config.name                 = "dragonfly"
   # config.path_prefix          = nil
   # config.plugin               = "libvips"
diff --git a/config/initializers/refinery/images.rb b/config/initializers/refinery/images.rb
index 241d56da..438fa266 100644
--- a/config/initializers/refinery/images.rb
+++ b/config/initializers/refinery/images.rb
@@ -18,7 +18,7 @@ Refinery::Images.configure do |config|
   # config.user_image_sizes = {:small=>"110x110>", :medium=>"225x255>", :large=>"450x450>"}

   # Configure white-listed mime types for validation
-  # config.whitelisted_mime_types = ["image/jpeg", "image/png", "image/gif", "image/tiff"]
+  config.whitelisted_mime_types = ["image/jpeg", "image/webp" "image/png", "image/gif", "image/tiff"]

   # Configure image view options
   # config.image_views = [:grid, :list]

Is this a bug, or am I holding it wrong?

evenreven commented 10 months ago

To rule out upstream issues, I tried using Dragonfly on a barebones Rails 7 app without Refinery now, and although it doesn't work out of the box (it says octet-stream), it's as easy as adding mime_type "webp", "image/webp" in the configure block of config/initializers/dragonfly.rb as suggested here.

It's not an apples to apples comparison (because it's a Rails 7 app), but still it makes me believe Dragonfly just doesn't see the mime_type Refinery tries to add.

Any help/pointers would be very welcome!