add file uploads
This commit is contained in:
parent
93027bfd09
commit
2c480909ee
6 changed files with 42 additions and 7 deletions
|
@ -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);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue