aboutsummaryrefslogtreecommitdiffstats
path: root/src/internal-server/app/ImageHelper.js
blob: 905ba81abca4e31b118d1416f25947789af0e85c (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() ? '-1' : iconId;
};

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

module.exports = { moveIcon, deduceIconUrl };