From b04633a9614609f18b39ba0f0015df301a04ab64 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 13 Oct 2022 11:29:19 +0200 Subject: Add image processing and generate blurhash for server thumbnail (#19348) Remove separate server hero setting --- app/models/form/admin_settings.rb | 2 -- app/models/site_upload.rb | 27 +++++++++++++++++++++++++- app/presenters/instance_presenter.rb | 4 ---- app/serializers/rest/instance_serializer.rb | 15 +++++++++++++- app/serializers/rest/v1/instance_serializer.rb | 2 +- app/views/admin/settings/edit.html.haml | 2 -- app/views/application/_sidebar.html.haml | 2 +- app/views/shared/_og.html.haml | 2 +- 8 files changed, 43 insertions(+), 13 deletions(-) (limited to 'app') diff --git a/app/models/form/admin_settings.rb b/app/models/form/admin_settings.rb index 7bd9e3743..b6bb3d795 100644 --- a/app/models/form/admin_settings.rb +++ b/app/models/form/admin_settings.rb @@ -22,7 +22,6 @@ class Form::AdminSettings custom_css profile_directory thumbnail - hero mascot trends trendable_by_default @@ -49,7 +48,6 @@ class Form::AdminSettings UPLOAD_KEYS = %i( thumbnail - hero mascot ).freeze diff --git a/app/models/site_upload.rb b/app/models/site_upload.rb index cf10b30fc..d3b81d4d5 100644 --- a/app/models/site_upload.rb +++ b/app/models/site_upload.rb @@ -12,10 +12,35 @@ # meta :json # created_at :datetime not null # updated_at :datetime not null +# blurhash :string # class SiteUpload < ApplicationRecord - has_attached_file :file + include Attachmentable + + STYLES = { + thumbnail: { + '@1x': { + format: 'png', + geometry: '1200x630#', + file_geometry_parser: FastGeometryParser, + blurhash: { + x_comp: 4, + y_comp: 4, + }.freeze, + }, + + '@2x': { + format: 'png', + geometry: '2400x1260#', + file_geometry_parser: FastGeometryParser, + }.freeze, + }.freeze, + + mascot: {}.freeze, + }.freeze + + has_attached_file :file, styles: ->(file) { STYLES[file.instance.var.to_sym] }, convert_options: { all: '-coalesce -strip' }, processors: [:lazy_thumbnail, :blurhash_transcoder, :type_corrector] validates_attachment_content_type :file, content_type: /\Aimage\/.*\z/ validates :file, presence: true diff --git a/app/presenters/instance_presenter.rb b/app/presenters/instance_presenter.rb index c461ac55f..43594a280 100644 --- a/app/presenters/instance_presenter.rb +++ b/app/presenters/instance_presenter.rb @@ -84,10 +84,6 @@ class InstancePresenter < ActiveModelSerializers::Model @thumbnail ||= Rails.cache.fetch('site_uploads/thumbnail') { SiteUpload.find_by(var: 'thumbnail') } end - def hero - @hero ||= Rails.cache.fetch('site_uploads/hero') { SiteUpload.find_by(var: 'hero') } - end - def mascot @mascot ||= Rails.cache.fetch('site_uploads/mascot') { SiteUpload.find_by(var: 'mascot') } end diff --git a/app/serializers/rest/instance_serializer.rb b/app/serializers/rest/instance_serializer.rb index f4ea49427..dfa8ce40a 100644 --- a/app/serializers/rest/instance_serializer.rb +++ b/app/serializers/rest/instance_serializer.rb @@ -17,7 +17,20 @@ class REST::InstanceSerializer < ActiveModel::Serializer has_many :rules, serializer: REST::RuleSerializer def thumbnail - object.thumbnail ? full_asset_url(object.thumbnail.file.url) : full_pack_url('media/images/preview.png') + if object.thumbnail + { + url: full_asset_url(object.thumbnail.file.url(:'@1x')), + blurhash: object.thumbnail.blurhash, + versions: { + '@1x': full_asset_url(object.thumbnail.file.url(:'@1x')), + '@2x': full_asset_url(object.thumbnail.file.url(:'@2x')), + }, + } + else + { + url: full_pack_url('media/images/preview.png'), + } + end end def usage diff --git a/app/serializers/rest/v1/instance_serializer.rb b/app/serializers/rest/v1/instance_serializer.rb index 47fa086fc..872175451 100644 --- a/app/serializers/rest/v1/instance_serializer.rb +++ b/app/serializers/rest/v1/instance_serializer.rb @@ -33,7 +33,7 @@ class REST::V1::InstanceSerializer < ActiveModel::Serializer end def thumbnail - instance_presenter.thumbnail ? full_asset_url(instance_presenter.thumbnail.file.url) : full_pack_url('media/images/preview.png') + instance_presenter.thumbnail ? full_asset_url(instance_presenter.thumbnail.file.url(:'@1x')) : full_pack_url('media/images/preview.png') end def stats diff --git a/app/views/admin/settings/edit.html.haml b/app/views/admin/settings/edit.html.haml index 79f73a60f..15b1a2498 100644 --- a/app/views/admin/settings/edit.html.haml +++ b/app/views/admin/settings/edit.html.haml @@ -34,8 +34,6 @@ .fields-row .fields-row__column.fields-row__column-6.fields-group = f.input :thumbnail, as: :file, wrapper: :with_block_label, label: t('admin.settings.thumbnail.title'), hint: site_upload_delete_hint(t('admin.settings.thumbnail.desc_html'), :thumbnail) - .fields-row__column.fields-row__column-6.fields-group - = f.input :hero, as: :file, wrapper: :with_block_label, label: t('admin.settings.hero.title'), hint: site_upload_delete_hint(t('admin.settings.hero.desc_html'), :hero) .fields-row .fields-row__column.fields-row__column-6.fields-group diff --git a/app/views/application/_sidebar.html.haml b/app/views/application/_sidebar.html.haml index eb2813dd0..6d18668b0 100644 --- a/app/views/application/_sidebar.html.haml +++ b/app/views/application/_sidebar.html.haml @@ -1,6 +1,6 @@ .hero-widget .hero-widget__img - = image_tag @instance_presenter.hero&.file&.url || @instance_presenter.thumbnail&.file&.url || asset_pack_path('media/images/preview.png'), alt: @instance_presenter.title + = image_tag @instance_presenter.thumbnail&.file&.url(:'@1x') || asset_pack_path('media/images/preview.png'), alt: @instance_presenter.title .hero-widget__text %p= @instance_presenter.description.html_safe.presence || t('about.about_mastodon_html') diff --git a/app/views/shared/_og.html.haml b/app/views/shared/_og.html.haml index b54ab2429..2941b566e 100644 --- a/app/views/shared/_og.html.haml +++ b/app/views/shared/_og.html.haml @@ -8,7 +8,7 @@ = opengraph 'og:type', 'website' = opengraph 'og:title', @instance_presenter.title = opengraph 'og:description', description -= opengraph 'og:image', full_asset_url(thumbnail&.file&.url || asset_pack_path('media/images/preview.png', protocol: :request)) += opengraph 'og:image', full_asset_url(thumbnail&.file&.url(:'@1x') || asset_pack_path('media/images/preview.png', protocol: :request)) = opengraph 'og:image:width', thumbnail ? thumbnail.meta['width'] : '1200' = opengraph 'og:image:height', thumbnail ? thumbnail.meta['height'] : '630' = opengraph 'twitter:card', 'summary_large_image' -- cgit