aboutsummaryrefslogtreecommitdiffstats
path: root/src/internal-server/app/Controllers/Http/RecipeController.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/internal-server/app/Controllers/Http/RecipeController.js')
-rw-r--r--src/internal-server/app/Controllers/Http/RecipeController.js51
1 files changed, 24 insertions, 27 deletions
diff --git a/src/internal-server/app/Controllers/Http/RecipeController.js b/src/internal-server/app/Controllers/Http/RecipeController.js
index 1a7595a9d..2c7baf2a4 100644
--- a/src/internal-server/app/Controllers/Http/RecipeController.js
+++ b/src/internal-server/app/Controllers/Http/RecipeController.js
@@ -1,8 +1,6 @@
1const Recipe = use('App/Models/Recipe'); 1const Recipe = use('App/Models/Recipe');
2const Drive = use('Drive'); 2const Drive = use('Drive');
3const { 3const { validateAll } = use('Validator');
4 validateAll,
5} = use('Validator');
6const Env = use('Env'); 4const Env = use('Env');
7 5
8const fetch = require('node-fetch'); 6const fetch = require('node-fetch');
@@ -14,9 +12,7 @@ const RECIPES_URL = `${LIVE_FERDI_API}/${API_VERSION}/recipes`;
14 12
15class RecipeController { 13class RecipeController {
16 // List official and custom recipes 14 // List official and custom recipes
17 async list({ 15 async list({ response }) {
18 response,
19 }) {
20 const officialRecipes = JSON.parse(await (await fetch(RECIPES_URL)).text()); 16 const officialRecipes = JSON.parse(await (await fetch(RECIPES_URL)).text());
21 const customRecipesArray = (await Recipe.all()).rows; 17 const customRecipesArray = (await Recipe.all()).rows;
22 const customRecipes = customRecipesArray.map(recipe => ({ 18 const customRecipes = customRecipesArray.map(recipe => ({
@@ -25,19 +21,13 @@ class RecipeController {
25 ...JSON.parse(recipe.data), 21 ...JSON.parse(recipe.data),
26 })); 22 }));
27 23
28 const recipes = [ 24 const recipes = [...officialRecipes, ...customRecipes];
29 ...officialRecipes,
30 ...customRecipes,
31 ];
32 25
33 return response.send(recipes); 26 return response.send(recipes);
34 } 27 }
35 28
36 // Search official and custom recipes 29 // Search official and custom recipes
37 async search({ 30 async search({ request, response }) {
38 request,
39 response,
40 }) {
41 // Validate user input 31 // Validate user input
42 const validation = await validateAll(request.all(), { 32 const validation = await validateAll(request.all(), {
43 needle: 'required', 33 needle: 'required',
@@ -64,13 +54,23 @@ class RecipeController {
64 })); 54 }));
65 } else { 55 } else {
66 let remoteResults = []; 56 let remoteResults = [];
67 if (Env.get('CONNECT_WITH_FRANZ') == 'true') { // eslint-disable-line eqeqeq 57 // eslint-disable-next-line eqeqeq
68 remoteResults = JSON.parse(await (await fetch(`${RECIPES_URL}/search?needle=${encodeURIComponent(needle)}`)).text()); 58 if (Env.get('CONNECT_WITH_FRANZ') == 'true') {
59 // eslint-disable-line eqeqeq
60 remoteResults = JSON.parse(
61 await (
62 await fetch(
63 `${RECIPES_URL}/search?needle=${encodeURIComponent(needle)}`,
64 )
65 ).text(),
66 );
69 } 67 }
70 68
71 debug('remoteResults:', remoteResults); 69 debug('remoteResults:', remoteResults);
72 70
73 const localResultsArray = (await Recipe.query().where('name', 'LIKE', `%${needle}%`).fetch()).toJSON(); 71 const localResultsArray = (
72 await Recipe.query().where('name', 'LIKE', `%${needle}%`).fetch()
73 ).toJSON();
74 const localResults = localResultsArray.map(recipe => ({ 74 const localResults = localResultsArray.map(recipe => ({
75 id: recipe.recipeId, 75 id: recipe.recipeId,
76 name: recipe.name, 76 name: recipe.name,
@@ -79,20 +79,14 @@ class RecipeController {
79 79
80 debug('localResults:', localResults); 80 debug('localResults:', localResults);
81 81
82 results = [ 82 results = [...localResults, ...(remoteResults || [])];
83 ...localResults,
84 ...remoteResults || [],
85 ];
86 } 83 }
87 84
88 return response.send(results); 85 return response.send(results);
89 } 86 }
90 87
91 // Download a recipe 88 // Download a recipe
92 async download({ 89 async download({ response, params }) {
93 response,
94 params,
95 }) {
96 // Validate user input 90 // Validate user input
97 const validation = await validateAll(params, { 91 const validation = await validateAll(params, {
98 recipe: 'required|accepted', 92 recipe: 'required|accepted',
@@ -108,14 +102,17 @@ class RecipeController {
108 const service = params.recipe; 102 const service = params.recipe;
109 103
110 // Check for invalid characters 104 // Check for invalid characters
111 if (/\.{1,}/.test(service) || /\/{1,}/.test(service)) { 105 if (/\.+/.test(service) || /\/+/.test(service)) {
112 return response.send('Invalid recipe name'); 106 return response.send('Invalid recipe name');
113 } 107 }
114 108
115 // Check if recipe exists in recipes folder 109 // Check if recipe exists in recipes folder
116 if (await Drive.exists(`${service}.tar.gz`)) { 110 if (await Drive.exists(`${service}.tar.gz`)) {
117 return response.send(await Drive.get(`${service}.tar.gz`)); 111 return response.send(await Drive.get(`${service}.tar.gz`));
118 } if (Env.get('CONNECT_WITH_FRANZ') == 'true') { // eslint-disable-line eqeqeq 112 }
113 // eslint-disable-next-line eqeqeq
114 if (Env.get('CONNECT_WITH_FRANZ') == 'true') {
115 // eslint-disable-line eqeqeq
119 return response.redirect(`${RECIPES_URL}/download/${service}`); 116 return response.redirect(`${RECIPES_URL}/download/${service}`);
120 } 117 }
121 return response.status(400).send({ 118 return response.status(400).send({