summaryrefslogtreecommitdiffstats
path: root/src/internal-server/app/ImageHelper.js
blob: b399d08c4931f32f07b849b7c4e6032c055c9f9b (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
const Env = use('Env');

const { v4: uuid } = require('uuid');

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

module.exports = 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;
};