From b6d22a855243dfb3f6e834ef60eb83a47f31507b Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 28 Mar 2022 14:44:22 +0200 Subject: [PATCH 1/2] Add webp as allowed image format --- app/models/alchemy/picture.rb | 2 +- config/alchemy/config.yml | 1 + spec/fixtures/image5.webp | Bin 0 -> 6134 bytes spec/models/alchemy/picture_spec.rb | 36 +++++++++++++++++++++++++++- 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/image5.webp diff --git a/app/models/alchemy/picture.rb b/app/models/alchemy/picture.rb index c45a40c5d0..da7dc7adf3 100644 --- a/app/models/alchemy/picture.rb +++ b/app/models/alchemy/picture.rb @@ -28,7 +28,7 @@ class Picture < BaseRecord large: "240x180", }.with_indifferent_access.freeze - CONVERTIBLE_FILE_FORMATS = %w(gif jpg jpeg png).freeze + CONVERTIBLE_FILE_FORMATS = %w[gif jpg jpeg png webp].freeze TRANSFORMATION_OPTIONS = [ :crop, diff --git a/config/alchemy/config.yml b/config/alchemy/config.yml index 001f7a91d0..298e7156ae 100644 --- a/config/alchemy/config.yml +++ b/config/alchemy/config.yml @@ -173,6 +173,7 @@ uploader: - gif - png - svg + - webp # === Link Target Options # diff --git a/spec/fixtures/image5.webp b/spec/fixtures/image5.webp new file mode 100644 index 0000000000000000000000000000000000000000..aaaad415db9a76a085b6df312fac9eeffc2c0f45 GIT binary patch literal 6134 zcmYLtWl$W7((Eqo?tXB05AN;`0fG};LU0WlG`PFFyXyi077rd|LvVMNk8|&R^`@$; zySjS%|BRNRoZJ~90H7}`rJ<)GXn+C$0Fd4>4gjzK0q+jg4FLe)>J7*RGEV?lfnrG# z*~)aqMXV(}vB-%y$kxs~0{{zJi<*(U^0zgg%ll*r4zb$4P4P?6xdsHYT#V&TYJ-Q^ zQxM0`XRB~K;f?c6GULNp|MHu8Z;w?_F(zWh=y%ANa$1k8yV8IVT)}_!pdKaEI(*kvhIdjUh@JKE2z`$CcYcZ ziK0`9xi$_tuese?t=d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>&fYghIlxVLCqHk*cVmYE;>H(z1PVIfG!0{t z$mv#n%aJ^9+(*7DE8*E|x;(9!qt?`0E)E0Hk_%d7r+x1qy`C4+2Bml{C}wXmszc?@ zuDz|bqg1|0UQY9_Ax&V-ncNQ>GE0PG`n18g351I!Bi`o%6LNdTt7rOvB(FY`rr6oe z3Cl{3b#&Bzu~3>WPw|a!gvH{nbh!JuP{A@;dq`oNf$()^uQW$ibK*idx8uf1ija=S zL0JaQ{T%d6HeNtd*RW9mco5x(j&XVBoD;4+B-!04u;rodqM3Q_nQ0JDRg*Zffp=Hy zj&rL&!+JSlFK5L9FFg>_tyFIsO&uh~s6zpWYqd1+l0~9!$i&{QS`rYI`&_OZ#2#vAOZQ+#4wfJKA{ zh$8@SfUp4IyYnwdfbjoey?0IeKW+N18UL4odDplw|37aZ3HP78&Ab2a5WnN!p8v;) z06-!l0Pfx44FG^C_;35fdj$4> dn)ToM_mR|=R#bdv4SP$EFHTg7GQw0`{|5rSsd@ka literal 0 HcmV?d00001 diff --git a/spec/models/alchemy/picture_spec.rb b/spec/models/alchemy/picture_spec.rb index e1c02e07b1..50b3be5072 100644 --- a/spec/models/alchemy/picture_spec.rb +++ b/spec/models/alchemy/picture_spec.rb @@ -34,6 +34,18 @@ module Alchemy end end + context "with a webp file" do + let :image_file do + File.new(File.expand_path("../../fixtures/image5.webp", __dir__)) + end + + it "generates thumbnails after create" do + expect { + create(:alchemy_picture) + }.to change { Alchemy::PictureThumb.count }.by(3) + end + end + it "is valid with valid attributes" do picture = Picture.new(image_file: image_file) expect(picture).to be_valid @@ -385,7 +397,7 @@ module Alchemy end end - context "when `image_output_format` is configured to an image format" do + context "when `image_output_format` is configured to jpg" do before do stub_alchemy_config(:image_output_format, "jpg") end @@ -406,6 +418,28 @@ module Alchemy end end end + + context "when `image_output_format` is configured to webp" do + before do + stub_alchemy_config(:image_output_format, "webp") + end + + context "and the format is a convertible format" do + it "returns the configured file format." do + is_expected.to eq("webp") + end + end + + context "but the format is not a convertible format" do + before do + allow(picture).to receive(:image_file_format) { "svg" } + end + + it "returns the original file format." do + is_expected.to eq("svg") + end + end + end end describe "after update" do From 25cc942982d05171e1ae0990fcdf18e98b30ebfa Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 28 Mar 2022 14:48:09 +0200 Subject: [PATCH 2/2] Register webp as mime type Rails 6.1.5 does not have a image/webp mime type registered. --- lib/alchemy/engine.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/alchemy/engine.rb b/lib/alchemy/engine.rb index 73aae979f0..c3ec702565 100644 --- a/lib/alchemy/engine.rb +++ b/lib/alchemy/engine.rb @@ -45,5 +45,16 @@ class Engine < Rails::Engine end end end + + initializer "alchemy.webp-mime_type" do + # Rails does not know anything about webp even in 2022 + unless Mime::Type.lookup_by_extension(:webp) + Mime::Type.register("image/webp", :webp) + end + # Dragonfly uses Rack to read the mime type and guess what + unless Rack::Mime::MIME_TYPES[".webp"] + Rack::Mime::MIME_TYPES[".webp"] = "image/webp" + end + end end end