Skip to content

Modern Docker Management, Designed for Everyone

refactor: move pwa images to be embeded in the backend (#2147)

Kyle Mendell471b0d793a4f
diff --git a/backend/internal/huma/handlers/appimages.go b/backend/internal/huma/handlers/appimages.go
index b5730b294..44447c4df 100644
--- a/backend/internal/huma/handlers/appimages.go
+++ b/backend/internal/huma/handlers/appimages.go
@@ -3,6 +3,8 @@ package handlers
import (
"context"
"net/http"
+ "path/filepath"
+ "strings"
"github.com/danielgtaylor/huma/v2"
"github.com/getarcaneapp/arcane/backend/internal/common"
@@ -21,12 +23,27 @@ type GetLogoInput struct {
Color string `query:"color" doc:"Optional accent color override for preview (e.g., 'oklch(0.65 0.2 150)')"`
}
+type GetPWAIconInput struct {
+ Filename string `path:"filename" example:"icon-192x192.png" doc:"PWA icon filename"`
+}
+
type GetAppImageOutput struct {
ContentType string `header:"Content-Type"`
CacheControl string `header:"Cache-Control"`
Body []byte
}
+var allowedPWAIconFilenames = map[string]struct{}{
+ "icon-72x72.png": {},
+ "icon-96x96.png": {},
+ "icon-128x128.png": {},
+ "icon-144x144.png": {},
+ "icon-152x152.png": {},
+ "icon-192x192.png": {},
+ "icon-384x384.png": {},
+ "icon-512x512.png": {},
+}
+
// RegisterAppImages registers application image routes using Huma.
func RegisterAppImages(api huma.API, appImagesService *services.ApplicationImagesService) {
h := &AppImagesHandler{
@@ -68,6 +85,15 @@ func RegisterAppImages(api huma.API, appImagesService *services.ApplicationImage
Description: "Get the default user profile image",
Tags: []string{"Application Images"},
}, h.GetDefaultProfile)
+
+ huma.Register(api, huma.Operation{
+ OperationID: "get-pwa-icon",
+ Method: http.MethodGet,
+ Path: "/app-images/pwa/{filename}",
+ Summary: "Get PWA icon",
+ Description: "Get a Progressive Web App icon image",
+ Tags: []string{"Application Images"},
+ }, h.GetPWAIcon)
}
// GetLogo returns the application logo image.
@@ -111,10 +137,28 @@ func (h *AppImagesHandler) GetDefaultProfile(ctx context.Context, input *struct{
return h.getImage("profile")
}
+// GetPWAIcon returns a PWA icon image.
+func (h *AppImagesHandler) GetPWAIcon(ctx context.Context, input *GetPWAIconInput) (*GetAppImageOutput, error) {
+ if h.appImagesService == nil {
+ return nil, huma.Error500InternalServerError("service not available")
+ }
+
+ return h.getImageByFilenameInternal(input.Filename)
+}
+
func (h *AppImagesHandler) getImage(name string) (*GetAppImageOutput, error) {
return h.getImageWithColor(name, "")
}
+func (h *AppImagesHandler) getImageByFilenameInternal(filename string) (*GetAppImageOutput, error) {
+ if _, ok := allowedPWAIconFilenames[filename]; !ok {
+ return nil, huma.Error400BadRequest("invalid PWA icon filename")
+ }
+
+ name := strings.TrimSuffix(filename, filepath.Ext(filename))
+ return h.getImage(name)
+}
+
func (h *AppImagesHandler) getImageWithColor(name string, colorOverride string) (*GetAppImageOutput, error) {
imageData, mimeType, err := h.appImagesService.GetImageWithColor(name, colorOverride)
if err != nil {
diff --git a/backend/internal/huma/handlers/appimages_test.go b/backend/internal/huma/handlers/appimages_test.go
new file mode 100644
index 000000000..48b32df97
--- /dev/null
+++ b/backend/internal/huma/handlers/appimages_test.go
@@ -0,0 +1,55 @@
+package handlers
+
+import (
+ "context"
+ "testing"
+
+ "github.com/danielgtaylor/huma/v2"
+ "github.com/getarcaneapp/arcane/backend/internal/services"
+ "github.com/getarcaneapp/arcane/backend/resources"
+ "github.com/stretchr/testify/require"
+)
+
+func TestGetPWAIconReturnsPNGFromEmbeddedBackendAssets(t *testing.T) {
+ handler := &AppImagesHandler{
+ appImagesService: services.NewApplicationImagesService(resources.FS, nil),
+ }
+
+ filenames := []string{
+ "icon-72x72.png",
+ "icon-96x96.png",
+ "icon-128x128.png",
+ "icon-144x144.png",
+ "icon-152x152.png",
+ "icon-192x192.png",
+ "icon-384x384.png",
+ "icon-512x512.png",
+ }
+
+ for _, filename := range filenames {
+ t.Run(filename, func(t *testing.T) {
+ resp, err := handler.GetPWAIcon(context.Background(), &GetPWAIconInput{
+ Filename: filename,
+ })
+ require.NoError(t, err)
+ require.Equal(t, "image/png", resp.ContentType)
+ require.Equal(t, "public, max-age=900, stale-while-revalidate=86400", resp.CacheControl)
+ require.NotEmpty(t, resp.Body)
+ })
+ }
+}
+
+func TestGetPWAIconRejectsNonPWAAssets(t *testing.T) {
+ handler := &AppImagesHandler{
+ appImagesService: services.NewApplicationImagesService(resources.FS, nil),
+ }
+
+ resp, err := handler.GetPWAIcon(context.Background(), &GetPWAIconInput{
+ Filename: "logo.png",
+ })
+ require.Nil(t, resp)
+ require.Error(t, err)
+
+ statusErr := huma.Error400BadRequest("invalid PWA icon filename")
+ require.Equal(t, statusErr.GetStatus(), err.(interface{ GetStatus() int }).GetStatus())
+}
diff --git a/frontend/static/img/pwa/icon-128x128.png b/backend/resources/images/icon-128x128.png
similarity index 100%
rename from frontend/static/img/pwa/icon-128x128.png
rename to backend/resources/images/icon-128x128.png
diff --git a/frontend/static/img/pwa/icon-144x144.png b/backend/resources/images/icon-144x144.png
similarity index 100%
rename from frontend/static/img/pwa/icon-144x144.png
rename to backend/resources/images/icon-144x144.png
diff --git a/frontend/static/img/pwa/icon-152x152.png b/backend/resources/images/icon-152x152.png
similarity index 100%
rename from frontend/static/img/pwa/icon-152x152.png
rename to backend/resources/images/icon-152x152.png
diff --git a/frontend/static/img/pwa/icon-192x192.png b/backend/resources/images/icon-192x192.png
similarity index 100%
rename from frontend/static/img/pwa/icon-192x192.png
rename to backend/resources/images/icon-192x192.png
diff --git a/frontend/static/img/pwa/icon-384x384.png b/backend/resources/images/icon-384x384.png
similarity index 100%
rename from frontend/static/img/pwa/icon-384x384.png
rename to backend/resources/images/icon-384x384.png
diff --git a/frontend/static/img/pwa/icon-512x512.png b/backend/resources/images/icon-512x512.png
similarity index 100%
rename from frontend/static/img/pwa/icon-512x512.png
rename to backend/resources/images/icon-512x512.png
diff --git a/frontend/static/img/pwa/icon-72x72.png b/backend/resources/images/icon-72x72.png
similarity index 100%
rename from frontend/static/img/pwa/icon-72x72.png
rename to backend/resources/images/icon-72x72.png
diff --git a/frontend/static/img/pwa/icon-96x96.png b/backend/resources/images/icon-96x96.png
similarity index 100%
rename from frontend/static/img/pwa/icon-96x96.png
rename to backend/resources/images/icon-96x96.png
diff --git a/frontend/src/app.html b/frontend/src/app.html
index 5c76b9748..214370ae1 100644
--- a/frontend/src/app.html
+++ b/frontend/src/app.html
@@ -6,8 +6,8 @@
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<link rel="icon" href="/api/app-images/favicon" />
- <link rel="apple-touch-icon" sizes="152x152" href="/img/pwa/icon-152x152.png" />
- <link rel="apple-touch-icon" href="/img/pwa/icon-192x192.png" />
+ <link rel="apple-touch-icon" sizes="152x152" href="/api/app-images/pwa/icon-152x152.png" />
+ <link rel="apple-touch-icon" href="/api/app-images/pwa/icon-192x192.png" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, viewport-fit=cover" />
<link rel="manifest" href="%sveltekit.assets%/app.webmanifest" />
<meta name="theme-color" content="oklch(1 0 0)" media="(prefers-color-scheme: light)" />
diff --git a/frontend/static/app.webmanifest b/frontend/static/app.webmanifest
index d6bdec823..64abc5dd5 100644
--- a/frontend/static/app.webmanifest
+++ b/frontend/static/app.webmanifest
@@ -12,49 +12,49 @@
"categories": ["productivity", "developer", "utilities"],
"icons": [
{
- "src": "img/pwa/icon-72x72.png",
+ "src": "/api/app-images/pwa/icon-72x72.png",
"sizes": "72x72",
"type": "image/png",
"purpose": "maskable any"
},
{
- "src": "img/pwa/icon-96x96.png",
+ "src": "/api/app-images/pwa/icon-96x96.png",
"sizes": "96x96",
"type": "image/png",
"purpose": "maskable any"
},
{
- "src": "img/pwa/icon-128x128.png",
+ "src": "/api/app-images/pwa/icon-128x128.png",
"sizes": "128x128",
"type": "image/png",
"purpose": "maskable any"
},
{
- "src": "img/pwa/icon-144x144.png",
+ "src": "/api/app-images/pwa/icon-144x144.png",
"sizes": "144x144",
"type": "image/png",
"purpose": "maskable any"
},
{
- "src": "img/pwa/icon-152x152.png",
+ "src": "/api/app-images/pwa/icon-152x152.png",
"sizes": "152x152",
"type": "image/png",
"purpose": "maskable any"
},
{
- "src": "img/pwa/icon-192x192.png",
+ "src": "/api/app-images/pwa/icon-192x192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "maskable any"
},
{
- "src": "img/pwa/icon-384x384.png",
+ "src": "/api/app-images/pwa/icon-384x384.png",
"sizes": "384x384",
"type": "image/png",
"purpose": "maskable any"
},
{
- "src": "img/pwa/icon-512x512.png",
+ "src": "/api/app-images/pwa/icon-512x512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable any"
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index d3b551f1c..8653b6298 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -3145,7 +3145,6 @@ packages:
resolution: {integrity: sha512-vuNwKSaKiqm7g0THUBu2x7ckSs3XJLXE+2ssL7/MfTGPLLcrJQ/4Uq1CjPTtO5cCIiRxqvN6Twy1qOwhL0Xjcw==}
engines: {node: 20 || >=22}
- deprecated: Old versions of glob are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exorbitant rates) by contacting [email protected]
hasBin: true