Skip to content

Commit

Permalink
Issue diaspora#8355: Adding webp as supported file format
Browse files Browse the repository at this point in the history
Converting all uploaded images to the webp format. Adding heic to the list of supported file formats for post images
  • Loading branch information
tclaus committed Jun 28, 2022
1 parent 792d034 commit 020cf19
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/assets/javascripts/helpers/post_photo_uploader.es6
Expand Up @@ -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/heic, image/webp",
allowedExtensions: ["jpg", "jpeg", "png", "gif", "heic", "webp"],
sizeLimit: (window.Promise && qq.supportedFeatures.scaling ? null : this.sizeLimit)
},
messages: {
Expand Down
Expand Up @@ -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/heic, image/webp",
allowedExtensions: ["png", "jpg", "jpeg", "gif", "heic", "webp"],
sizeLimit: 4194304
},
request: {
Expand Down
2 changes: 1 addition & 1 deletion app/uploaders/processed_image.rb
Expand Up @@ -12,7 +12,7 @@ def store_dir
end

def extension_allowlist
%w[jpg jpeg png gif]
%w[jpg jpeg png gif heic webp]
end

def filename
Expand Down
27 changes: 23 additions & 4 deletions app/uploaders/unprocessed_image.rb
Expand Up @@ -18,24 +18,43 @@ def store_dir
end

def extension_allowlist
%w[jpg jpeg png gif]
%w[jpg jpeg png gif heic 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)
!extname.eql?(".webp")
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?

convert_to_jpeg(img) if needs_converting?
img
end
end

# @param [ImageProcessing::Builder] img
def convert_to_jpeg(img)
img.format("webp")
end

version :thumb_small
version :thumb_medium
version :thumb_large
Expand Down
Binary file added spec/fixtures/autumn_1440x960.heic
Binary file not shown.
18 changes: 15 additions & 3 deletions spec/models/photo_spec.rb
Expand Up @@ -17,12 +17,16 @@ def with_carrierwave_processing(&block)
@aspect = @user.aspects.first

@fixture_filename = 'button.png'
@heic_filename = "autumn_1440x960.heic"

@fixture_name = File.join(File.dirname(__FILE__), '..', 'fixtures', @fixture_filename)
@heic_file_name = File.join(File.dirname(__FILE__), "..", "fixtures", @heic_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)
@heic_photo = @user.build_post(:photo, user_file: File.open(@heic_file_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

Expand Down Expand Up @@ -182,7 +186,15 @@ def image_from(photo)
@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
@heic_photo.unprocessed_image.store! File.open(@heic_file_name)
end
expect(@heic_photo.remote_photo_name).to include(".webp")
end
end

describe "remote photos" do
Expand Down

0 comments on commit 020cf19

Please sign in to comment.