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 @@
- <% if @web_app.icon.attached? %> - <%= image_tag s3_image_url(@web_app.icon), class: "h-full w-full" %> - <% elsif @web_app.apple_touch_icon.attached? %> - <%= image_tag s3_image_url(@web_app.apple_touch_icon), class: "h-full w-full" %> - <% else %> - <%= render partial: "icons/remotestorage", locals: { custom_class: "h-full w-full p-0.5 text-gray-200" } %> - <% end %> + <%= render AppCatalog::WebAppIconComponent.new(web_app: @web_app) %>

- <%= @web_app.name %> + <%= @web_app&.name || @auth.app_name %>

<%= @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 @@ - + diff --git a/config/environments/development.rb b/config/environments/development.rb index e13b809..9f73578 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -71,7 +71,7 @@ Rails.application.configure do # Allow requests from any IP config.web_console.permissions = '0.0.0.0/0' - if ENV["S3_ENABLED"] + if ENV["S3_ENABLED"] && ENV["S3_ENABLED"].to_s != "false" config.active_storage.service = :s3 else config.active_storage.service = :local diff --git a/config/environments/production.rb b/config/environments/production.rb index 5adf41e..16e7fa5 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -110,7 +110,7 @@ Rails.application.configure do # Set this to true and configure the email server for immediate delivery to raise delivery errors. config.action_mailer.raise_delivery_errors = true - if ENV["S3_ENABLED"] + if ENV["S3_ENABLED"] && ENV["S3_ENABLED"].to_s != "false" config.active_storage.service = :s3 else config.active_storage.service = :local diff --git a/config/storage.yml b/config/storage.yml index 3ddfbb8..010aeb8 100644 --- a/config/storage.yml +++ b/config/storage.yml @@ -1,12 +1,12 @@ local: service: Disk - root: <%= Rails.root.join("storage") %> + root: <%= ENV["ACTIVE_STORAGE_PATH"] || Rails.root.join("storage") %> test: service: Disk root: <%= Rails.root.join("tmp/storage") %> -<% if ENV["S3_ENABLED"] %> +<% if ENV["S3_ENABLED"] && ENV["S3_ENABLED"].to_s != "false" %> s3: service: S3 endpoint: <%= ENV["S3_ENDPOINT"] %> diff --git a/docker-compose.yml b/docker-compose.yml index f046edf..e24b39e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -42,8 +42,10 @@ services: LDAP_ADMIN_PASSWORD: passthebutter LDAP_USE_TLS: "false" REDIS_URL: redis://redis:6379/0 + ACTIVE_STORAGE_PATH: "/akkounts/tmp/attachments" RS_REDIS_URL: redis://redis:6379/1 RS_STORAGE_URL: "http://localhost:4567" + S3_ENABLED: false depends_on: - ldap - redis @@ -67,6 +69,7 @@ services: REDIS_URL: redis://redis:6379/0 RS_REDIS_URL: redis://redis:6379/1 RS_STORAGE_URL: "http://localhost:4567" + S3_ENABLED: false depends_on: - ldap - redis diff --git a/spec/components/app_catalog/web_app_icon_component_spec.rb b/spec/components/app_catalog/web_app_icon_component_spec.rb new file mode 100644 index 0000000..f9ed3e5 --- /dev/null +++ b/spec/components/app_catalog/web_app_icon_component_spec.rb @@ -0,0 +1,11 @@ +require "rails_helper" + +RSpec.describe AppCatalog::WebAppIconComponent, type: :component do + describe "No web app given" do + it "renders the default icon" do + expect( + render_inline(described_class.new(web_app: nil)) {}.to_html + ).to include("icon-remotestorage") + end + end +end