diff options
author | kartofen <mladenovnasko0@gmail.com> | 2022-08-28 23:22:15 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2022-08-28 23:22:15 +0300 |
commit | 9d952483f250a97cbeab4061fa1c4e68341b330f (patch) | |
tree | 9d094911dc5178db9375eaa661e7f4bf0d5bf2cc /src/lib/image.ts | |
parent | 35b06b4fc851192916e000ad5e7e2f458846448c (diff) |
posting thread and image works
Diffstat (limited to 'src/lib/image.ts')
-rw-r--r-- | src/lib/image.ts | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/src/lib/image.ts b/src/lib/image.ts index 53d4c98..c7d4d49 100644 --- a/src/lib/image.ts +++ b/src/lib/image.ts @@ -1,10 +1,39 @@ 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; + if(imgId == null || imgId == '' || imgId == undefined) return null; - const response = await api('get', `image/${imgId}`) + const response = await api('get', `image/${imgId}`) - if(response.status !== 200) return `Server error: ${response.status}`; - return response.text(); + 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}`; + + //save file in this server + writeFileSync(`dist/client/images/${filename}`, img_b64, "base64"); + // writeFileSync(`public/images/${filename}`, img_b64, "base64"); + + let data = { + 'name': filename, + 'base64': img_b64 + }; + await api('post', 'image', JSON.stringify(data)); + + return filename; +} + +export function getImgType(img: string): string +{ + if(!img) return null; + return img.split('/')[0].split(':')[1]; } |