diff options
Diffstat (limited to 'app/Controllers/Http/RecipeController.js')
-rw-r--r-- | app/Controllers/Http/RecipeController.js | 110 |
1 files changed, 93 insertions, 17 deletions
diff --git a/app/Controllers/Http/RecipeController.js b/app/Controllers/Http/RecipeController.js index 0b9d488..641f4ef 100644 --- a/app/Controllers/Http/RecipeController.js +++ b/app/Controllers/Http/RecipeController.js | |||
@@ -1,27 +1,103 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const Recipe = use('App/Models/Recipe'); | 3 | const Recipe = use('App/Models/Recipe'); |
4 | const Helpers = use('Helpers') | ||
5 | const Drive = use('Drive') | ||
4 | const fetch = require('node-fetch'); | 6 | const fetch = require('node-fetch'); |
7 | const path = require('path'); | ||
5 | 8 | ||
6 | class RecipeController { | 9 | class RecipeController { |
7 | async list({ | 10 | async list({ |
8 | response | 11 | response |
9 | }) { | 12 | }) { |
10 | const officialRecipes = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes')).text()); | 13 | const officialRecipes = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes')).text()); |
11 | const customRecipesArray = (await Recipe.all()).rows; | 14 | const customRecipesArray = (await Recipe.all()).rows; |
12 | const customRecipes = customRecipesArray.map(recipe => ({ | 15 | const customRecipes = customRecipesArray.map(recipe => ({ |
13 | "id": recipe.recipeId, | 16 | "id": recipe.recipeId, |
14 | "name": recipe.name, | 17 | "name": recipe.name, |
15 | ...JSON.parse(recipe.data) | 18 | ...JSON.parse(recipe.data) |
16 | })) | 19 | })) |
17 | 20 | ||
18 | const recipes = [ | 21 | const recipes = [ |
19 | ...officialRecipes, | 22 | ...officialRecipes, |
20 | ...customRecipes, | 23 | ...customRecipes, |
21 | ] | 24 | ] |
22 | 25 | ||
23 | return response.send(recipes) | 26 | return response.send(recipes) |
27 | } | ||
28 | |||
29 | async create({ | ||
30 | request, | ||
31 | response | ||
32 | }) { | ||
33 | const data = request.all(); | ||
34 | |||
35 | const pkg = request.file('package') | ||
36 | |||
37 | await pkg.move(path.join(Helpers.appRoot(), '/recipes/'), { | ||
38 | name: data.id + '.tar.gz', | ||
39 | overwrite: false | ||
40 | }) | ||
41 | |||
42 | await Recipe.create({ | ||
43 | name: data.name, | ||
44 | recipeId: data.id, | ||
45 | data: JSON.stringify({ | ||
46 | "author": data.author, | ||
47 | "featured": false, | ||
48 | "version": "1.0.0", | ||
49 | "icons": { | ||
50 | "png": data.png, | ||
51 | "svg": data.svg | ||
52 | } | ||
53 | }) | ||
54 | }) | ||
55 | |||
56 | return response.send('Created new recipe') | ||
57 | } | ||
58 | |||
59 | async search({ | ||
60 | request, | ||
61 | response | ||
62 | }) { | ||
63 | const needle = request.input('needle') | ||
64 | |||
65 | const remoteResults = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes/search?needle=' + needle)).text()); | ||
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 | const results = [ | ||
74 | ...localResults, | ||
75 | ...remoteResults, | ||
76 | ] | ||
77 | |||
78 | return response.send(results); | ||
79 | } | ||
80 | |||
81 | // Download a recipe | ||
82 | async download({ | ||
83 | request, | ||
84 | response, | ||
85 | params | ||
86 | }) { | ||
87 | const service = params.recipe; | ||
88 | |||
89 | // Chack for invalid characters | ||
90 | if (/\.{1,}/.test(service) || /\/{1,}/.test(service)) { | ||
91 | return response.send('Invalid recipe name'); | ||
92 | } | ||
93 | |||
94 | // Check if recipe exists in recipes folder | ||
95 | if (await Drive.exists(service + '.tar.gz')) { | ||
96 | response.send(await Drive.get(service + '.tar.gz')) | ||
97 | } else { | ||
98 | response.redirect('https://api.franzinfra.com/v1/recipes/download/' + service) | ||
24 | } | 99 | } |
100 | } | ||
25 | } | 101 | } |
26 | 102 | ||
27 | module.exports = RecipeController | 103 | module.exports = RecipeController |