Feat: panel support file upload

This commit is contained in:
unitwk 2023-11-27 19:50:08 +08:00
parent 6e0be288f8
commit 51cbdd5696
2 changed files with 23 additions and 1 deletions

View File

@ -123,7 +123,14 @@ _ / / / / /___ ____/ /_ / / / / /_/ /_ / / / /_/ /_ /_/ // __/ /
app.use(
koaBody({
multipart: true,
parsedMethods: ["POST", "PUT", "DELETE", "GET"]
parsedMethods: ["POST", "PUT", "DELETE", "GET"],
formidable: {
maxFieldsSize: Number.MAX_VALUE,
maxFileSize: Number.MAX_VALUE
},
onError(err, ctx) {
logger.error("koaBody Lib Error:", err);
}
})
);

View File

@ -6,6 +6,9 @@ import { saveSystemConfig, systemConfig } from "../../setting";
import { logger } from "../../service/log";
import { i18next } from "../../i18n";
import userSystem from "../../service/system_user";
import { v4 } from "uuid";
import path from "path";
import * as fs from "fs-extra";
import {
getFrontendLayoutConfig,
resetFrontendLayoutConfig,
@ -86,4 +89,16 @@ router.delete("/layout", permission({ level: 10 }), async (ctx) => {
ctx.body = true;
});
router.post("/upload_assets", permission({ level: 10 }), async (ctx) => {
const tmpFiles = ctx.request.files.file;
if (!tmpFiles || tmpFiles instanceof Array) throw new Error("The body is incorrect");
if (!tmpFiles.path || !fs.existsSync(tmpFiles.path)) throw new Error("The file does not exist");
const tmpFile = tmpFiles;
const newFileName = v4() + path.extname(tmpFile.name);
const saveDirPath = path.join(process.cwd(), "public/upload_files/");
if (!fs.existsSync(saveDirPath)) fs.mkdirsSync(saveDirPath);
await fs.move(tmpFile.path, path.join(saveDirPath, newFileName));
ctx.body = newFileName;
});
export default router;