Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow webp as image file format #2274

Merged
merged 2 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/alchemy/picture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions config/alchemy/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ uploader:
- gif
- png
- svg
- webp

# === Link Target Options
#
Expand Down
11 changes: 11 additions & 0 deletions lib/alchemy/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file added spec/fixtures/image5.webp
Binary file not shown.
36 changes: 35 additions & 1 deletion spec/models/alchemy/picture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down