aboutsummaryrefslogtreecommitdiff
path: root/src/lib/image.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/image.ts')
-rw-r--r--src/lib/image.ts42
1 files changed, 14 insertions, 28 deletions
diff --git a/src/lib/image.ts b/src/lib/image.ts
index 6ab20a5..3f19128 100644
--- a/src/lib/image.ts
+++ b/src/lib/image.ts
@@ -1,39 +1,25 @@
-import { api } from './api';
import { v4 as uuidV4 } from 'uuid';
import { writeFileSync } from 'fs';
-export async function getImg(imgId: string) {
- if(imgId == null || imgId == '' || imgId == undefined) return null;
-
- const response = await api('get', `image/${imgId}`)
-
- if(response.status !== 200) return `Server error: ${response.status}`;
- return response.text();
-}
-
-export async function postImg(img: string) {
- if(!img) return null;
-
- img = img.split(':')[1]; // remove 'data:' part
- let img_b64= img.split(',')[1]; // remove header
- let fileExt = img.split(',')[0].split('/')[1].split(';')[0];
- let filename = `${uuidV4()}.${fileExt}`;
+export function saveImg(img: string): string
+{
+ if(!img) return null;
- //save file in this server
- // writeFileSync(`dist/client/images/${filename}`, img_b64, "base64");
- writeFileSync(`public/images/${filename}`, img_b64, "base64");
+ let filename = `/images/${uuidV4()}.${img.split(';')[0].split('/')[1]}`;
- let data = {
- 'name': filename,
- 'base64': img_b64
- };
- await api('post', 'image', JSON.stringify(data));
+ let path;
+ if(process.env.DEV) {
+ path = `public/${filename}`;
+ } else {
+ path = `dist/client/${filename}`;
+ }
+ writeFileSync(path, img.split(',')[1], "base64");
- return filename;
+ return filename;
}
export function getImgType(img: string): string
{
- if(!img) return null;
- return img.split('/')[0].split(':')[1];
+ if(!img) return null;
+ return img.split('/')[0].split(':')[1];
}