From 9d952483f250a97cbeab4061fa1c4e68341b330f Mon Sep 17 00:00:00 2001 From: kartofen Date: Sun, 28 Aug 2022 23:22:15 +0300 Subject: posting thread and image works --- src/components/Comment.svelte | 45 --------------------- src/components/Form.astro | 53 +++++++++++++++++++++++++ src/components/Thread.svelte | 79 ++++++++++++++++++++++++++----------- src/layouts/Default.astro | 11 ++++-- src/lib/api.ts | 2 - src/lib/image.ts | 37 +++++++++++++++-- src/lib/thread.ts | 33 ++++++++++++---- src/lib/util.ts | 30 +++++++------- src/models/Thread.ts | 28 ++++++------- src/pages/board/[board].astro | 11 ++++++ src/pages/board/[board]/[tid].astro | 11 ++---- src/pages/boards.astro | 22 ++++------- src/pages/create/[board].astro | 67 +++++++++---------------------- src/pages/create/thread.ts | 32 +++++---------- src/styles/blackbox.css | 8 ++++ src/styles/thread.css | 18 +++++++++ 16 files changed, 280 insertions(+), 207 deletions(-) delete mode 100644 src/components/Comment.svelte create mode 100644 src/components/Form.astro create mode 100644 src/styles/blackbox.css create mode 100644 src/styles/thread.css (limited to 'src') diff --git a/src/components/Comment.svelte b/src/components/Comment.svelte deleted file mode 100644 index bc53f4d..0000000 --- a/src/components/Comment.svelte +++ /dev/null @@ -1,45 +0,0 @@ - - - -
- - - {comment.id} - at {formatTime(comment.creationDate)}
- -> - {comment.commentCreator} -
-
- - {#if comment.imageId!= null && comment.imageId != undefined} - {#if comment.fileType == 'image'} - - {comment.imageId}
-
- {:else if comment.fileType == 'video'} - - {/if} - {/if} - -

{@html comment.commentText}

- -
- - diff --git a/src/components/Form.astro b/src/components/Form.astro new file mode 100644 index 0000000..417b569 --- /dev/null +++ b/src/components/Form.astro @@ -0,0 +1,53 @@ +--- +import '../styles/blackbox.css'; +import { sha256 } from 'js-sha256'; + +const { board, tid } = Astro.props; +const iphash = sha256(Astro.clientAddress); +--- + +
+ +
+ + diff --git a/src/components/Thread.svelte b/src/components/Thread.svelte index fee1a78..0321b8d 100644 --- a/src/components/Thread.svelte +++ b/src/components/Thread.svelte @@ -1,11 +1,15 @@ + {#if thread.id != "rules"}
@@ -27,13 +31,16 @@ -> {thread.threadCreator}
+
- {#if listReplies(thread.id)} -
- {#each replies as id} - >>{id}  - {/each} -
+ {#if comments} + {#if listReplies(thread.id) } + + {#each listReplies(thread.id, true) as id} + >>{id}  + {/each} +
+ {/if} {/if} @@ -54,21 +61,45 @@

{@html thread.threadText}

- +{#if comments} + {#each thread.comments as comment} +
+ + {comment.id} + at {formatTime(comment.creationDate)}
-
+ -> + {comment.commentCreator} +
+
+ + {#if listReplies(comment.id)} + + {#each listReplies(comment.id, true) as id} + >>{id}  + {/each} +
+ {/if} + + + + {#if comment.imageId!= null && comment.imageId != undefined} + {#if comment.fileType == 'image'} + + {comment.imageId}
+
+ {:else if comment.fileType == 'video'} + + {/if} + {/if} + +

{@html comment.commentText}

+ +
+ {/each} {/if} - + +{/if} diff --git a/src/layouts/Default.astro b/src/layouts/Default.astro index 5a2a372..3c0b6ff 100644 --- a/src/layouts/Default.astro +++ b/src/layouts/Default.astro @@ -1,12 +1,15 @@ --- +const { title } = Astro.props as Props; --- - - + + + + {title} @@ -15,7 +18,7 @@ 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; } diff --git a/src/models/Thread.ts b/src/models/Thread.ts index e5d9f66..c31404f 100644 --- a/src/models/Thread.ts +++ b/src/models/Thread.ts @@ -1,19 +1,19 @@ export type Comment = { - id: string; - commentCreator: string; - commentText: string; - creationDate: number; - imageId: string; - fileType: string //image or video + id: string; + commentCreator: string; + commentText: string; + creationDate: number; + imageId: string; + fileType: string //image or video }; export type Thread = { - id: string; - threadName: string; - threadCreator: string; - threadText: string; - comments: Comment[]; - creationDate: number; - imageId: string; - fileType: string; //image or video + id: string; + threadName: string; + threadCreator: string; + threadText: string; + comments: Comment[]; + creationDate: number; + imageId: string; + fileType: string; //image or video }; diff --git a/src/pages/board/[board].astro b/src/pages/board/[board].astro index 23a5551..a529d3c 100644 --- a/src/pages/board/[board].astro +++ b/src/pages/board/[board].astro @@ -1,6 +1,8 @@ --- import Default from '../../layouts/Default.astro'; import Thread from '../../components/Thread.svelte' +import '../../styles/thread.css' +import '../../styles/blackbox.css?' import type Thread from '../../models/Thread'; import { api } from '../../lib/api.ts'; @@ -18,8 +20,17 @@ for(let thread of threads)

{board}

+
+ +
{threads.map((thread) => ( ))}
+ + diff --git a/src/pages/board/[board]/[tid].astro b/src/pages/board/[board]/[tid].astro index 80e7fbe..4aa34ef 100644 --- a/src/pages/board/[board]/[tid].astro +++ b/src/pages/board/[board]/[tid].astro @@ -1,7 +1,7 @@ --- import Default from '../../../layouts/Default.astro'; import Thread from '../../../components/Thread.svelte' -import Comment from '../../../components/Comment.svelte' +import '../../../styles/thread.css' import type Thread from '../../../models/Thread'; import { api } from '../../../lib/api'; @@ -14,13 +14,8 @@ if(data.status === 404) return Astro.redirect('/404'); const thread: Thread = await data.json(); await processThreadIn(board, thread, true); -const comments: Comment[] = thread.comments; --- - - {comments.map((comment) => ( - - ))} - - + + \ No newline at end of file diff --git a/src/pages/boards.astro b/src/pages/boards.astro index ab3158f..f10fa3a 100644 --- a/src/pages/boards.astro +++ b/src/pages/boards.astro @@ -1,32 +1,26 @@ --- import Default from '../layouts/Default.astro'; +import '../styles/blackbox.css' import { api } from '../lib/api.ts'; const data = await api('get', 'boards'); const boards: string[] = await data.json(); --- - -
-

Boards

+ +

Boards

+
- diff --git a/src/pages/create/[board].astro b/src/pages/create/[board].astro index 45f8b0d..9c2308f 100644 --- a/src/pages/create/[board].astro +++ b/src/pages/create/[board].astro @@ -1,54 +1,25 @@ --- -import Default from '../../layouts/Default.astro' +import Default from '../../layouts/Default.astro'; +import Form from '../../components/Form.astro'; + const { board } = Astro.params; --- -

{board}

- -
-
- -
- - -
- +

{board}

+ +
+ +
+ +
+ +
+
+ + diff --git a/src/pages/create/thread.ts b/src/pages/create/thread.ts index 0af3471..0a0b7d1 100644 --- a/src/pages/create/thread.ts +++ b/src/pages/create/thread.ts @@ -1,28 +1,16 @@ -import type { APIRoute } from 'astro'; -import { v4 as uuidV4 } from 'uuid'; +import { api } from '../../lib/api'; +import { processThreadOut } from '../../lib/thread'; -import type Thread from '../../models/Thread' -import { api } from '../../lib/api' +export async function post({ request }) { + const form = await request.formData(); -export async function post({ params, request }): APIRoute { - const form = await request.formData(); + console.log(form); - let t: Thread = {}; - t.id = uuidV4().toString(); - t.ThreadName = form.get('ThreadName').toString(); - // data["ThreadCreator"] = locals.userid; - t.ThreadCreator = ''; - t.ThreadText = form.get('ThreadText').toString(); - t.Comments = []; - t.CreationDate = Date.now(); - // data["ImageId"] = await post_img(base64image, writeFileSync); - // data["FileType"] = get_img_type(base64image); - t.ImageId = form.get('image').toString(); - t.FileType = ''; + let t: Thread = await processThreadOut(form); - // await api('post', `thread/${form.get('board')}`, JSON.stringify(data)); + await api('post', `thread/${form.get('board')}`, JSON.stringify(t)); - return new Response(null, { - status: 200 - }); + return new Response(t.id, { + status: 200 + }); } diff --git a/src/styles/blackbox.css b/src/styles/blackbox.css new file mode 100644 index 0000000..4a0e63f --- /dev/null +++ b/src/styles/blackbox.css @@ -0,0 +1,8 @@ + .blackbox { + width: var(--wdt); + border: 10px solid black; + padding: 10px; + margin: 10px; + margin-left: 0px; + background-color: white; + } diff --git a/src/styles/thread.css b/src/styles/thread.css new file mode 100644 index 0000000..14997de --- /dev/null +++ b/src/styles/thread.css @@ -0,0 +1,18 @@ +.threadbox { + width: 600px; + border: 10px solid green; + padding: 10px; + margin: 10px; + margin-left: 0px; + background-color: white; +} +.commentbox { + width: 500px; + border: 5px solid green; + padding: 10px; + margin: 10px; + background-color: white; +} +:target, .targeted { + background-color: #ffa; +} -- cgit v1.2.3