aboutsummaryrefslogtreecommitdiff
path: root/src/utils/files.ts
blob: 5d16fdb014f99bf21fba36954098e99533afb038 (plain)
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
import Compressor from 'compressorjs';


export const getLocalFileUrl = (file: File): Promise<string> => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();

    reader.addEventListener('load', () => {
      if (typeof reader.result === 'string') resolve(reader.result);
    }, false);

    reader.addEventListener('error', () => {
      reject(new Error('Error reading the file'))
    }, false );

    if (file) reader.readAsDataURL(file);
  });
};

export const compressFile = (file: File, quality = 0.6): Promise<Blob> => {
  return new Promise((resolve, reject) => {
    return new Compressor(file, {
      success: result => resolve(result),
      error: err => reject(err),
      quality
    });
  });
};