diff --git a/src/backend/src/services/AppIconService.js b/src/backend/src/services/AppIconService.js index 770fb83b1..ddefcd018 100644 --- a/src/backend/src/services/AppIconService.js +++ b/src/backend/src/services/AppIconService.js @@ -1,6 +1,8 @@ const { HLWrite } = require("../filesystem/hl_operations/hl_write"); const { LLMkdir } = require("../filesystem/ll_operations/ll_mkdir"); +const { LLRead } = require("../filesystem/ll_operations/ll_read"); const { NodePathSelector, NodeChildSelector, RootNodeSelector } = require("../filesystem/node/selectors"); +const { Endpoint } = require("../util/expressutil"); const { buffer_to_stream } = require("../util/streamutil"); const BaseService = require("./BaseService"); @@ -11,6 +13,52 @@ class AppIconService extends BaseService { sharp: require('sharp'), } + async ['__on_install.routes'] (_, { app }) { + Endpoint({ + route: '/app-icon/:app_uid/:size', + methods: ['GET'], + handler: async (req, res) => { + // Validate parameters + let { app_uid, size } = req.params; + if ( ! ICON_SIZES.includes(Number(size)) ) { + res.status(400).send('Invalid size'); + return; + } + if ( ! app_uid.startsWith('app-') ) { + app_uid = `app-${app_uid}`; + } + + // Get icon file node + const dir_app_icons = await this.get_app_icons(); + const node = await dir_app_icons.getChild(`${app_uid}-${size}.png`); + await node.fetchEntry(); + + const svc_su = this.services.get('su'); + const ll_read = new LLRead(); + const stream = await ll_read.run({ + fsNode: node, + actor: await svc_su.get_system_actor(), + }); + + res.set('Content-Type', 'image/png'); + stream.pipe(res); + }, + }).attach(app); + } + + async get_app_icons () { + if ( this.dir_app_icons ) { + return this.dir_app_icons; + } + + const svc_fs = this.services.get('filesystem'); + const dir_app_icons = await svc_fs.node( + new NodePathSelector('/system/app_icons') + ); + + this.dir_app_icons = dir_app_icons; + } + async ['__on_user.system-user-ready'] () { const svc_su = this.services.get('su'); const svc_fs = this.services.get('filesystem'); @@ -30,6 +78,7 @@ class AppIconService extends BaseService { actor: await svc_su.get_system_actor(), }); } + this.dir_app_icons = dir_app_icons; // Listen for new app icons const svc_event = this.services.get('event');