diff options
author | vantezzen <hello@vantezzen.io> | 2019-09-25 10:45:41 +0200 |
---|---|---|
committer | vantezzen <hello@vantezzen.io> | 2019-09-25 10:45:41 +0200 |
commit | 64ab9231042408a7eeb174e30e598e42d09c6c29 (patch) | |
tree | 6ddd0df2906df6fe515c87517d9b0d3931567bf1 /src/api | |
parent | 5.3.4-beta.2 (diff) | |
download | ferdium-app-64ab9231042408a7eeb174e30e598e42d09c6c29.tar.gz ferdium-app-64ab9231042408a7eeb174e30e598e42d09c6c29.tar.zst ferdium-app-64ab9231042408a7eeb174e30e598e42d09c6c29.zip |
Shipping recipe files directly with Ferdi
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/server/ServerApi.js | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/src/api/server/ServerApi.js b/src/api/server/ServerApi.js index 3d973e586..663c2716f 100644 --- a/src/api/server/ServerApi.js +++ b/src/api/server/ServerApi.js | |||
@@ -13,6 +13,7 @@ import UserModel from '../../models/User'; | |||
13 | import OrderModel from '../../models/Order'; | 13 | import OrderModel from '../../models/Order'; |
14 | 14 | ||
15 | import { sleep } from '../../helpers/async-helpers'; | 15 | import { sleep } from '../../helpers/async-helpers'; |
16 | import { asarPath } from '../../helpers/asar-helpers'; | ||
16 | 17 | ||
17 | import { API } from '../../environment'; | 18 | import { API } from '../../environment'; |
18 | import apiBase from '../apiBase'; | 19 | import apiBase from '../apiBase'; |
@@ -369,14 +370,30 @@ export default class ServerApi { | |||
369 | try { | 370 | try { |
370 | const recipesDirectory = path.join(app.getPath('userData'), 'recipes'); | 371 | const recipesDirectory = path.join(app.getPath('userData'), 'recipes'); |
371 | const recipeTempDirectory = path.join(recipesDirectory, 'temp', recipeId); | 372 | const recipeTempDirectory = path.join(recipesDirectory, 'temp', recipeId); |
372 | const archivePath = path.join(recipeTempDirectory, 'recipe.tar.gz'); | 373 | const tempArchivePath = path.join(recipeTempDirectory, 'recipe.tar.gz'); |
373 | const packageUrl = `${apiBase()}/recipes/download/${recipeId}`; | 374 | |
375 | const internalRecipesDirectory = asarPath(path.join(__dirname, '../../', 'recipes')); | ||
376 | const internalRecipeFile = path.join(internalRecipesDirectory, `${recipeId}.tar.gz`); | ||
374 | 377 | ||
375 | fs.ensureDirSync(recipeTempDirectory); | 378 | fs.ensureDirSync(recipeTempDirectory); |
376 | const res = await fetch(packageUrl); | 379 | |
377 | debug('Recipe downloaded', recipeId); | 380 | let archivePath; |
378 | const buffer = await res.buffer(); | 381 | |
379 | fs.writeFileSync(archivePath, buffer); | 382 | if (await fs.exists(internalRecipeFile)) { |
383 | console.log('[ServerApi::getRecipePackage] Using internal recipe file'); | ||
384 | archivePath = internalRecipeFile; | ||
385 | } else { | ||
386 | console.log('[ServerApi::getRecipePackage] Downloading recipe from server'); | ||
387 | archivePath = tempArchivePath; | ||
388 | |||
389 | const packageUrl = `${apiBase()}/recipes/download/${recipeId}`; | ||
390 | |||
391 | const res = await fetch(packageUrl); | ||
392 | debug('Recipe downloaded', recipeId); | ||
393 | const buffer = await res.buffer(); | ||
394 | fs.writeFileSync(archivePath, buffer); | ||
395 | } | ||
396 | console.log(archivePath); | ||
380 | 397 | ||
381 | await sleep(10); | 398 | await sleep(10); |
382 | 399 | ||