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

View file

@ -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,

View file

@ -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);