diff options
Diffstat (limited to 'src/internal-server/app/Controllers/Http')
-rw-r--r-- | src/internal-server/app/Controllers/Http/ImageController.js | 21 | ||||
-rw-r--r-- | src/internal-server/app/Controllers/Http/ServiceController.js | 40 |
2 files changed, 28 insertions, 33 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 @@ | |||
1 | const Env = use('Env'); | ||
2 | |||
3 | const path = require('path'); | ||
4 | const fs = require('fs-extra'); | ||
5 | |||
6 | class 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 | |||
21 | 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'); | |||
3 | const Env = use('Env'); | 3 | const Env = use('Env'); |
4 | 4 | ||
5 | const { v4: uuid } = require('uuid'); | 5 | const { v4: uuid } = require('uuid'); |
6 | const path = require('path'); | ||
7 | const fs = require('fs-extra'); | ||
8 | const { LOCAL_HOSTNAME, DEFAULT_SERVICE_ORDER } = require('../../../../config'); | 6 | const { LOCAL_HOSTNAME, DEFAULT_SERVICE_ORDER } = require('../../../../config'); |
9 | const { API_VERSION } = require('../../../../environment-remote'); | 7 | const { API_VERSION } = require('../../../../environment-remote'); |
8 | const moveIcon = require('../../ImageHelper'); | ||
10 | 9 | ||
11 | const hostname = LOCAL_HOSTNAME; | 10 | const hostname = LOCAL_HOSTNAME; |
12 | const port = Env.get('PORT'); | 11 | const 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 | ||