add file uploads

This commit is contained in:
biglyderv 2025-02-26 20:17:33 -05:00
parent 93027bfd09
commit 2c480909ee
Signed by: biglyderv
GPG key ID: 0E2EB0B4CD7397B5
6 changed files with 42 additions and 7 deletions

8
lib.js
View file

@ -9,6 +9,11 @@ function apiStat(res, next, message, success = false, redirect = '/') {
next(); next();
} }
function splitUp(content) {
let out = content.split('\n').map(x => x.split('::'));
return out;
}
async function replyIterator(replies, db) { async function replyIterator(replies, db) {
for (let reply of replies) { for (let reply of replies) {
if (reply.childType == 'hub') { if (reply.childType == 'hub') {
@ -31,6 +36,9 @@ async function replyIterator(replies, db) {
reply.comment.type = reply.childType; reply.comment.type = reply.childType;
//reply.comment.id = reply.childId; //reply.comment.id = reply.childId;
} }
for (let reply of replies) {
reply.comment.content = splitUp(reply.comment.content)
}
return replies; return replies;
} }

View file

@ -194,4 +194,9 @@ h1 {
.link { .link {
color: inherit; color: inherit;
}
.commentbox {
white-space: pre-wrap;
word-wrap: break-word;
} }

View file

@ -5,9 +5,10 @@ async function formClick(ev) {
let fData = new FormData(target); let fData = new FormData(target);
let file = target.querySelector('input[type=file]'); for (let file of target.querySelectorAll('.form-input')) {
if (file)
file.value = null; file.value = null;
file.textContent = '';
}
let fetched = await fetch(target.action, { let fetched = await fetch(target.action, {
'method': 'POST', 'method': 'POST',

View file

@ -23,7 +23,11 @@ router.post('/', async (req, res, next) => {
return; return;
} }
let token = randomBytes(12).toString('hex'); let token = randomBytes(8).toString('hex');
if (req.file && req.file.path) {
post += `\nfile::${req.file.path}`;
}
await db.run('INSERT INTO comment (username, date, content, id) VALUES (?,?,?,?)', [ await db.run('INSERT INTO comment (username, date, content, id) VALUES (?,?,?,?)', [
username, username,

View file

@ -3,10 +3,21 @@ import multer from "multer";
import cookieParser from "cookie-parser"; import cookieParser from "cookie-parser";
import { initDb } from "../db.js"; import { initDb } from "../db.js";
import express from "express"; import express from "express";
import { randomBytes } from 'node:crypto';
let db = await initDb(); let db = await initDb();
const upload = multer({ dest: 'uploads/' }); const upload = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads/')
},
filename: function (req, file, cb) {
const uniqueSuffix = randomBytes(8).toString('hex');
cb(null, uniqueSuffix + '.png')
}
});
const upload2 = multer({ storage: upload });
const aliases = { const aliases = {
'/': '/walls/get/hub/main/0' '/': '/walls/get/hub/main/0'
@ -95,8 +106,8 @@ async function doInit(app) {
app.use(auther) app.use(auther)
app.use(initr) app.use(initr)
app.use('/api/form', upload.none()); app.use('/api/form', upload2.none());
app.use('/api/file', upload.single('file')); app.use('/api/file', upload2.single('file'));
await importRouters(app, routers); await importRouters(app, routers);

View file

@ -6,7 +6,13 @@
Context Context
<% } %> <% } %>
</h1> </h1>
<div><%= content %></div> <% for (let contentN of content) { %>
<% if (contentN[0] == 'file' && contentN [1]) { %>
<img class='art' src='<%= new URL(contentN[1], 'https://tbg.dervland.net/') %>'>
<% } else { %>
<div class='commentbox'><%= contentN[0] %></div>
<% } %>
<% } %>
<% if (!isNaN(date * 1)) { %> <% if (!isNaN(date * 1)) { %>
<div class='sub'><b>Posted on</b> <%= new Date(date).toISOString() %> </div> <div class='sub'><b>Posted on</b> <%= new Date(date).toISOString() %> </div>
<% } %> <% } %>