aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/server/ServerApi.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/server/ServerApi.js')
-rw-r--r--src/api/server/ServerApi.js76
1 files changed, 71 insertions, 5 deletions
diff --git a/src/api/server/ServerApi.js b/src/api/server/ServerApi.js
index f25f02eaa..d37ff51f8 100644
--- a/src/api/server/ServerApi.js
+++ b/src/api/server/ServerApi.js
@@ -12,6 +12,8 @@ import NewsModel from '../../models/News';
12import UserModel from '../../models/User'; 12import UserModel from '../../models/User';
13import OrderModel from '../../models/Order'; 13import OrderModel from '../../models/Order';
14 14
15import { sleep } from '../../helpers/async-helpers';
16
15import { API } from '../../environment'; 17import { API } from '../../environment';
16 18
17import { 19import {
@@ -20,6 +22,10 @@ import {
20 loadRecipeConfig, 22 loadRecipeConfig,
21} from '../../helpers/recipe-helpers'; 23} from '../../helpers/recipe-helpers';
22 24
25import {
26 removeServicePartitionDirectory,
27} from '../../helpers/service-helpers.js';
28
23module.paths.unshift( 29module.paths.unshift(
24 getDevRecipeDirectory(), 30 getDevRecipeDirectory(),
25 getRecipeDirectory(), 31 getRecipeDirectory(),
@@ -125,6 +131,19 @@ export default class ServerApi {
125 return user; 131 return user;
126 } 132 }
127 133
134 async deleteAccount() {
135 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, this._prepareAuthRequest({
136 method: 'DELETE',
137 }));
138 if (!request.ok) {
139 throw request;
140 }
141 const data = await request.json();
142
143 console.debug('ServerApi::deleteAccount resolves', data);
144 return data;
145 }
146
128 // Services 147 // Services
129 async getServices() { 148 async getServices() {
130 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/services`, this._prepareAuthRequest({ 149 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/services`, this._prepareAuthRequest({
@@ -152,27 +171,65 @@ export default class ServerApi {
152 throw request; 171 throw request;
153 } 172 }
154 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
155 const service = Object.assign(serviceData, { data: await this._prepareServiceModel(serviceData.data) }); 181 const service = Object.assign(serviceData, { data: await this._prepareServiceModel(serviceData.data) });
156 182
157 console.debug('ServerApi::createService resolves', service); 183 console.debug('ServerApi::createService resolves', service);
158 return service; 184 return service;
159 } 185 }
160 186
161 async updateService(recipeId, data) { 187 async updateService(serviceId, rawData) {
162 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({
163 method: 'PUT', 195 method: 'PUT',
164 body: JSON.stringify(data), 196 body: JSON.stringify(data),
165 })); 197 }));
198
166 if (!request.ok) { 199 if (!request.ok) {
167 throw request; 200 throw request;
168 } 201 }
202
169 const serviceData = await request.json(); 203 const serviceData = await request.json();
204
170 const service = Object.assign(serviceData, { data: await this._prepareServiceModel(serviceData.data) }); 205 const service = Object.assign(serviceData, { data: await this._prepareServiceModel(serviceData.data) });
171 206
172 console.debug('ServerApi::updateService resolves', service); 207 console.debug('ServerApi::updateService resolves', service);
173 return service; 208 return service;
174 } 209 }
175 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
176 async reorderService(data) { 233 async reorderService(data) {
177 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({
178 method: 'PUT', 235 method: 'PUT',
@@ -195,6 +252,8 @@ export default class ServerApi {
195 } 252 }
196 const data = await request.json(); 253 const data = await request.json();
197 254
255 removeServicePartitionDirectory(id, true);
256
198 console.debug('ServerApi::deleteService resolves', data); 257 console.debug('ServerApi::deleteService resolves', data);
199 return data; 258 return data;
200 } 259 }
@@ -290,18 +349,25 @@ export default class ServerApi {
290 349
291 fs.ensureDirSync(recipeTempDirectory); 350 fs.ensureDirSync(recipeTempDirectory);
292 const res = await fetch(packageUrl); 351 const res = await fetch(packageUrl);
352 console.debug('Recipe downloaded', recipeId);
293 const buffer = await res.buffer(); 353 const buffer = await res.buffer();
294 fs.writeFileSync(archivePath, buffer); 354 fs.writeFileSync(archivePath, buffer);
295 355
296 tar.x({ 356 await sleep(10);
357
358 await tar.x({
297 file: archivePath, 359 file: archivePath,
298 cwd: recipeTempDirectory, 360 cwd: recipeTempDirectory,
299 sync: true, 361 preservePaths: true,
362 unlink: true,
363 preserveOwner: false,
364 onwarn: x => console.log('warn', recipeId, x),
300 }); 365 });
301 366
367 await sleep(10);
368
302 const { id } = fs.readJsonSync(path.join(recipeTempDirectory, 'package.json')); 369 const { id } = fs.readJsonSync(path.join(recipeTempDirectory, 'package.json'));
303 const recipeDirectory = path.join(recipesDirectory, id); 370 const recipeDirectory = path.join(recipesDirectory, id);
304
305 fs.copySync(recipeTempDirectory, recipeDirectory); 371 fs.copySync(recipeTempDirectory, recipeDirectory);
306 fs.remove(recipeTempDirectory); 372 fs.remove(recipeTempDirectory);
307 fs.remove(path.join(recipesDirectory, recipeId, 'recipe.tar.gz')); 373 fs.remove(path.join(recipesDirectory, recipeId, 'recipe.tar.gz'));