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

const path = require('node: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 (error) {
      console.log(error);
      // File not available.
      return response.status(404).send({
        status: "Icon doesn't exist",
      });
    }

    return response.download(iconPath);
  }
}

module.exports = ImageController;