aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/server/ServerApi.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/server/ServerApi.ts')
-rw-r--r--src/api/server/ServerApi.ts84
1 files changed, 39 insertions, 45 deletions
diff --git a/src/api/server/ServerApi.ts b/src/api/server/ServerApi.ts
index 0144a1069..706520cb3 100644
--- a/src/api/server/ServerApi.ts
+++ b/src/api/server/ServerApi.ts
@@ -13,7 +13,6 @@ import {
13 removeSync, 13 removeSync,
14 PathOrFileDescriptor, 14 PathOrFileDescriptor,
15} from 'fs-extra'; 15} from 'fs-extra';
16import fetch from 'electron-fetch';
17 16
18import ServiceModel from '../../models/Service'; 17import ServiceModel from '../../models/Service';
19import RecipePreviewModel from '../../models/RecipePreview'; 18import RecipePreviewModel from '../../models/RecipePreview';
@@ -391,59 +390,54 @@ export default class ServerApi {
391 } 390 }
392 391
393 async getRecipePackage(recipeId: string) { 392 async getRecipePackage(recipeId: string) {
394 try { 393 const recipesDirectory = userDataRecipesPath();
395 const recipesDirectory = userDataRecipesPath(); 394 const recipeTempDirectory = join(recipesDirectory, 'temp', recipeId);
396 const recipeTempDirectory = join(recipesDirectory, 'temp', recipeId); 395 const tempArchivePath = join(recipeTempDirectory, 'recipe.tar.gz');
397 const tempArchivePath = join(recipeTempDirectory, 'recipe.tar.gz');
398
399 const internalRecipeFile = asarRecipesPath(`${recipeId}.tar.gz`);
400
401 ensureDirSync(recipeTempDirectory);
402 396
403 let archivePath: PathOrFileDescriptor; 397 const internalRecipeFile = asarRecipesPath(`${recipeId}.tar.gz`);
404 398
405 if (pathExistsSync(internalRecipeFile)) { 399 ensureDirSync(recipeTempDirectory);
406 debug('[ServerApi::getRecipePackage] Using internal recipe file');
407 archivePath = internalRecipeFile;
408 } else {
409 debug('[ServerApi::getRecipePackage] Downloading recipe from server');
410 archivePath = tempArchivePath;
411 400
412 const packageUrl = `${apiBase()}/recipes/download/${recipeId}`; 401 let archivePath: PathOrFileDescriptor;
413
414 const res = await fetch(packageUrl);
415 debug('Recipe downloaded', recipeId);
416 const buffer = await res.buffer();
417 writeFileSync(archivePath, buffer);
418 }
419 debug(archivePath);
420 402
421 await sleep(10); 403 if (pathExistsSync(internalRecipeFile)) {
404 debug('[ServerApi::getRecipePackage] Using internal recipe file');
405 archivePath = internalRecipeFile;
406 } else {
407 debug('[ServerApi::getRecipePackage] Downloading recipe from server');
408 archivePath = tempArchivePath;
422 409
423 // @ts-expect-error No overload matches this call. 410 const packageUrl = `${apiBase()}/recipes/download/${recipeId}`;
424 await tar.x({
425 file: archivePath,
426 cwd: recipeTempDirectory,
427 preservePaths: true,
428 unlink: true,
429 preserveOwner: false,
430 onwarn: x => debug('warn', recipeId, x),
431 });
432 411
433 await sleep(10); 412 const res = await window.fetch(packageUrl);
413 debug('Recipe downloaded', recipeId);
414 const blob = await res.blob();
415 const buffer = await blob.arrayBuffer();
416 writeFileSync(tempArchivePath, Buffer.from(buffer));
417 }
418 debug(archivePath);
419
420 await sleep(10);
421
422 // @ts-expect-error No overload matches this call.
423 await tar.x({
424 file: archivePath,
425 cwd: recipeTempDirectory,
426 preservePaths: true,
427 unlink: true,
428 preserveOwner: false,
429 onwarn: x => debug('warn', recipeId, x),
430 });
434 431
435 const { id } = readJsonSync(join(recipeTempDirectory, 'package.json')); 432 await sleep(10);
436 const recipeDirectory = join(recipesDirectory, id);
437 copySync(recipeTempDirectory, recipeDirectory);
438 removeSync(recipeTempDirectory);
439 removeSync(join(recipesDirectory, recipeId, 'recipe.tar.gz'));
440 433
441 return id; 434 const { id } = readJsonSync(join(recipeTempDirectory, 'package.json'));
442 } catch (error) { 435 const recipeDirectory = join(recipesDirectory, id);
443 console.error(error); 436 copySync(recipeTempDirectory, recipeDirectory);
437 removeSync(recipeTempDirectory);
438 removeSync(join(recipesDirectory, recipeId, 'recipe.tar.gz'));
444 439
445 return false; 440 return id;
446 }
447 } 441 }
448 442
449 // Health Check 443 // Health Check