aboutsummaryrefslogtreecommitdiffstats
path: root/src/internal-server/app/Controllers/Http/ServiceController.js
diff options
context:
space:
mode:
authorLibravatar Vijay A <vraravam@users.noreply.github.com>2022-05-14 21:46:47 -0500
committerLibravatar Vijay Aravamudhan <vraravam@users.noreply.github.com>2022-05-15 15:13:19 -0500
commitb153a938645a0c2193a20967325ca855ab671073 (patch)
treedd126334c16ea46136648147edde0e8125137947 /src/internal-server/app/Controllers/Http/ServiceController.js
parentUpgrade 'pnpm' to '7.1.0'; pull in latst version of 'recipes' [skip ci] (diff)
downloadferdium-app-b153a938645a0c2193a20967325ca855ab671073.tar.gz
ferdium-app-b153a938645a0c2193a20967325ca855ab671073.tar.zst
ferdium-app-b153a938645a0c2193a20967325ca855ab671073.zip
Extracted ImageHelper and ImageController from ServiceController for reuse
Diffstat (limited to 'src/internal-server/app/Controllers/Http/ServiceController.js')
-rw-r--r--src/internal-server/app/Controllers/Http/ServiceController.js40
1 files changed, 7 insertions, 33 deletions
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');
3const Env = use('Env'); 3const Env = use('Env');
4 4
5const { v4: uuid } = require('uuid'); 5const { v4: uuid } = require('uuid');
6const path = require('path');
7const fs = require('fs-extra');
8const { LOCAL_HOSTNAME, DEFAULT_SERVICE_ORDER } = require('../../../../config'); 6const { LOCAL_HOSTNAME, DEFAULT_SERVICE_ORDER } = require('../../../../config');
9const { API_VERSION } = require('../../../../environment-remote'); 7const { API_VERSION } = require('../../../../environment-remote');
8const moveIcon = require('../../ImageHelper');
10 9
11const hostname = LOCAL_HOSTNAME; 10const hostname = LOCAL_HOSTNAME;
12const port = Env.get('PORT'); 11const port = Env.get('PORT');
@@ -104,14 +103,8 @@ class ServiceController {
104 } 103 }
105 104
106 async edit({ request, response, params }) { 105 async edit({ request, response, params }) {
106 // Upload custom service icon if present
107 if (request.file('icon')) { 107 if (request.file('icon')) {
108 // Upload custom service icon
109 await fs.ensureDir(path.join(Env.get('USER_PATH'), 'icons'));
110
111 const icon = request.file('icon', {
112 types: ['image'],
113 size: '2mb',
114 });
115 const { id } = params; 108 const { id } = params;
116 const serviceQuery = await Service.query().where('serviceId', id).fetch(); 109 const serviceQuery = await Service.query().where('serviceId', id).fetch();
117 const service = serviceQuery.rows[0]; 110 const service = serviceQuery.rows[0];
@@ -120,19 +113,13 @@ class ServiceController {
120 ? JSON.parse(service.settings) 113 ? JSON.parse(service.settings)
121 : service.settings; 114 : service.settings;
122 115
123 // Generate new icon ID 116 const icon = request.file('icon', {
124 let iconId; 117 types: ['image'],
125 do { 118 size: '2mb',
126 iconId = uuid() + uuid();
127 } while (fs.existsSync(path.join(Env.get('USER_PATH'), 'icons', iconId)));
128 iconId = `${iconId}.${icon.extname}`;
129
130 await icon.move(path.join(Env.get('USER_PATH'), 'icons'), {
131 name: iconId,
132 overwrite: true,
133 }); 119 });
134 120
135 if (!icon.moved()) { 121 const iconId = await moveIcon(icon);
122 if (iconId === '-1') {
136 return response.status(500).send(icon.error()); 123 return response.status(500).send(icon.error());
137 } 124 }
138 125
@@ -205,19 +192,6 @@ class ServiceController {
205 }); 192 });
206 } 193 }
207 194
208 async icon({ params, response }) {
209 const { id } = params;
210
211 const iconPath = path.join(Env.get('USER_PATH'), 'icons', id);
212 if (!fs.existsSync(iconPath)) {
213 return response.status(404).send({
214 status: "Icon doesn't exist",
215 });
216 }
217
218 return response.download(iconPath);
219 }
220
221 async reorder({ request, response }) { 195 async reorder({ request, response }) {
222 const data = request.all(); 196 const data = request.all();
223 197