aboutsummaryrefslogtreecommitdiffstats
path: root/app/Controllers/Http/RecipeController.js
blob: 594c29849e5d67172c99331219998d3c7f866f29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict'

const Recipe = use('App/Models/Recipe');
const Helpers = use('Helpers')
const Drive = use('Drive')
const fetch = require('node-fetch');
const targz = require('targz');
const path = require('path');
const fs = require('fs-extra');

const compress = (src, dest) => {
  return new Promise((resolve, reject) => {
    targz.compress({
      src,
      dest
    }, function (err) {
      if (err) {
        reject(err);
      } else {
        resolve(dest);
      }
    });
  })
}

class RecipeController {
  // List official and custom recipes
  async list({
    response
  }) {
    const officialRecipes = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes')).text());
    const customRecipesArray = (await Recipe.all()).rows;
    const customRecipes = customRecipesArray.map(recipe => ({
      "id": recipe.recipeId,
      "name": recipe.name,
      ...JSON.parse(recipe.data)
    }))

    const recipes = [
      ...officialRecipes,
      ...customRecipes,
    ]

    return response.send(recipes)
  }

  // Create a new recipe using the new.html page
  async create({
    request,
    response
  }) {
    const data = request.all();

    if (!data.id) {
      return response.send('Please provide an ID');
    }

    // Check for invalid characters
    if (/\.{1,}/.test(data.id) || /\/{1,}/.test(data.id)) {
      return response.send('Invalid recipe name. Your recipe name may not contain "." or "/"');
    }

    // Clear temporary recipe folder
    await fs.emptyDir(Helpers.tmpPath('recipe'));

    // Move uploaded files to temporary path
    const files = request.file('files')
    await files.moveAll(Helpers.tmpPath('recipe'))

    // Compress files to .tar.gz file
    const source = Helpers.tmpPath('recipe');
    const destination = path.join(Helpers.appRoot(), '/recipes/' + data.id + '.tar.gz');
    console.log('a', source, destination)
    compress(
      source,
      destination
    );

    // Create recipe in db
    await Recipe.create({
      name: data.name,
      recipeId: data.id,
      data: JSON.stringify({
        "author": data.author,
        "featured": false,
        "version": "1.0.0",
        "icons": {
          "png": data.png,
          "svg": data.svg
        }
      })
    })

    return response.send('Created new recipe')
  }

  // Search official and custom recipes
  async search({
    request,
    response
  }) {
    const needle = request.input('needle')

    // Get results
    const remoteResults = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes/search?needle=' + needle)).text());
    const localResultsArray = (await Recipe.query().where('name', 'LIKE', '%' + needle + '%').fetch()).toJSON();
    const localResults = localResultsArray.map(recipe => ({
      "id": recipe.recipeId,
      "name": recipe.name,
      ...JSON.parse(recipe.data)
    }))

    const results = [
      ...localResults,
      ...remoteResults,
    ]

    return response.send(results);
  }

  // Download a recipe
  async download({
    request,
    response,
    params
  }) {
    const service = params.recipe;

    // Check for invalid characters
    if (/\.{1,}/.test(service) || /\/{1,}/.test(service)) {
      return response.send('Invalid recipe name');
    }

    // Check if recipe exists in recipes folder
    if (await Drive.exists(service + '.tar.gz')) {
      response.send(await Drive.get(service + '.tar.gz'))
    } else {
      response.redirect('https://api.franzinfra.com/v1/recipes/download/' + service)
    }
  }
}

module.exports = RecipeController