mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-06 07:29:38 +00:00
54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
import { RequestHandler } from "express";
|
|
import { resolve } from "path";
|
|
import { readdir, stat } from "fs/promises";
|
|
import { existsSync } from "fs";
|
|
import { dirname } from "node:path"
|
|
import { fileURLToPath } from "node:url"
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = dirname(__filename);
|
|
export const GetLogFileListHandler: RequestHandler = async (req, res) => {
|
|
try {
|
|
let LogsPath = resolve(__dirname, "./logs/");
|
|
let LogFiles = await readdir(LogsPath);
|
|
res.json({
|
|
code: 0,
|
|
data: LogFiles
|
|
});
|
|
} catch (error) {
|
|
res.json({ code: -1, msg: "Failed to retrieve log file list." });
|
|
}
|
|
};
|
|
|
|
export const GetLogFileHandler: RequestHandler = async (req, res) => {
|
|
let LogsPath = resolve(__dirname, "./logs/");
|
|
let LogFile = req.query.file as string;
|
|
|
|
if (!isValidFileName(LogFile)) {
|
|
res.json({ code: -1, msg: "LogFile is not safe" });
|
|
return;
|
|
}
|
|
|
|
let filePath = `${LogsPath}/${LogFile}`;
|
|
if (!existsSync(filePath)) {
|
|
res.status(404).json({ code: -1, msg: "LogFile does not exist" });
|
|
return;
|
|
}
|
|
|
|
try {
|
|
let fileStats = await stat(filePath);
|
|
if (!fileStats.isFile()) {
|
|
res.json({ code: -1, msg: "LogFile must be a file" });
|
|
return;
|
|
}
|
|
|
|
res.sendFile(filePath);
|
|
} catch (error) {
|
|
res.json({ code: -1, msg: "Failed to send log file." });
|
|
}
|
|
};
|
|
export function isValidFileName(fileName: string): boolean {
|
|
const invalidChars = /[\.\:\*\?\"\<\>\|\/\\]/;
|
|
return !invalidChars.test(fileName);
|
|
} |