From 28ee6daab453e1d4ee520fe714c2c4622aca9aed Mon Sep 17 00:00:00 2001 From: Zhoujiahui <539943419@qq.com> Date: Fri, 16 Aug 2024 17:04:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/useMkdirApi.js | 23 +++++++++++++++++++++++ api/useUploadApi.js | 10 ++++------ config.js | 13 +++++++------ index.js | 2 ++ 4 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 api/useMkdirApi.js diff --git a/api/useMkdirApi.js b/api/useMkdirApi.js new file mode 100644 index 0000000..1812c4b --- /dev/null +++ b/api/useMkdirApi.js @@ -0,0 +1,23 @@ +import fs from 'fs' +import path from 'path' +import express from 'express' +import { api, staticPath } from '../config.js' + +export default function useMkdirApi(app) { + app.post(api.mkdir, express.json(), (req, res) => { + const folderName = req.body.folderName + const decodedPath = decodeURIComponent(req.path).substring( + api.mkdir.length - 1 + ) + const mkdirPath = path.join(staticPath, decodedPath, folderName) + // res.status(200).json({ mkdirPath, folderName }) + + // 创建文件夹 + fs.mkdir(mkdirPath, { recursive: true }, (err) => { + if (err) { + return res.status(500).json({ message: 'failed', error: err }) + } + res.status(200).json({ message: 'success' }) + }) + }) +} diff --git a/api/useUploadApi.js b/api/useUploadApi.js index eb86476..f4c884d 100644 --- a/api/useUploadApi.js +++ b/api/useUploadApi.js @@ -3,22 +3,20 @@ import fs from "fs"; import multer from "multer"; import { api, staticPath } from "../config.js"; -const api_upload = api.upload - const storage = multer.memoryStorage(); const upload = multer({ storage: storage }).single('file'); export default function useUploadApi(app) { - app.post(api_upload, upload, (req, res) => { + app.post(api.upload, upload, (req, res) => { // 必须这样传,不然中文乱码 const fileName = req.body.fileName - const decodedPath = decodeURIComponent(req.path).substring(api_upload.length - 1); + const decodedPath = decodeURIComponent(req.path).substring(api.upload.length - 1); const savePath = path.join(staticPath, decodedPath, fileName) // 覆盖写or追加写 - const oprateType = req.body.oprateType === 'write' ? 'write' : 'append' + const operateType = req.body.operateType === 'write' ? 'write' : 'append' const data = req.file.buffer - if (oprateType === 'write') { + if (operateType === 'write') { // 覆盖写入 fs.writeFile(savePath, data, (err) => { if (err) { diff --git a/config.js b/config.js index 7bd8ba1..e8d1436 100644 --- a/config.js +++ b/config.js @@ -5,12 +5,13 @@ export const staticPath = path.join("./static"); export const port = 17705; export const api = { - get: `/${"get"}`, - query: `/${"query"}`, - upload: `/${"upload"}/*`, - delete: `/${"delete"}/*`, - rename: `/${"rename"}/*`, - cut: `/${"cut"}/*`, + get: `/${'get'}`, + query: `/${'query'}`, + upload: `/${'upload'}/*`, + delete: `/${'delete'}/*`, + rename: `/${'rename'}/*`, + mkdir: `/${'mkdir'}/*`, + cut: `/${'cut'}/*` } export default { staticPath, port, api } \ No newline at end of file diff --git a/index.js b/index.js index 582a809..5dfbfa5 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ import useDeleteApi from "./api/useDeleteApi.js"; import useQueryApi from "./api/useQueryApi.js"; import useRenameApi from "./api/useRenameApi.js"; import useCutApi from "./api/useCutApi.js"; +import useMkdirApi from "./api/useMkdirApi.js"; const { staticPath, port, api } = Config; @@ -25,6 +26,7 @@ useDeleteApi(app); useQueryApi(app); useRenameApi(app); useCutApi(app); +useMkdirApi(app); app.listen(port, "::", () => { const interfaces = os.networkInterfaces();