diff --git a/app/components/app_catalog/web_app_icon_component.html.erb b/app/components/app_catalog/web_app_icon_component.html.erb new file mode 100644 index 0000000..3f9c5f3 --- /dev/null +++ b/app/components/app_catalog/web_app_icon_component.html.erb @@ -0,0 +1,5 @@ +<% if @image_url %> + <%= image_tag @image_url, class: "h-full w-full" %> +<% else %> + <%= render partial: "icons/remotestorage", locals: { custom_class: "h-full w-full p-0.5 text-gray-200" } %> +<% end %> diff --git a/app/components/app_catalog/web_app_icon_component.rb b/app/components/app_catalog/web_app_icon_component.rb new file mode 100644 index 0000000..8421d4f --- /dev/null +++ b/app/components/app_catalog/web_app_icon_component.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module AppCatalog + class WebAppIconComponent < ViewComponent::Base + def initialize(web_app:) + if web_app&.icon&.attached? + @image_url = image_url_for(web_app.icon) + elsif web_app&.apple_touch_icon&.attached? + @image_url = image_url_for(web_app.apple_touch_icon) + end + end + + def image_url_for(attachment) + if Setting.s3_enabled? + s3_image_url(attachment) + else + Rails.application.routes.url_helpers.rails_blob_path(attachment, only_path: true) + end + end + end +end diff --git a/app/components/rs_auth_component.html.erb b/app/components/rs_auth_component.html.erb index 18ffcc8..535dc1b 100644 --- a/app/components/rs_auth_component.html.erb +++ b/app/components/rs_auth_component.html.erb @@ -1,16 +1,10 @@
<%= @auth.client_id %> diff --git a/app/models/app_catalog/web_app.rb b/app/models/app_catalog/web_app.rb index 0b88333..388e610 100644 --- a/app/models/app_catalog/web_app.rb +++ b/app/models/app_catalog/web_app.rb @@ -1,7 +1,7 @@ class AppCatalog::WebApp < ApplicationRecord store :metadata, coder: JSON - has_many :remote_storage_authorizations + has_many :remote_storage_authorizations, dependent: :destroy has_one_attached :icon has_one_attached :apple_touch_icon diff --git a/app/models/setting.rb b/app/models/setting.rb index 27acdcc..daa4b29 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -15,6 +15,9 @@ class Setting < RailsSettings::Base field :redis_url, type: :string, default: ENV["REDIS_URL"] || "redis://localhost:6379/0" + field :s3_enabled, type: :boolean, + default: ENV["S3_ENABLED"] && ENV["S3_ENABLED"].to_s != "false" + # # Registrations # diff --git a/app/services/app_catalog_manager/update_metadata.rb b/app/services/app_catalog_manager/update_metadata.rb index c11b864..a506013 100644 --- a/app/services/app_catalog_manager/update_metadata.rb +++ b/app/services/app_catalog_manager/update_metadata.rb @@ -18,6 +18,10 @@ module AppCatalogManager @app.metadata[prop] = metadata.send(prop) if prop end + @app.save! + + # TODO move icon downloads to separate, async job + if icon = metadata.select_icon(sizes: "256x256") || icon = metadata.select_icon(sizes: "192x192") attach_remote_image(:icon, icon) @@ -27,8 +31,6 @@ module AppCatalogManager if apple_touch_icon = metadata.select_icon(purpose: "apple-touch-icon") attach_remote_image(:apple_touch_icon, apple_touch_icon) end - - @app.save! rescue Manifique::Error => e msg = "Fetching web app manifest failed for #{e.url}: #{e.type}" Rails.logger.warn(msg) @@ -42,14 +44,19 @@ module AppCatalogManager else download_url = "#{@app.url}/#{icon["src"].gsub(/^\//,'')}" end - filename = "#{attachment_name}.png" - key = "web_apps/#{@app.id}/icons/#{attachment_name}.png" + filename = "#{attachment_name}-#{Time.now.to_i}.png" + key = "web_apps/#{@app.id}/icons/#{filename}" begin tempfile = Down.download(download_url) @app.send(attachment_name).attach(key: key, io: tempfile, filename: filename) rescue Down::NotFound - Rails.logger.warn "Icon download failed: NotFound error for #{download_url}" + msg = "Download of \"#{attachment_name}\" failed: NotFound error for #{download_url}" + Rails.logger.warn(msg) + Sentry.capture_message(msg) + rescue => e + Rails.logger.warn "Saving attachment \"#{attachment_name}\" failed: \"#{e.message}\"" + Sentry.capture_exception(e) if Setting.sentry_enabled? end end end diff --git a/app/views/icons/_kebab-menu.html.erb b/app/views/icons/_kebap-menu.html.erb similarity index 100% rename from app/views/icons/_kebab-menu.html.erb rename to app/views/icons/_kebap-menu.html.erb diff --git a/app/views/icons/_remotestorage.html.erb b/app/views/icons/_remotestorage.html.erb index 2daafc4..4de0c04 100644 --- a/app/views/icons/_remotestorage.html.erb +++ b/app/views/icons/_remotestorage.html.erb @@ -1,5 +1,5 @@ -