diff options
author | Benjamin Neff <benjamin@coding4coffee.ch> | 2022-07-16 05:46:18 +0300 |
---|---|---|
committer | Benjamin Neff <benjamin@coding4coffee.ch> | 2022-07-16 05:48:03 +0300 |
commit | d0af34c0790e13eef187813d12f35ffe6f11b373 (patch) | |
tree | c91b1598053f8692aaea893ba7b3b36215ede80b | |
parent | 5669ba6b48f2488e394789bc6fb8504358e966b7 (diff) | |
parent | f995e6af0d02e9c085c2237f44ec36234ac0ed6e (diff) |
Merge pull request #8358 from tclaus/supporting_heic_images
Using webp as storage format for images
-rw-r--r-- | Changelog.md | 1 | ||||
-rw-r--r-- | app/assets/javascripts/helpers/post_photo_uploader.es6 | 4 | ||||
-rw-r--r-- | app/assets/javascripts/mobile/mobile_profile_photo_uploader.js | 4 | ||||
-rw-r--r-- | app/models/photo.rb | 2 | ||||
-rw-r--r-- | app/services/import_service.rb | 1 | ||||
-rw-r--r-- | app/uploaders/processed_image.rb | 2 | ||||
-rw-r--r-- | app/uploaders/unprocessed_image.rb | 22 | ||||
-rw-r--r-- | spec/fixtures/autumn_1440x960.heic | bin | 0 -> 293608 bytes | |||
-rw-r--r-- | spec/models/photo_spec.rb | 17 |
9 files changed, 39 insertions, 14 deletions
diff --git a/Changelog.md b/Changelog.md index 0681d69f3..423960738 100644 --- a/Changelog.md +++ b/Changelog.md @@ -36,6 +36,7 @@ Although the chat was never enabled per default and was marked as experimental, * For pods running PostgreSQL, make sure that no upper-case/mixed-case tags exist, and create a `lower(name)` index on tags to speed up ActsAsTaggableOn [#8206](https://github.com/diaspora/diaspora/pull/8206) * Allow podmins/moderators to see all local public posts to improve moderation [#8232](https://github.com/diaspora/diaspora/pull/8232) [#8320](https://github.com/diaspora/diaspora/pull/8320) * Add support for directly paste images to upload them [#8237](https://github.com/diaspora/diaspora/pull/8237) +* Add support for webp images and convert new png/jpg to webp to save space and bandwidth [#8358](https://github.com/diaspora/diaspora/pull/8358) # 0.7.18.0 diff --git a/app/assets/javascripts/helpers/post_photo_uploader.es6 b/app/assets/javascripts/helpers/post_photo_uploader.es6 index ba94ec5ed..8f1f5695b 100644 --- a/app/assets/javascripts/helpers/post_photo_uploader.es6 +++ b/app/assets/javascripts/helpers/post_photo_uploader.es6 @@ -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: { diff --git a/app/assets/javascripts/mobile/mobile_profile_photo_uploader.js b/app/assets/javascripts/mobile/mobile_profile_photo_uploader.js index cc70ba1c1..f3a5a9c08 100644 --- a/app/assets/javascripts/mobile/mobile_profile_photo_uploader.js +++ b/app/assets/javascripts/mobile/mobile_profile_photo_uploader.js @@ -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: { diff --git a/app/models/photo.rb b/app/models/photo.rb index 0a77df62a..e9fbe5d20 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -38,6 +38,7 @@ class Photo < ApplicationRecord mount_uploader :processed_image, ProcessedImage mount_uploader :unprocessed_image, UnprocessedImage + attr_accessor :keep_original_format belongs_to :status_message, foreign_key: :status_message_guid, primary_key: :guid, optional: true validates_associated :status_message @@ -50,7 +51,6 @@ class Photo < ApplicationRecord after_commit on: :create do queue_processing_job if author.local? - end scope :on_statuses, ->(post_guids) { diff --git a/app/services/import_service.rb b/app/services/import_service.rb index cb6d02e3b..89e107452 100644 --- a/app/services/import_service.rb +++ b/app/services/import_service.rb @@ -74,6 +74,7 @@ class ImportService def store_and_process_photo(photo, uploaded_file, random_string) File.open(uploaded_file) do |file| photo.random_string = random_string + photo.keep_original_format = true photo.unprocessed_image.store! file photo.update_remote_path photo.save(touch: false) diff --git a/app/uploaders/processed_image.rb b/app/uploaders/processed_image.rb index 79b61b9c7..5dfa6e5f0 100644 --- a/app/uploaders/processed_image.rb +++ b/app/uploaders/processed_image.rb @@ -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 diff --git a/app/uploaders/unprocessed_image.rb b/app/uploaders/unprocessed_image.rb index 6dad5f602..a653aa34f 100644 --- a/app/uploaders/unprocessed_image.rb +++ b/app/uploaders/unprocessed_image.rb @@ -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) && !model.keep_original_format 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 diff --git a/spec/fixtures/autumn_1440x960.heic b/spec/fixtures/autumn_1440x960.heic Binary files differnew file mode 100644 index 000000000..00cc549c0 --- /dev/null +++ b/spec/fixtures/autumn_1440x960.heic diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index 0b4996506..34c24b936 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -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 |