aboutsummaryrefslogtreecommitdiffstats
path: root/src/api
diff options
context:
space:
mode:
authorLibravatar kytwb <kytwb@pm.me>2022-01-07 12:50:26 +0100
committerLibravatar kytwb <kytwb@pm.me>2022-01-07 12:50:26 +0100
commit1f1b1a9696ccdd2155cece3c5dccdb08c01025fb (patch)
treee907ca44bec689c51aafd476706a75f41f48b4cd /src/api
parentEventually get remote recipes updates in internal server (diff)
downloadferdium-app-1f1b1a9696ccdd2155cece3c5dccdb08c01025fb.tar.gz
ferdium-app-1f1b1a9696ccdd2155cece3c5dccdb08c01025fb.tar.zst
ferdium-app-1f1b1a9696ccdd2155cece3c5dccdb08c01025fb.zip
Remove try/catch in getRecipePackage
Diffstat (limited to 'src/api')
-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 c5c658df4..706520cb3 100644
--- a/src/api/server/ServerApi.ts
+++ b/src/api/server/ServerApi.ts
@@ -390,60 +390,54 @@ export default class ServerApi {
390 } 390 }
391 391
392 async getRecipePackage(recipeId: string) { 392 async getRecipePackage(recipeId: string) {
393 try { 393 const recipesDirectory = userDataRecipesPath();
394 const recipesDirectory = userDataRecipesPath(); 394 const recipeTempDirectory = join(recipesDirectory, 'temp', recipeId);
395 const recipeTempDirectory = join(recipesDirectory, 'temp', recipeId); 395 const tempArchivePath = join(recipeTempDirectory, 'recipe.tar.gz');
396 const tempArchivePath = join(recipeTempDirectory, 'recipe.tar.gz');
397
398 const internalRecipeFile = asarRecipesPath(`${recipeId}.tar.gz`);
399
400 ensureDirSync(recipeTempDirectory);
401 396
402 let archivePath: PathOrFileDescriptor; 397 const internalRecipeFile = asarRecipesPath(`${recipeId}.tar.gz`);
403 398
404 if (pathExistsSync(internalRecipeFile)) { 399 ensureDirSync(recipeTempDirectory);
405 debug('[ServerApi::getRecipePackage] Using internal recipe file');
406 archivePath = internalRecipeFile;
407 } else {
408 debug('[ServerApi::getRecipePackage] Downloading recipe from server');
409 archivePath = tempArchivePath;
410 400
411 const packageUrl = `${apiBase()}/recipes/download/${recipeId}`; 401 let archivePath: PathOrFileDescriptor;
412
413 const res = await window.fetch(packageUrl);
414 debug('Recipe downloaded', recipeId);
415 const blob = await res.blob();
416 const buffer = await blob.arrayBuffer();
417 writeFileSync(tempArchivePath, Buffer.from(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