File size: 1,019 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
// @flow
import uuidv4 from 'uuid/v4';
import fs from 'fs';

import type { FileUpload, EntityTypes } from 'shared/types';

const STORAGE_DIR = 'public/uploads';
const READ_WRITE_MODE = 0o777;

const dirExists = (path: string): Promise<boolean> =>
  new Promise(res => fs.access(path, fs.constants.F_OK, err => res(!!!err)));

const createUploadsDir = (path: string): Promise<void> =>
  new Promise(res =>
    fs.mkdir(path, READ_WRITE_MODE, err => {
      if (err) throw new Error(err);
      res();
    })
  );

export const uploadImage = async (
  file: FileUpload,
  entity: EntityTypes,
  id: string
): Promise<string> => {
  const result = await file;

  if (!(await dirExists(STORAGE_DIR))) {
    await createUploadsDir(STORAGE_DIR);
  }

  return new Promise(res => {
    const filePath = `${uuidv4()}-${entity}-${id}`;
    const { stream } = result;
    stream.pipe(fs.createWriteStream(`${STORAGE_DIR}/${filePath}`));
    stream.on('end', () => {
      res(encodeURI(`/uploads/${filePath}`));
    });
  });
};