diff options
author | vantezzen <hello@vantezzen.io> | 2019-10-18 21:43:42 +0200 |
---|---|---|
committer | vantezzen <hello@vantezzen.io> | 2019-10-18 21:43:42 +0200 |
commit | 1cfff4a4324e130aa7579ea8694438ade686dd55 (patch) | |
tree | 7f50f0142283aa1f1e41084dd8efa243ba91b8c0 /src/server/app/Controllers/Http/RecipeController.js | |
parent | Fix lint (diff) | |
download | ferdium-app-1cfff4a4324e130aa7579ea8694438ade686dd55.tar.gz ferdium-app-1cfff4a4324e130aa7579ea8694438ade686dd55.tar.zst ferdium-app-1cfff4a4324e130aa7579ea8694438ade686dd55.zip |
Move internal server to submodule
Diffstat (limited to 'src/server/app/Controllers/Http/RecipeController.js')
-rw-r--r-- | src/server/app/Controllers/Http/RecipeController.js | 119 |
1 files changed, 0 insertions, 119 deletions
diff --git a/src/server/app/Controllers/Http/RecipeController.js b/src/server/app/Controllers/Http/RecipeController.js deleted file mode 100644 index 71ac12c0f..000000000 --- a/src/server/app/Controllers/Http/RecipeController.js +++ /dev/null | |||
@@ -1,119 +0,0 @@ | |||
1 | |||
2 | const Recipe = use('App/Models/Recipe'); | ||
3 | const Drive = use('Drive'); | ||
4 | const { | ||
5 | validateAll, | ||
6 | } = use('Validator'); | ||
7 | const Env = use('Env'); | ||
8 | |||
9 | const fetch = require('node-fetch'); | ||
10 | |||
11 | class RecipeController { | ||
12 | // List official and custom recipes | ||
13 | async list({ | ||
14 | response, | ||
15 | }) { | ||
16 | const officialRecipes = JSON.parse(await (await fetch('https://api.getferdi.com/v1/recipes')).text()); | ||
17 | const customRecipesArray = (await Recipe.all()).rows; | ||
18 | const customRecipes = customRecipesArray.map(recipe => ({ | ||
19 | id: recipe.recipeId, | ||
20 | name: recipe.name, | ||
21 | ...JSON.parse(recipe.data), | ||
22 | })); | ||
23 | |||
24 | const recipes = [ | ||
25 | ...officialRecipes, | ||
26 | ...customRecipes, | ||
27 | ]; | ||
28 | |||
29 | return response.send(recipes); | ||
30 | } | ||
31 | |||
32 | // Search official and custom recipes | ||
33 | async search({ | ||
34 | request, | ||
35 | response, | ||
36 | }) { | ||
37 | // Validate user input | ||
38 | const validation = await validateAll(request.all(), { | ||
39 | needle: 'required', | ||
40 | }); | ||
41 | if (validation.fails()) { | ||
42 | return response.status(401).send({ | ||
43 | message: 'Please provide a needle', | ||
44 | messages: validation.messages(), | ||
45 | status: 401, | ||
46 | }); | ||
47 | } | ||
48 | |||
49 | const needle = request.input('needle'); | ||
50 | |||
51 | // Get results | ||
52 | let results; | ||
53 | |||
54 | if (needle === 'ferdi:custom') { | ||
55 | const dbResults = (await Recipe.all()).toJSON(); | ||
56 | results = dbResults.map(recipe => ({ | ||
57 | id: recipe.recipeId, | ||
58 | name: recipe.name, | ||
59 | ...JSON.parse(recipe.data), | ||
60 | })); | ||
61 | } else { | ||
62 | let remoteResults = []; | ||
63 | if (Env.get('CONNECT_WITH_FRANZ') == 'true') { // eslint-disable-line eqeqeq | ||
64 | remoteResults = JSON.parse(await (await fetch(`https://api.getferdi.com/v1/recipes/search?needle=${encodeURIComponent(needle)}`)).text()); | ||
65 | } | ||
66 | const localResultsArray = (await Recipe.query().where('name', 'LIKE', `%${needle}%`).fetch()).toJSON(); | ||
67 | const localResults = localResultsArray.map(recipe => ({ | ||
68 | id: recipe.recipeId, | ||
69 | name: recipe.name, | ||
70 | ...JSON.parse(recipe.data), | ||
71 | })); | ||
72 | |||
73 | results = [ | ||
74 | ...localResults, | ||
75 | ...remoteResults || [], | ||
76 | ]; | ||
77 | } | ||
78 | |||
79 | return response.send(results); | ||
80 | } | ||
81 | |||
82 | // Download a recipe | ||
83 | async download({ | ||
84 | response, | ||
85 | params, | ||
86 | }) { | ||
87 | // Validate user input | ||
88 | const validation = await validateAll(params, { | ||
89 | recipe: 'required|accepted', | ||
90 | }); | ||
91 | if (validation.fails()) { | ||
92 | return response.status(401).send({ | ||
93 | message: 'Please provide a recipe ID', | ||
94 | messages: validation.messages(), | ||
95 | status: 401, | ||
96 | }); | ||
97 | } | ||
98 | |||
99 | const service = params.recipe; | ||
100 | |||
101 | // Check for invalid characters | ||
102 | if (/\.{1,}/.test(service) || /\/{1,}/.test(service)) { | ||
103 | return response.send('Invalid recipe name'); | ||
104 | } | ||
105 | |||
106 | // Check if recipe exists in recipes folder | ||
107 | if (await Drive.exists(`${service}.tar.gz`)) { | ||
108 | return response.send(await Drive.get(`${service}.tar.gz`)); | ||
109 | } if (Env.get('CONNECT_WITH_FRANZ') == 'true') { // eslint-disable-line eqeqeq | ||
110 | return response.redirect(`https://api.getferdi.com/v1/recipes/download/${service}`); | ||
111 | } | ||
112 | return response.status(400).send({ | ||
113 | message: 'Recipe not found', | ||
114 | code: 'recipe-not-found', | ||
115 | }); | ||
116 | } | ||
117 | } | ||
118 | |||
119 | module.exports = RecipeController; | ||