add file uploads
This commit is contained in:
parent
93027bfd09
commit
2c480909ee
6 changed files with 42 additions and 7 deletions
8
lib.js
8
lib.js
|
@ -9,6 +9,11 @@ function apiStat(res, next, message, success = false, redirect = '/') {
|
|||
next();
|
||||
}
|
||||
|
||||
function splitUp(content) {
|
||||
let out = content.split('\n').map(x => x.split('::'));
|
||||
return out;
|
||||
}
|
||||
|
||||
async function replyIterator(replies, db) {
|
||||
for (let reply of replies) {
|
||||
if (reply.childType == 'hub') {
|
||||
|
@ -31,6 +36,9 @@ async function replyIterator(replies, db) {
|
|||
reply.comment.type = reply.childType;
|
||||
//reply.comment.id = reply.childId;
|
||||
}
|
||||
for (let reply of replies) {
|
||||
reply.comment.content = splitUp(reply.comment.content)
|
||||
}
|
||||
return replies;
|
||||
}
|
||||
|
||||
|
|
|
@ -195,3 +195,8 @@ h1 {
|
|||
.link {
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
.commentbox {
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
}
|
|
@ -5,9 +5,10 @@ async function formClick(ev) {
|
|||
|
||||
let fData = new FormData(target);
|
||||
|
||||
let file = target.querySelector('input[type=file]');
|
||||
if (file)
|
||||
for (let file of target.querySelectorAll('.form-input')) {
|
||||
file.value = null;
|
||||
file.textContent = '';
|
||||
}
|
||||
|
||||
let fetched = await fetch(target.action, {
|
||||
'method': 'POST',
|
||||
|
|
|
@ -23,7 +23,11 @@ router.post('/', async (req, res, next) => {
|
|||
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 (?,?,?,?)', [
|
||||
username,
|
||||
|
|
|
@ -3,10 +3,21 @@ import multer from "multer";
|
|||
import cookieParser from "cookie-parser";
|
||||
import { initDb } from "../db.js";
|
||||
import express from "express";
|
||||
import { randomBytes } from 'node:crypto';
|
||||
|
||||
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 = {
|
||||
'/': '/walls/get/hub/main/0'
|
||||
|
@ -95,8 +106,8 @@ async function doInit(app) {
|
|||
app.use(auther)
|
||||
app.use(initr)
|
||||
|
||||
app.use('/api/form', upload.none());
|
||||
app.use('/api/file', upload.single('file'));
|
||||
app.use('/api/form', upload2.none());
|
||||
app.use('/api/file', upload2.single('file'));
|
||||
|
||||
await importRouters(app, routers);
|
||||
|
||||
|
|
|
@ -6,7 +6,13 @@
|
|||
Context
|
||||
<% } %>
|
||||
</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)) { %>
|
||||
<div class='sub'><b>Posted on</b> <%= new Date(date).toISOString() %> </div>
|
||||
<% } %>
|
||||
|
|
Loading…
Reference in a new issue