Issue #8355: Adding webp as supported file format

Converting all uploaded images to the webp format.
This commit is contained in:
Thorsten Claus 2022-06-28 21:22:29 +02:00
parent 1b2270572b
commit 492ac74819
6 changed files with 36 additions and 13 deletions

View file

@ -59,8 +59,8 @@ Diaspora.PostPhotoUploader = class {
promptForName: true
},
validation: {
acceptFiles: "image/png, image/jpeg, image/gif",
allowedExtensions: ["jpg", "jpeg", "png", "gif"],
acceptFiles: "image/png, image/jpeg, image/gif, image/webp",
allowedExtensions: ["jpg", "jpeg", "png", "gif", "webp"],
sizeLimit: (window.Promise && qq.supportedFeatures.scaling ? null : this.sizeLimit)
},
messages: {

View file

@ -11,8 +11,8 @@ Diaspora.ProfilePhotoUploader.prototype = {
new qq.FineUploaderBasic({
element: document.getElementById("file-upload"),
validation: {
acceptFiles: "image/png, image/jpeg, image/gif",
allowedExtensions: ["jpg", "jpeg", "png"],
acceptFiles: "image/png, image/jpeg, image/gif, image/webp",
allowedExtensions: ["png", "jpg", "jpeg", "gif", "webp"],
sizeLimit: 4194304
},
request: {

View file

@ -12,7 +12,7 @@ class ProcessedImage < CarrierWave::Uploader::Base
end
def extension_allowlist
%w[jpg jpeg png gif]
%w[jpg jpeg png gif webp]
end
def filename

View file

@ -18,20 +18,34 @@ class UnprocessedImage < CarrierWave::Uploader::Base
end
def extension_allowlist
%w[jpg jpeg png gif]
%w[jpg jpeg png gif webp]
end
def filename
model.random_string + File.extname(@filename) if @filename
model.random_string + extension if @filename
end
def extension
needs_converting? ? ".webp" : File.extname(@filename)
end
def needs_converting?
extname = File.extname(@filename)
%w[.webp .gif].exclude?(extname)
end
process :basic_process
def basic_process
manipulate! do |img|
img.auto_orient
img.strip if strip_exif
img.combine_options do |i|
i.auto_orient
i.strip if strip_exif
end
img = yield(img) if block_given?
img.format("webp") if needs_converting?
img
end
end

BIN
spec/fixtures/autumn_1440x960.heic vendored Normal file

Binary file not shown.

View file

@ -17,12 +17,13 @@ describe Photo, :type => :model do
@aspect = @user.aspects.first
@fixture_filename = 'button.png'
@fixture_name = File.join(File.dirname(__FILE__), '..', 'fixtures', @fixture_filename)
@fail_fixture_name = File.join(File.dirname(__FILE__), '..', 'fixtures', 'msg.xml')
@photo = @user.build_post(:photo, :user_file => File.open(@fixture_name), :to => @aspect.id)
@photo2 = @user.build_post(:photo, :user_file => File.open(@fixture_name), :to => @aspect.id)
@saved_photo = @user.build_post(:photo, :user_file => File.open(@fixture_name), :to => @aspect.id)
@photo = @user.build_post(:photo, user_file: File.open(@fixture_name), to: @aspect.id)
@photo2 = @user.build_post(:photo, user_file: File.open(@fixture_name), to: @aspect.id)
@saved_photo = @user.build_post(:photo, user_file: File.open(@fixture_name), to: @aspect.id)
@saved_photo.save
end
@ -90,7 +91,7 @@ describe Photo, :type => :model do
@photo.update_remote_path
expect(@photo.remote_photo_path).to include("http")
expect(@photo.remote_photo_name).to include(".png")
expect(@photo.remote_photo_name).to include(".webp")
end
end
@ -182,7 +183,15 @@ describe Photo, :type => :model do
@photo.unprocessed_image.store! file
}.to raise_error CarrierWave::IntegrityError
end
end
describe "converting files" do
it "convert to webp" do
with_carrierwave_processing do
@photo.unprocessed_image.store! File.open(@fixture_name)
end
expect(@photo.remote_photo_name).to include(".webp")
end
end
describe "remote photos" do