From b153a938645a0c2193a20967325ca855ab671073 Mon Sep 17 00:00:00 2001 From: Vijay A Date: Sat, 14 May 2022 21:46:47 -0500 Subject: Extracted ImageHelper and ImageController from ServiceController for reuse --- .../app/Controllers/Http/ImageController.js | 21 ++++++++++++ .../app/Controllers/Http/ServiceController.js | 40 ++++------------------ src/internal-server/app/ImageHelper.js | 24 +++++++++++++ src/internal-server/start/routes.js | 2 +- 4 files changed, 53 insertions(+), 34 deletions(-) create mode 100644 src/internal-server/app/Controllers/Http/ImageController.js create mode 100644 src/internal-server/app/ImageHelper.js (limited to 'src/internal-server') diff --git a/src/internal-server/app/Controllers/Http/ImageController.js b/src/internal-server/app/Controllers/Http/ImageController.js new file mode 100644 index 000000000..9b11783c7 --- /dev/null +++ b/src/internal-server/app/Controllers/Http/ImageController.js @@ -0,0 +1,21 @@ +const Env = use('Env'); + +const path = require('path'); +const fs = require('fs-extra'); + +class ImageController { + async icon({ params, response }) { + const { id } = params; + + const iconPath = path.join(Env.get('USER_PATH'), 'icons', id); + if (!fs.existsSync(iconPath)) { + return response.status(404).send({ + status: "Icon doesn't exist", + }); + } + + return response.download(iconPath); + } +} + +module.exports = ImageController; diff --git a/src/internal-server/app/Controllers/Http/ServiceController.js b/src/internal-server/app/Controllers/Http/ServiceController.js index e8c94c25f..f2f6e7028 100644 --- a/src/internal-server/app/Controllers/Http/ServiceController.js +++ b/src/internal-server/app/Controllers/Http/ServiceController.js @@ -3,10 +3,9 @@ const { validateAll } = use('Validator'); const Env = use('Env'); const { v4: uuid } = require('uuid'); -const path = require('path'); -const fs = require('fs-extra'); const { LOCAL_HOSTNAME, DEFAULT_SERVICE_ORDER } = require('../../../../config'); const { API_VERSION } = require('../../../../environment-remote'); +const moveIcon = require('../../ImageHelper'); const hostname = LOCAL_HOSTNAME; const port = Env.get('PORT'); @@ -104,14 +103,8 @@ class ServiceController { } async edit({ request, response, params }) { + // Upload custom service icon if present if (request.file('icon')) { - // Upload custom service icon - await fs.ensureDir(path.join(Env.get('USER_PATH'), 'icons')); - - const icon = request.file('icon', { - types: ['image'], - size: '2mb', - }); const { id } = params; const serviceQuery = await Service.query().where('serviceId', id).fetch(); const service = serviceQuery.rows[0]; @@ -120,19 +113,13 @@ class ServiceController { ? JSON.parse(service.settings) : service.settings; - // Generate new icon ID - let iconId; - do { - iconId = uuid() + uuid(); - } while (fs.existsSync(path.join(Env.get('USER_PATH'), 'icons', iconId))); - iconId = `${iconId}.${icon.extname}`; - - await icon.move(path.join(Env.get('USER_PATH'), 'icons'), { - name: iconId, - overwrite: true, + const icon = request.file('icon', { + types: ['image'], + size: '2mb', }); - if (!icon.moved()) { + const iconId = await moveIcon(icon); + if (iconId === '-1') { return response.status(500).send(icon.error()); } @@ -205,19 +192,6 @@ class ServiceController { }); } - async icon({ params, response }) { - const { id } = params; - - const iconPath = path.join(Env.get('USER_PATH'), 'icons', id); - if (!fs.existsSync(iconPath)) { - return response.status(404).send({ - status: "Icon doesn't exist", - }); - } - - return response.download(iconPath); - } - async reorder({ request, response }) { const data = request.all(); diff --git a/src/internal-server/app/ImageHelper.js b/src/internal-server/app/ImageHelper.js new file mode 100644 index 000000000..b399d08c4 --- /dev/null +++ b/src/internal-server/app/ImageHelper.js @@ -0,0 +1,24 @@ +const Env = use('Env'); + +const { v4: uuid } = require('uuid'); + +const path = require('path'); +const fs = require('fs-extra'); + +module.exports = async (icon) => { + const iconsPath = path.join(Env.get('USER_PATH'), 'icons'); + await fs.ensureDir(iconsPath); + + // Generate new icon ID + let iconId; + do { + iconId = uuid() + uuid(); + } while (fs.existsSync(path.join(iconsPath, iconId))); + iconId = `${iconId}.${icon.extname}`; + + await icon.move(iconsPath, { + name: iconId, + overwrite: true, + }); + return !icon.moved() ? '-1' : iconId; +}; diff --git a/src/internal-server/start/routes.js b/src/internal-server/start/routes.js index 63d0876e3..79c809f5f 100644 --- a/src/internal-server/start/routes.js +++ b/src/internal-server/start/routes.js @@ -66,7 +66,7 @@ Route.group(() => { .middleware(OnlyAllowFerdium); Route.group(() => { - Route.get('icon/:id', 'ServiceController.icon'); + Route.get('icon/:id', 'ImageController.icon'); }).prefix(API_VERSION); // Franz account import -- cgit v1.2.3-70-g09d2