summaryrefslogtreecommitdiffstats
path: root/src/internal-server
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
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')
-rw-r--r--src/internal-server/app/Controllers/Http/ImageController.js21
-rw-r--r--src/internal-server/app/Controllers/Http/ServiceController.js40
-rw-r--r--src/internal-server/app/ImageHelper.js24
-rw-r--r--src/internal-server/start/routes.js2
4 files changed, 53 insertions, 34 deletions
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 @@
1const Env = use('Env');
2
3const path = require('path');
4const fs = require('fs-extra');
5
6class ImageController {
7 async icon({ params, response }) {
8 const { id } = params;
9
10 const iconPath = path.join(Env.get('USER_PATH'), 'icons', id);
11 if (!fs.existsSync(iconPath)) {
12 return response.status(404).send({
13 status: "Icon doesn't exist",
14 });
15 }
16
17 return response.download(iconPath);
18 }
19}
20
21module.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');
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
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 @@
1const Env = use('Env');
2
3const { v4: uuid } = require('uuid');
4
5const path = require('path');
6const fs = require('fs-extra');
7
8module.exports = async (icon) => {
9 const iconsPath = path.join(Env.get('USER_PATH'), 'icons');
10 await fs.ensureDir(iconsPath);
11
12 // Generate new icon ID
13 let iconId;
14 do {
15 iconId = uuid() + uuid();
16 } while (fs.existsSync(path.join(iconsPath, iconId)));
17 iconId = `${iconId}.${icon.extname}`;
18
19 await icon.move(iconsPath, {
20 name: iconId,
21 overwrite: true,
22 });
23 return !icon.moved() ? '-1' : iconId;
24};
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(() => {
66 .middleware(OnlyAllowFerdium); 66 .middleware(OnlyAllowFerdium);
67 67
68Route.group(() => { 68Route.group(() => {
69 Route.get('icon/:id', 'ServiceController.icon'); 69 Route.get('icon/:id', 'ImageController.icon');
70}).prefix(API_VERSION); 70}).prefix(API_VERSION);
71 71
72// Franz account import 72// Franz account import