aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/api.ts17
-rw-r--r--src/lib/image.ts42
-rw-r--r--src/lib/thread.ts169
-rw-r--r--src/lib/util.ts27
4 files changed, 103 insertions, 152 deletions
diff --git a/src/lib/api.ts b/src/lib/api.ts
index e6ac0f1..160e308 100644
--- a/src/lib/api.ts
+++ b/src/lib/api.ts
@@ -1,12 +1,11 @@
-const base = 'https://localhost:5001/api'
+const base = 'http://localhost:6969'
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: {
- 'content-type': 'application/json'
- },
- body: data
- });
+ return await fetch(`${base}/${resource}`, {
+ method,
+ headers: {
+ 'content-type': 'application/json'
+ },
+ body: data
+ });
}
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];
}
diff --git a/src/lib/thread.ts b/src/lib/thread.ts
index 2199318..d7460c7 100644
--- a/src/lib/thread.ts
+++ b/src/lib/thread.ts
@@ -1,115 +1,92 @@
import { existsSync } from 'fs';
import { v4 as uuidV4, v5 as uuidV5, NIL as uuidNIL } from 'uuid';
-import { getImg, postImg, getImgType } from './image';
+import { saveImg, 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(`dist/client/images/${imageId}`))
- if(existsSync(`public/images/${imageId}`))
- thread.imageId = `/images/${imageId}`;
- else
- thread.imageId = await getImg(imageId);
-
- thread.threadText = replaceURLs(thread.threadText, board, thread.id);
+export async function processThreadIn(board: string, thread: Thread) {
+ for(let comment of thread.comments) {
+ comment.id += thread.id;
+ comment.content = replaceURLs(comment.content, board, thread.id);
+ }
+}
- if(!comments) return;
+export function processThreadOut(form: any): Thread {
+ let img = form.get('image');
- for(let comment of thread.comments)
- {
- let cimageId = comment.imageId;
- // if(existsSync(`dist/client/images/${cimageId}`))
- if(existsSync(`public/images/${cimageId}`))
- comment.imageId = `/images/${cimageId}`;
- else
- comment.imageId = await getImg(cimageId);
+ let t: Thread = {
+ title: form.get('title'),
+ content: form.get('content'),
+ image: saveImg(img),
+ imagetype: getImgType(img),
+ }
- comment.commentText = replaceURLs(comment.commentText, board, thread.id);
- }
+ return t;
}
-export async function processThreadOut(form: any): Thread {
- let img = form.get('image');
-
- let t: Thread = {
- id: uuidV4(),
- threadName: form.get('ThreadName'),
- threadCreator: uuidV5(form.get('iphash'), uuidNIL),
- threadText: form.get('ThreadText'),
- comments: [],
- creationDate: Date.now(),
- imageId: await postImg(img),
- fileType: getImgType(img),
- }
-
- return t;
-}
+export function processCommentOut(form: any): Comment {
+ let img = form.get('image');
-export async function processCommentOut(form: any): Comment {
- let img = form.get('image');
+ let c: Comment = {
+ content: form.get('content'),
+ image: saveImg(img),
+ imagetype: getImgType(img),
+ }
- let c: Comment = {};
- c.id = uuidV4();
- c.commentCreator = uuidV5(form.get('iphash'), uuidNIL);
- c.commentText = form.get('CommentText');
- c.creationDate = Date.now();
- c.imageId = await postImg(img);
- c.fileType = getImgType(img);
-
- return c;
+ return c;
}
function replaceURLs(text: string, board: string, OPtid?: string): string {
- if(!text) return;
+ if(!text) return;
+
+ let quoteRegex = />\s.*/g;
+ text = text.replace(quoteRegex, (q) => {
+ return ` <span class="quote"> ${q} </span> `;
+ });
- if(OPtid) {
- let replyRegex = /(^|[^>])>>[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}/g;
+ let replyRegex = />>\d+/g;
text = text.replace(replyRegex, (id) => {
- let showid = id;
- id = id.replace(/(>|\n| )/g, '');
- if(id == OPtid) showid += "(OP)";
- // this adds the targeted class
- // to the target id when its hovered on
- // and removes it when it stops hovering
- return ` <a class="${id}" href="#${id}" onmouseover="document.getElementById('${id}').classList.add('targeted')" onmouseleave="document.getElementById('${id}').classList.remove('targeted')"> ${showid} </a> `;
+ let showid = id;
+ id = id.replace(/>/g, '');
+ if(id == OPtid) showid += "(OP)";
+ // to highlight referenced id
+ return ` <a class="${id}" href="#${id}" onmouseover="document.getElementById('${id}').classList.add('targeted')" onmouseleave="document.getElementById('${id}').classList.remove('targeted')"> ${showid} </a> `;
+ });
+
+
+
+ // link to thread on other board
+ // let otherthreadlinkRegex = />>>\/.*\/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}/g;
+ // text = text.replace(otherthreadlinkRegex, (id) => {
+ // let link = id.slice(4);
+ // let parts = link.split('/');
+ // let linkid = parts[1]; let board = parts[0];
+ // return ` <a href="${board}/${linkid}#{linkid}"> >>>/${board}/${linkid} </a> `
+ // });
+ // link to thread on this board
+ // let threadlinkRegex = />>>[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}/g;
+ // text = text.replace(threadlinkRegex, (id) => {
+ // let linkid = id.slice(3); // remove the first >>>
+ // return ` <a href="../${board}/${linkid}#${linkid}"> ${id} </a> `
+ // });
+ // link to another board
+ // let otherboardRegex = />>>\/.*\/($|[^0-9a-fA-F])/g;
+ // text = text.replace(otherboardRegex, (id) => {
+ // let boardid = id.split('/')[1];
+ // return ` <a href="../${boardid}"> >>>/${boardid}/ </a> `
+ // });
+
+ let newlineRegex = /(\r\n|\r|\n)/g;
+ text = text.replace(newlineRegex, () => {
+ return ' <br> '
+ });
+
+ let urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
+ return text.replace(urlRegex, (url) => {
+ var hyperlink = url;
+ if (!hyperlink.match('^https?:\/\/')) {
+ hyperlink = 'http://' + hyperlink;
+ }
+ return ' <a href="' + hyperlink + '" target="_blank" rel="noopener noreferrer">' + url + '</a> '
});
- }
-
- // link to thread on other board
- let otherthreadlinkRegex = />>>\/.*\/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}/g;
- text = text.replace(otherthreadlinkRegex, (id) => {
- let link = id.slice(4);
- let parts = link.split('/');
- let linkid = parts[1]; let board = parts[0];
- return ` <a href="${board}/${linkid}#{linkid}"> >>>/${board}/${linkid} </a> `
- });
- // link to thread on this board
- let threadlinkRegex = />>>[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}/g;
- text = text.replace(threadlinkRegex, (id) => {
- let linkid = id.slice(3); // remove the first >>>
- return ` <a href="../${board}/${linkid}#${linkid}"> ${id} </a> `
- });
- // link to another board
- let otherboardRegex = />>>\/.*\/($|[^0-9a-fA-F])/g;
- text = text.replace(otherboardRegex, (id) => {
- let boardid = id.split('/')[1];
- return ` <a href="../${boardid}"> >>>/${boardid}/ </a> `
- });
-
- let newlineRegex = /(\r\n|\r|\n)/g;
- text = text.replace(newlineRegex, () => {
- return ' <br> '
- });
-
- let urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
- return text.replace(urlRegex, (url) => {
- var hyperlink = url;
- if (!hyperlink.match('^https?:\/\/')) {
- hyperlink = 'http://' + hyperlink;
- }
- return ' <a href="' + hyperlink + '" target="_blank" rel="noopener noreferrer">' + url + '</a> '
- });
}
diff --git a/src/lib/util.ts b/src/lib/util.ts
index b6dd442..8434c86 100644
--- a/src/lib/util.ts
+++ b/src/lib/util.ts
@@ -1,23 +1,12 @@
-export const creatorColor = (creatorid: string) => {
- 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;
-};
-
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*1000);
+ let year = date.getFullYear();
+ let month = date.getMonth() + 1;
+ let day = date.getDate();
+ let time = date.toTimeString().split(' ')[0];
+ console.log(date.toTimeString());
+ let formattedTime = `${time}, ${day}/${month}/${year}`
- return formattedTime;
+ return formattedTime;
}