summaryrefslogtreecommitdiffstats
path: root/src/internal-server/app/ImageHelper.js
blob: fba6100691918574164d74ccdbb1f15de886133f (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 { v4: uuid } = require('uuid');

const path = require('path');
const fs = require('fs-extra');
const { API_VERSION } = require('../../environment-remote');

const moveIcon = async icon => {
  const iconsPath = path.join(Env.get('USER_PATH'), 'icons');
  await fs.ensureDir(iconsPath);

  // Generate new icon ID
  let iconId;
  do {
    iconId = uuid() + uuid();
  } while (fs.existsSync(path.join(iconsPath, iconId)));
  iconId = `${iconId}.${icon.extname}`;

  await icon.move(iconsPath, {
    name: iconId,
    overwrite: true,
  });
  return icon.moved() ? iconId : '-1';
};

const deduceIconUrl = iconId =>
  iconId
    ? `http://${Env.get('HOST')}:${Env.get(
        'PORT',
      )}/${API_VERSION}/icon/${iconId}`
    : null;

module.exports = { moveIcon, deduceIconUrl };