aboutsummaryrefslogtreecommitdiff
path: root/src/lib/image.ts
blob: 69aa0b090670412679e53090ed76bda99dd28254 (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
import { v4 as uuidV4 } from 'uuid';
import { writeFileSync, mkdir } from 'fs';

export function saveImg(img: string): string
{
    if(!img) return null;

    let filename = `${uuidV4()}.${img.split(';')[0].split('/')[1]}`;

    let path;
    if(process.env.DEV) {
        path = `/public/${filename}`;
    } else {
        path = `/dist/client/${filename}`;
    }
    writeFileSync(path, img.split(',')[1], "base64");

    return filename;
}

export function getImgType(img: string): string
{
    if(!img) return null;
    return img.split('/')[0].split(':')[1];
}