Download and attach icons for web apps

This commit is contained in:
Râu Cao 2023-10-23 15:31:22 +02:00
parent e56c9bd0d5
commit d4f71e98ed
Signed by: raucao
GPG Key ID: 15E65F399D084BA9
6 changed files with 84 additions and 32 deletions

View File

@ -47,6 +47,8 @@ gem 'flipper-ui'
# HTTP requests # HTTP requests
gem 'faraday' gem 'faraday'
gem 'down'
gem 'aws-sdk-s3', require: false
# Background/scheduled jobs # Background/scheduled jobs
gem 'sidekiq', '< 7' gem 'sidekiq', '< 7'

View File

@ -92,6 +92,22 @@ GEM
addressable (2.8.5) addressable (2.8.5)
public_suffix (>= 2.0.2, < 6.0) public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2) ast (2.4.2)
aws-eventstream (1.2.0)
aws-partitions (1.839.0)
aws-sdk-core (3.185.1)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.651.0)
aws-sigv4 (~> 1.5)
jmespath (~> 1, >= 1.6.1)
aws-sdk-kms (1.72.0)
aws-sdk-core (~> 3, >= 3.184.0)
aws-sigv4 (~> 1.1)
aws-sdk-s3 (1.136.0)
aws-sdk-core (~> 3, >= 3.181.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.6)
aws-sigv4 (1.6.0)
aws-eventstream (~> 1, >= 1.0.2)
backport (1.2.0) backport (1.2.0)
base64 (0.1.1) base64 (0.1.1)
bcrypt (3.1.19) bcrypt (3.1.19)
@ -147,6 +163,8 @@ GEM
dotenv-rails (2.8.1) dotenv-rails (2.8.1)
dotenv (= 2.8.1) dotenv (= 2.8.1)
railties (>= 3.2) railties (>= 3.2)
down (5.4.1)
addressable (~> 2.8)
e2mmap (0.1.0) e2mmap (0.1.0)
ecdsa (1.2.0) ecdsa (1.2.0)
ecdsa_ext (0.5.0) ecdsa_ext (0.5.0)
@ -206,6 +224,7 @@ GEM
jbuilder (2.11.5) jbuilder (2.11.5)
actionview (>= 5.0.0) actionview (>= 5.0.0)
activesupport (>= 5.0.0) activesupport (>= 5.0.0)
jmespath (1.6.2)
json (2.6.3) json (2.6.3)
kramdown (2.4.0) kramdown (2.4.0)
rexml rexml
@ -445,6 +464,7 @@ PLATFORMS
x86_64-linux x86_64-linux
DEPENDENCIES DEPENDENCIES
aws-sdk-s3
byebug (~> 11.1) byebug (~> 11.1)
capybara capybara
cssbundling-rails cssbundling-rails
@ -453,6 +473,7 @@ DEPENDENCIES
devise_ldap_authenticatable devise_ldap_authenticatable
discourse_api discourse_api
dotenv-rails dotenv-rails
down
factory_bot_rails factory_bot_rails
faker faker
faraday faraday

View File

@ -1,9 +1,20 @@
class AppCatalog::WebApp < ApplicationRecord class AppCatalog::WebApp < ApplicationRecord
store :metadata, coder: JSON store :metadata, coder: JSON
has_one_attached :icon do |attachable|
attachable.variant :medium, resize_to_limit: [128,128]
attachable.variant :large, resize_to_limit: [256,256]
end
has_one_attached :apple_touch_icon
validates :url, presence: true, uniqueness: true validates :url, presence: true, uniqueness: true
validates :url, format: { with: URI.regexp }, validates :url, format: { with: URI.regexp },
if: Proc.new { |a| a.url.present? } if: Proc.new { |a| a.url.present? }
# before_create :update_metadata
def update_metadata
AppCatalogManager::UpdateMetadata.call(self)
end
end end

View File

@ -1,26 +0,0 @@
require 'manifique'
module AppCatalogManager
class FetchMetadata < AppCatalogManagerService
def initialize(app)
@app = app
end
def call
agent = Manifique::Agent.new(url: @app.url)
metadata = agent.fetch_metadata
@app.name = metadata.name
[:name, :short_name, :description, :theme_color, :background_color,
:display, :start_url, :scope, :share_target].each do |prop|
@app.metadata[prop] = metadata.send(prop) if prop
end
rescue Manifique::Error => e
msg = "Fetching web app manifest failed for #{e.url}: #{e.type}"
Rails.logger.warn(msg)
Sentry.capture_message(msg) if Setting.sentry_enabled?
false
end
end
end

View File

@ -0,0 +1,44 @@
require "manifique"
require "down"
module AppCatalogManager
class UpdateMetadata < AppCatalogManagerService
def initialize(app)
@app = app
end
def call
agent = Manifique::Agent.new(url: @app.url)
metadata = agent.fetch_metadata
@app.name = metadata.name
[:name, :short_name, :description, :theme_color, :background_color,
:display, :start_url, :scope, :share_target, :icons].each do |prop|
@app.metadata[prop] = metadata.send(prop) if prop
end
if icon = metadata.select_icon(sizes: "256x256")
attach_remote_image(:icon, icon)
end
if apple_touch_icon = metadata.select_icon(purpose: "apple-touch-icon")
attach_remote_image(:apple_touch_icon, apple_touch_icon)
end
byebug
rescue Manifique::Error => e
msg = "Fetching web app manifest failed for #{e.url}: #{e.type}"
Rails.logger.warn(msg)
Sentry.capture_message(msg) if Setting.sentry_enabled?
false
end
def attach_remote_image(attachment_name, icon)
download_url = "#{@app.url}/#{icon["src"].gsub(/^\//,'')}"
filename = "web_apps/#{@app.id}/icons/#{attachment_name}.png"
tempfile = Down.download(download_url)
@app.send(attachment_name).attach(io: tempfile, filename: filename)
end
end
end

View File

@ -9,9 +9,9 @@ test:
<% if ENV["S3_ENABLED"] %> <% if ENV["S3_ENABLED"] %>
s3: s3:
service: S3 service: S3
endpoint: ENV["S3_ENDPOINT"] endpoint: <%= ENV["S3_ENDPOINT"] %>
region: ENV["S3_REGION"] region: <%= ENV["S3_REGION"] %>
bucket: ENV["S3_BUCKET"] bucket: <%= ENV["S3_BUCKET"] %>
access_key_id: ENV["S3_ACCESS_KEY"] access_key_id: <%= ENV["S3_ACCESS_KEY"] %>
secret_access_key: ENV["S3_SECRET_KEY"] secret_access_key: <%= ENV["S3_SECRET_KEY"] %>
<% end %> <% end %>