aboutsummaryrefslogtreecommitdiffstats
path: root/src/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/api')
-rw-r--r--src/api/LocalApi.js8
-rw-r--r--src/api/ServicesApi.js7
-rw-r--r--src/api/server/LocalApi.js34
-rw-r--r--src/api/server/ServerApi.js48
4 files changed, 94 insertions, 3 deletions
diff --git a/src/api/LocalApi.js b/src/api/LocalApi.js
index 6f2b049d6..3f84f8a0b 100644
--- a/src/api/LocalApi.js
+++ b/src/api/LocalApi.js
@@ -15,4 +15,12 @@ export default class LocalApi {
15 removeKey(key) { 15 removeKey(key) {
16 return this.local.removeKey(key); 16 return this.local.removeKey(key);
17 } 17 }
18
19 getAppCacheSize() {
20 return this.local.getAppCacheSize();
21 }
22
23 clearAppCache() {
24 return this.local.clearAppCache();
25 }
18} 26}
diff --git a/src/api/ServicesApi.js b/src/api/ServicesApi.js
index 3cb40ba0d..36ed9482f 100644
--- a/src/api/ServicesApi.js
+++ b/src/api/ServicesApi.js
@@ -1,5 +1,6 @@
1export default class ServicesApi { 1export default class ServicesApi {
2 constructor(server) { 2 constructor(server, local) {
3 this.local = local;
3 this.server = server; 4 this.server = server;
4 } 5 }
5 6
@@ -30,4 +31,8 @@ export default class ServicesApi {
30 reorder(data) { 31 reorder(data) {
31 return this.server.reorderService(data); 32 return this.server.reorderService(data);
32 } 33 }
34
35 clearCache(serviceId) {
36 return this.local.clearCache(serviceId);
37 }
33} 38}
diff --git a/src/api/server/LocalApi.js b/src/api/server/LocalApi.js
index 79ac6e12f..e95d750ac 100644
--- a/src/api/server/LocalApi.js
+++ b/src/api/server/LocalApi.js
@@ -1,3 +1,10 @@
1import { remote } from 'electron';
2import du from 'du';
3
4import { getServicePartitionsDirectory } from '../../helpers/service-helpers.js';
5
6const { session } = remote;
7
1export default class LocalApi { 8export default class LocalApi {
2 // App 9 // App
3 async updateAppSettings(data) { 10 async updateAppSettings(data) {
@@ -30,4 +37,31 @@ export default class LocalApi {
30 localStorage.setItem('app', JSON.stringify(settings)); 37 localStorage.setItem('app', JSON.stringify(settings));
31 } 38 }
32 } 39 }
40
41 // Services
42 async getAppCacheSize() {
43 const partitionsDir = getServicePartitionsDirectory();
44 return new Promise((resolve, reject) => {
45 du(partitionsDir, (err, size) => {
46 if (err) reject(err);
47
48 console.debug('LocalApi::getAppCacheSize resolves', size);
49 resolve(size);
50 });
51 });
52 }
53
54 async clearCache(serviceId) {
55 const s = session.fromPartition(`persist:service-${serviceId}`);
56
57 console.debug('LocalApi::clearCache resolves', serviceId);
58 return new Promise(resolve => s.clearCache(resolve));
59 }
60
61 async clearAppCache() {
62 const s = session.defaultSession;
63
64 console.debug('LocalApi::clearCache clearAppCache');
65 return new Promise(resolve => s.clearCache(resolve));
66 }
33} 67}
diff --git a/src/api/server/ServerApi.js b/src/api/server/ServerApi.js
index 8b3136d27..d37ff51f8 100644
--- a/src/api/server/ServerApi.js
+++ b/src/api/server/ServerApi.js
@@ -22,6 +22,10 @@ import {
22 loadRecipeConfig, 22 loadRecipeConfig,
23} from '../../helpers/recipe-helpers'; 23} from '../../helpers/recipe-helpers';
24 24
25import {
26 removeServicePartitionDirectory,
27} from '../../helpers/service-helpers.js';
28
25module.paths.unshift( 29module.paths.unshift(
26 getDevRecipeDirectory(), 30 getDevRecipeDirectory(),
27 getRecipeDirectory(), 31 getRecipeDirectory(),
@@ -167,27 +171,65 @@ export default class ServerApi {
167 throw request; 171 throw request;
168 } 172 }
169 const serviceData = await request.json(); 173 const serviceData = await request.json();
174
175 if (data.iconFile) {
176 const iconData = await this.uploadServiceIcon(serviceData.data.id, data.iconFile);
177
178 serviceData.data = iconData;
179 }
180
170 const service = Object.assign(serviceData, { data: await this._prepareServiceModel(serviceData.data) }); 181 const service = Object.assign(serviceData, { data: await this._prepareServiceModel(serviceData.data) });
171 182
172 console.debug('ServerApi::createService resolves', service); 183 console.debug('ServerApi::createService resolves', service);
173 return service; 184 return service;
174 } 185 }
175 186
176 async updateService(recipeId, data) { 187 async updateService(serviceId, rawData) {
177 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${recipeId}`, this._prepareAuthRequest({ 188 const data = rawData;
189
190 if (data.iconFile) {
191 await this.uploadServiceIcon(serviceId, data.iconFile);
192 }
193
194 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${serviceId}`, this._prepareAuthRequest({
178 method: 'PUT', 195 method: 'PUT',
179 body: JSON.stringify(data), 196 body: JSON.stringify(data),
180 })); 197 }));
198
181 if (!request.ok) { 199 if (!request.ok) {
182 throw request; 200 throw request;
183 } 201 }
202
184 const serviceData = await request.json(); 203 const serviceData = await request.json();
204
185 const service = Object.assign(serviceData, { data: await this._prepareServiceModel(serviceData.data) }); 205 const service = Object.assign(serviceData, { data: await this._prepareServiceModel(serviceData.data) });
186 206
187 console.debug('ServerApi::updateService resolves', service); 207 console.debug('ServerApi::updateService resolves', service);
188 return service; 208 return service;
189 } 209 }
190 210
211 async uploadServiceIcon(serviceId, icon) {
212 const formData = new FormData();
213 formData.append('icon', icon);
214
215 const requestData = this._prepareAuthRequest({
216 method: 'PUT',
217 body: formData,
218 });
219
220 delete requestData.headers['Content-Type'];
221
222 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${serviceId}`, requestData);
223
224 if (!request.ok) {
225 throw request;
226 }
227
228 const serviceData = await request.json();
229
230 return serviceData.data;
231 }
232
191 async reorderService(data) { 233 async reorderService(data) {
192 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/reorder`, this._prepareAuthRequest({ 234 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/reorder`, this._prepareAuthRequest({
193 method: 'PUT', 235 method: 'PUT',
@@ -210,6 +252,8 @@ export default class ServerApi {
210 } 252 }
211 const data = await request.json(); 253 const data = await request.json();
212 254
255 removeServicePartitionDirectory(id, true);
256
213 console.debug('ServerApi::deleteService resolves', data); 257 console.debug('ServerApi::deleteService resolves', data);
214 return data; 258 return data;
215 } 259 }