aboutsummaryrefslogtreecommitdiffstats
path: root/app/Controllers/Http/RecipeController.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/Controllers/Http/RecipeController.js')
-rw-r--r--app/Controllers/Http/RecipeController.js159
1 files changed, 77 insertions, 82 deletions
diff --git a/app/Controllers/Http/RecipeController.js b/app/Controllers/Http/RecipeController.js
index fd9ed83..217b05b 100644
--- a/app/Controllers/Http/RecipeController.js
+++ b/app/Controllers/Http/RecipeController.js
@@ -1,61 +1,58 @@
1'use strict'
2 1
3const Recipe = use('App/Models/Recipe'); 2const Recipe = use('App/Models/Recipe');
4const Helpers = use('Helpers') 3const Helpers = use('Helpers');
5const Drive = use('Drive') 4const Drive = use('Drive');
6const { 5const {
7 validateAll 6 validateAll,
8} = use('Validator'); 7} = use('Validator');
9const Env = use('Env') 8const Env = use('Env');
10 9
11const fetch = require('node-fetch'); 10const fetch = require('node-fetch');
12const targz = require('targz'); 11const targz = require('targz');
13const path = require('path'); 12const path = require('path');
14const fs = require('fs-extra'); 13const fs = require('fs-extra');
15 14
16const compress = (src, dest) => { 15const compress = (src, dest) => new Promise((resolve, reject) => {
17 return new Promise((resolve, reject) => { 16 targz.compress({
18 targz.compress({ 17 src,
19 src, 18 dest,
20 dest 19 }, (err) => {
21 }, function (err) { 20 if (err) {
22 if (err) { 21 reject(err);
23 reject(err); 22 } else {
24 } else { 23 resolve(dest);
25 resolve(dest); 24 }
26 } 25 });
27 }); 26});
28 })
29}
30 27
31class RecipeController { 28class RecipeController {
32 // List official and custom recipes 29 // List official and custom recipes
33 async list({ 30 async list({
34 response 31 response,
35 }) { 32 }) {
36 const officialRecipes = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes')).text()); 33 const officialRecipes = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes')).text());
37 const customRecipesArray = (await Recipe.all()).rows; 34 const customRecipesArray = (await Recipe.all()).rows;
38 const customRecipes = customRecipesArray.map(recipe => ({ 35 const customRecipes = customRecipesArray.map((recipe) => ({
39 "id": recipe.recipeId, 36 id: recipe.recipeId,
40 "name": recipe.name, 37 name: recipe.name,
41 ...JSON.parse(recipe.data) 38 ...JSON.parse(recipe.data),
42 })) 39 }));
43 40
44 const recipes = [ 41 const recipes = [
45 ...officialRecipes, 42 ...officialRecipes,
46 ...customRecipes, 43 ...customRecipes,
47 ] 44 ];
48 45
49 return response.send(recipes) 46 return response.send(recipes);
50 } 47 }
51 48
52 // Create a new recipe using the new.html page 49 // Create a new recipe using the new.html page
53 async create({ 50 async create({
54 request, 51 request,
55 response 52 response,
56 }) { 53 }) {
57 // Check if recipe creation is enabled 54 // Check if recipe creation is enabled
58 if (Env.get('IS_CREATION_ENABLED') == 'false') { 55 if (Env.get('IS_CREATION_ENABLED') == 'false') { // eslint-disable-line eqeqeq
59 return response.send('This server doesn\'t allow the creation of new recipes.'); 56 return response.send('This server doesn\'t allow the creation of new recipes.');
60 } 57 }
61 58
@@ -69,10 +66,10 @@ class RecipeController {
69 }); 66 });
70 if (validation.fails()) { 67 if (validation.fails()) {
71 return response.status(401).send({ 68 return response.status(401).send({
72 "message": "Invalid POST arguments", 69 message: 'Invalid POST arguments',
73 "messages": validation.messages(), 70 messages: validation.messages(),
74 "status": 401 71 status: 401,
75 }) 72 });
76 } 73 }
77 74
78 const data = request.all(); 75 const data = request.all();
@@ -90,16 +87,16 @@ class RecipeController {
90 await fs.emptyDir(Helpers.tmpPath('recipe')); 87 await fs.emptyDir(Helpers.tmpPath('recipe'));
91 88
92 // Move uploaded files to temporary path 89 // Move uploaded files to temporary path
93 const files = request.file('files') 90 const files = request.file('files');
94 await files.moveAll(Helpers.tmpPath('recipe')) 91 await files.moveAll(Helpers.tmpPath('recipe'));
95 92
96 // Compress files to .tar.gz file 93 // Compress files to .tar.gz file
97 const source = Helpers.tmpPath('recipe'); 94 const source = Helpers.tmpPath('recipe');
98 const destination = path.join(Helpers.appRoot(), '/recipes/' + data.id + '.tar.gz'); 95 const destination = path.join(Helpers.appRoot(), `/recipes/${data.id}.tar.gz`);
99 96
100 compress( 97 compress(
101 source, 98 source,
102 destination 99 destination,
103 ); 100 );
104 101
105 // Create recipe in db 102 // Create recipe in db
@@ -107,74 +104,73 @@ class RecipeController {
107 name: data.name, 104 name: data.name,
108 recipeId: data.id, 105 recipeId: data.id,
109 data: JSON.stringify({ 106 data: JSON.stringify({
110 "author": data.author, 107 author: data.author,
111 "featured": false, 108 featured: false,
112 "version": "1.0.0", 109 version: '1.0.0',
113 "icons": { 110 icons: {
114 "png": data.png, 111 png: data.png,
115 "svg": data.svg 112 svg: data.svg,
116 } 113 },
117 }) 114 }),
118 }) 115 });
119 116
120 return response.send('Created new recipe') 117 return response.send('Created new recipe');
121 } 118 }
122 119
123 // Search official and custom recipes 120 // Search official and custom recipes
124 async search({ 121 async search({
125 request, 122 request,
126 response 123 response,
127 }) { 124 }) {
128 // Validate user input 125 // Validate user input
129 const validation = await validateAll(request.all(), { 126 const validation = await validateAll(request.all(), {
130 needle: 'required' 127 needle: 'required',
131 }); 128 });
132 if (validation.fails()) { 129 if (validation.fails()) {
133 return response.status(401).send({ 130 return response.status(401).send({
134 "message": "Please provide a needle", 131 message: 'Please provide a needle',
135 "messages": validation.messages(), 132 messages: validation.messages(),
136 "status": 401 133 status: 401,
137 }) 134 });
138 } 135 }
139 136
140 const needle = request.input('needle') 137 const needle = request.input('needle');
141 138
142 // Get results 139 // Get results
143 let remoteResults = []; 140 let remoteResults = [];
144 if (Env.get('CONNECT_WITH_FRANZ') == 'true') { 141 if (Env.get('CONNECT_WITH_FRANZ') == 'true') { // eslint-disable-line eqeqeq
145 remoteResults = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes/search?needle=' + encodeURIComponent(needle))).text()); 142 remoteResults = JSON.parse(await (await fetch(`https://api.franzinfra.com/v1/recipes/search?needle=${encodeURIComponent(needle)}`)).text());
146 } 143 }
147 const localResultsArray = (await Recipe.query().where('name', 'LIKE', '%' + needle + '%').fetch()).toJSON(); 144 const localResultsArray = (await Recipe.query().where('name', 'LIKE', `%${needle}%`).fetch()).toJSON();
148 const localResults = localResultsArray.map(recipe => ({ 145 const localResults = localResultsArray.map((recipe) => ({
149 "id": recipe.recipeId, 146 id: recipe.recipeId,
150 "name": recipe.name, 147 name: recipe.name,
151 ...JSON.parse(recipe.data) 148 ...JSON.parse(recipe.data),
152 })) 149 }));
153 150
154 const results = [ 151 const results = [
155 ...localResults, 152 ...localResults,
156 ...remoteResults, 153 ...remoteResults,
157 ] 154 ];
158 155
159 return response.send(results); 156 return response.send(results);
160 } 157 }
161 158
162 // Download a recipe 159 // Download a recipe
163 async download({ 160 async download({
164 request,
165 response, 161 response,
166 params 162 params,
167 }) { 163 }) {
168 // Validate user input 164 // Validate user input
169 const validation = await validateAll(params, { 165 const validation = await validateAll(params, {
170 recipe: 'required|accepted' 166 recipe: 'required|accepted',
171 }); 167 });
172 if (validation.fails()) { 168 if (validation.fails()) {
173 return response.status(401).send({ 169 return response.status(401).send({
174 "message": "Please provide a recipe ID", 170 message: 'Please provide a recipe ID',
175 "messages": validation.messages(), 171 messages: validation.messages(),
176 "status": 401 172 status: 401,
177 }) 173 });
178 } 174 }
179 175
180 const service = params.recipe; 176 const service = params.recipe;
@@ -185,17 +181,16 @@ class RecipeController {
185 } 181 }
186 182
187 // Check if recipe exists in recipes folder 183 // Check if recipe exists in recipes folder
188 if (await Drive.exists(service + '.tar.gz')) { 184 if (await Drive.exists(`${service}.tar.gz`)) {
189 response.send(await Drive.get(service + '.tar.gz')) 185 response.send(await Drive.get(`${service}.tar.gz`));
190 } else if(Env.get('CONNECT_WITH_FRANZ') == 'true') { 186 } else if (Env.get('CONNECT_WITH_FRANZ') == 'true') { // eslint-disable-line eqeqeq
191 response.redirect('https://api.franzinfra.com/v1/recipes/download/' + service) 187 response.redirect(`https://api.franzinfra.com/v1/recipes/download/${service}`);
192 } else {
193 return response.status(400).send({
194 "message": "Recipe not found",
195 "code": "recipe-not-found"
196 })
197 } 188 }
189 return response.status(400).send({
190 message: 'Recipe not found',
191 code: 'recipe-not-found',
192 });
198 } 193 }
199} 194}
200 195
201module.exports = RecipeController 196module.exports = RecipeController;