aboutsummaryrefslogtreecommitdiffstats
path: root/src/internal-server/app/Controllers/Http/ImageController.js
blob: 731f181e0ce875d795543ccfb842745c08bba7f5 (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
const Env = use('Env');

const path = require('path');
const fs = require('fs-extra');
const sanitize = require('sanitize-filename');

class ImageController {
  async icon({ params, response }) {
    let { id } = params;

    id = sanitize(id);
    if (id === '') {
      return response.status(404).send({
        status: "Icon doesn't exist",
      });
    }

    const iconPath = path.join(Env.get('USER_PATH'), 'icons', id);

    try {
      await fs.access(iconPath);
    } catch {
      // File not available.
      return response.status(404).send({
        status: "Icon doesn't exist",
      });
    }

    return response.download(iconPath);
  }
}

module.exports = ImageController;