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 | |
parent | 35b06b4fc851192916e000ad5e7e2f458846448c (diff) |
posting thread and image works
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/api.ts | 2 | ||||
-rw-r--r-- | src/lib/image.ts | 37 | ||||
-rw-r--r-- | src/lib/thread.ts | 33 | ||||
-rw-r--r-- | src/lib/util.ts | 30 |
4 files changed, 74 insertions, 28 deletions
diff --git a/src/lib/api.ts b/src/lib/api.ts index 221a7b3..645fc23 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -2,8 +2,6 @@ const base = 'https://localhost:5001/api' export async function api(method: string, resource: string, data?: any) { console.log("API USED", method, resource, data); - - return await fetch(`${base}/${method}/${resource}`, { method, headers: { 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]; } diff --git a/src/lib/thread.ts b/src/lib/thread.ts index 6bc54ce..e0f0aa4 100644 --- a/src/lib/thread.ts +++ b/src/lib/thread.ts @@ -1,12 +1,15 @@ -import { writeFileSync, existsSync } from 'fs'; -import { getImg } from './image'; -import type Thread from '../models/Thread' +import { existsSync } from 'fs'; +import { v4 as uuidV4, v5 as uuidV5, NIL as uuidNIL } from 'uuid'; -export async function processThreadIn(board: string, thread: Thread, comments? =false) { +import { getImg, postImg, getImgType } from './image'; +import type { Thread, Comment } from '../models/Thread' + +export async function processThreadIn(board: string, thread: Thread, comments = false) { if(!thread || !board) return; let imageId: string = thread.imageId; - if(existsSync(`public/images/${imageId}`)) + if(existsSync(`dist/client/images/${imageId}`)) + // if(existsSync(`public/images/${imageId}`)) thread.imageId = `/images/${imageId}`; else thread.imageId = await getImg(imageId); @@ -18,7 +21,8 @@ export async function processThreadIn(board: string, thread: Thread, comments? = for(let comment of thread.comments) { let cimageId = comment.imageId; - if(existsSync(`public/images/${cimageId}`)) + if(existsSync(`dist/client/images/${cimageId}`)) + // if(existsSync(`public/images/${cimageId}`)) comment.imageId = `/images/${cimageId}`; else comment.imageId = await getImg(cimageId); @@ -27,8 +31,23 @@ export async function processThreadIn(board: string, thread: Thread, comments? = } } -export async function processThreadOUT(board: string, thread: Thread, comments? = false) { +export async function processThreadOut(form: any): Thread { + let img = form.get('image').toString(); + + let t: Thread = {}; + t.id = uuidV4(); + t.ThreadName = form.get('ThreadName').toString(); + t.ThreadCreator = uuidV5(form.get('iphash'), uuidNIL); + t.ThreadText = form.get('ThreadText').toString(); + t.Comments = []; + t.CreationDate = Date.now(); + t.ImageId = await postImg(img); + t.FileType = getImgType(img); + + return t; +} +export async function processCommentOut(form: any): Comment { } function replaceURLs(text: string, board: string, OPtid?: string): string { diff --git a/src/lib/util.ts b/src/lib/util.ts index a0734b9..b6dd442 100644 --- a/src/lib/util.ts +++ b/src/lib/util.ts @@ -1,23 +1,23 @@ export const creatorColor = (creatorid: string) => { - let parts = creatorid.split('-') - let ints = parts.map(function(d) { return parseInt(d,16) }) - let code = ints[0] + let parts = creatorid.split('-') + let ints = parts.map(function(d) { return parseInt(d,16) }) + let code = ints[0] - let blue = (code >> 16) & 31; - let green = (code >> 21) & 31; - let red = (code >> 27) & 31; - let foreColor = `border:5px solid rgb(${red << 3}, ${green << 3}, ${blue << 3});`; - return foreColor; + let blue = (code >> 16) & 31; + let green = (code >> 21) & 31; + let red = (code >> 27) & 31; + let foreColor = `border:5px solid rgb(${red << 3}, ${green << 3}, ${blue << 3});`; + return foreColor; }; export function formatTime(timestamp: number): string { - let date = new Date(timestamp); - let year = date.getFullYear(); - let month = date.getMonth() + 1; - let day = date.getDate(); - let time = date.toTimeString().split(' ')[0]; - let formattedTime = `${time}, ${day}/${month}/${year}` + let date = new Date(timestamp); + let year = date.getFullYear(); + let month = date.getMonth() + 1; + let day = date.getDate(); + let time = date.toTimeString().split(' ')[0]; + let formattedTime = `${time}, ${day}/${month}/${year}` - return formattedTime; + return formattedTime; } |