fix&rename: 重命名中间件, 上传文件中间件没能执行下一个函数
This commit is contained in:
69
server/fileupload-middleware.ts
Normal file
69
server/fileupload-middleware.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { Request, Response, NextFunction } from "express"
|
||||
import FileManager from "./data/FileManager.ts"
|
||||
import TokenManager from "./api/TokenManager.ts"
|
||||
import UserChatLinker from "./data/UserChatLinker.ts"
|
||||
import fileUpload from "express-fileupload"
|
||||
|
||||
export default class FileUploadMiddleware {
|
||||
static checkUser(req: Request, res: Response, chat_id: string | undefined) {
|
||||
const userToken = TokenManager.decode(req.headers.token || req.cookies.token)
|
||||
if (!TokenManager.checkToken(userToken, req.headers["device-id"] || req.cookies.device_id)) {
|
||||
res.status(401).send({
|
||||
msg: "401 UnAuthorized",
|
||||
})
|
||||
return false
|
||||
}
|
||||
if (chat_id && !UserChatLinker.checkUserIsLinkedToChat(userToken.author, chat_id)) {
|
||||
res.status(403).send({
|
||||
msg: "403 Forbidden",
|
||||
})
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
static checkAccessingUploadedFiles(req: Request, res: Response, next: NextFunction) {
|
||||
const hash = req.params.hash as string
|
||||
if (hash == null) {
|
||||
res.status(404).send({
|
||||
msg: "404 Not Found",
|
||||
})
|
||||
return
|
||||
}
|
||||
const file = FileManager.findByHash(hash)
|
||||
|
||||
if (file == null) {
|
||||
res.status(404).send({
|
||||
msg: "404 Not Found",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (file.getChatId() != null) {
|
||||
if (!FileUploadMiddleware.checkUser(req, res, file.getChatId() as string)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
next()
|
||||
}
|
||||
|
||||
static checkUploadedFile(req: Request, res: Response, next: NextFunction) {
|
||||
if (!FileUploadMiddleware.checkUser(req, res, req.body.chat_id)) {
|
||||
return
|
||||
}
|
||||
const file = req.files?.file as fileUpload.UploadedFile
|
||||
if (file?.data == null) {
|
||||
res.status(400).send({
|
||||
msg: "No file was found or multiple files were uploaded",
|
||||
})
|
||||
return
|
||||
}
|
||||
if (req.body.file_name == null) {
|
||||
res.status(400).send({
|
||||
msg: "Filename is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
next()
|
||||
}
|
||||
}
|
||||
@@ -16,12 +16,12 @@ import cookieParser from 'cookie-parser'
|
||||
import fs from 'node:fs/promises'
|
||||
// @ts-types="npm:@types/express-fileupload"
|
||||
import fileUpload from 'express-fileupload'
|
||||
import { Middleware } from "./middleware.ts"
|
||||
import FileUploadMiddleware from "./fileupload-middleware.ts"
|
||||
|
||||
const app = express()
|
||||
app.use('/', express.static(config.data_path + '/page_compiled'))
|
||||
app.use(cookieParser())
|
||||
app.get('/uploaded_files/:hash',Middleware.Get_uploaded_files, (req, res) => {
|
||||
app.get('/uploaded_files/:hash', FileUploadMiddleware.checkAccessingUploadedFiles, (req, res) => {
|
||||
const file = FileManager.findByHash(req.params.hash as string)
|
||||
|
||||
if (file == null) {
|
||||
@@ -41,7 +41,7 @@ app.use(fileUpload({
|
||||
tempFileDir: config.data_path + '/upload_cache',
|
||||
abortOnLimit: true,
|
||||
}))
|
||||
app.post('/upload_file',Middleware.Post_upload_file, async (req, res) => {
|
||||
app.post('/upload_file', FileUploadMiddleware.checkUploadedFile, async (req, res) => {
|
||||
const file = req.files?.file as fileUpload.UploadedFile
|
||||
const hash = (await FileManager.uploadFile(req.body.file_name, await fs.readFile(file.tempFilePath), req.body.chat_id)).getHash()
|
||||
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import FileManager from "./data/FileManager.ts";
|
||||
import TokenManager from "./api/TokenManager.ts";
|
||||
import UserChatLinker from "./data/UserChatLinker.ts";
|
||||
import fileUpload from "express-fileupload";
|
||||
|
||||
export class Middleware {
|
||||
static Authroize(req: Request, res: Response, chat_id: string | undefined) {
|
||||
const userToken = TokenManager.decode(req.headers.token || req.cookies.token);
|
||||
if (!TokenManager.checkToken(userToken, req.headers["device-id"] || req.cookies.device_id)) {
|
||||
res.status(401).send({
|
||||
msg: "401 UnAuthorized",
|
||||
});
|
||||
return false;
|
||||
}
|
||||
if (chat_id && !UserChatLinker.checkUserIsLinkedToChat(userToken.author, chat_id)) {
|
||||
res.status(403).send({
|
||||
msg: "403 Forbidden",
|
||||
});
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static Get_uploaded_files(req: Request, res: Response, next: NextFunction) {
|
||||
const hash = req.params.hash as string;
|
||||
if (hash == null) {
|
||||
res.status(404).send({
|
||||
msg: "404 Not Found",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const file = FileManager.findByHash(hash);
|
||||
|
||||
if (file == null) {
|
||||
res.status(404).send({
|
||||
msg: "404 Not Found",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (file.getChatId() != null) {
|
||||
if (!Middleware.Authroize(req, res, file.getChatId() as string)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
static Post_upload_file(req: Request, res: Response, next: NextFunction) {
|
||||
if (!Middleware.Authroize(req, res, req.body.chat_id)) {
|
||||
return;
|
||||
}
|
||||
const file = req.files?.file as fileUpload.UploadedFile;
|
||||
if (file?.data == null) {
|
||||
res.status(400).send({
|
||||
msg: "No file was found or multiple files were uploaded",
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (req.body.file_name == null) {
|
||||
res.status(400).send({
|
||||
msg: "Filename is required",
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user