feat: 新增剪切与重命名的api
This commit is contained in:
1 parent
e0febdfa07
commit
59dbbb288c
7 files changed
+83
-12
No files matched your search
@@ -0,0 +1,33 @@
|
|||||||
|
import { api, staticPath } from "../config.js";
|
||||||
|
import path from "path";
|
||||||
|
import useFsOprateEventCenter from "../hooks/useFsOprateEventCenter.js";
|
||||||
|
import fs from "fs";
|
||||||
|
import express from "express";
|
||||||
|
|
||||||
|
const fsOprateEventCenter = useFsOprateEventCenter();
|
||||||
|
|
||||||
|
function cutPath(targetPath, newPath) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
fs.stat(targetPath, (err, stats) => {
|
||||||
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
return reject(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.rename(targetPath, newPath, err => {
|
||||||
|
if (err) return reject(err);
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function useCutApi(app) {
|
||||||
|
app.post(api.cut, express.json(), (req, res) => {
|
||||||
|
const decodedPath = decodeURIComponent(req.path).substring(api.cut.length - 1);
|
||||||
|
const targetPath = path.join(staticPath, decodedPath);
|
||||||
|
const newPath = path.join(staticPath, req.body.newPath, path.basename(targetPath));
|
||||||
|
const eventId = fsOprateEventCenter.push(cutPath(targetPath, newPath));
|
||||||
|
res.send({ eventId });
|
||||||
|
});
|
||||||
|
}
|
||||||
+4
-10
@@ -10,27 +10,21 @@ function deletePath(targetPath) {
|
|||||||
fs.stat(targetPath, (err, stats) => {
|
fs.stat(targetPath, (err, stats) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(err)
|
console.log(err)
|
||||||
return reject(err); // 如果获取状态失败,返回错误
|
return reject(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stats.isFile()) {
|
if (stats.isFile()) {
|
||||||
// 如果是文件,直接删除
|
|
||||||
fs.rm(targetPath, { recursive: true }, (err) => {
|
fs.rm(targetPath, { recursive: true }, (err) => {
|
||||||
if (err) return reject(err);
|
if (err) return reject(err);
|
||||||
resolve(); // 删除成功
|
resolve();
|
||||||
});
|
});
|
||||||
// console.log('delete file')
|
|
||||||
} else if (stats.isDirectory()) {
|
} else if (stats.isDirectory()) {
|
||||||
// 如果是文件夹,递归删除
|
|
||||||
fs.rm(targetPath, { recursive: true }, (err) => {
|
fs.rm(targetPath, { recursive: true }, (err) => {
|
||||||
if (err) return reject(err);
|
if (err) return reject(err);
|
||||||
resolve(); // 删除成功
|
resolve();
|
||||||
});
|
});
|
||||||
// console.log('delete directory')
|
|
||||||
} else {
|
} else {
|
||||||
// console.log('unknow file')
|
reject('unknow file');
|
||||||
// 其他类型(如符号链接等),可以根据需要处理
|
|
||||||
reject('unknow file'); // 这里选择不处理其他类型
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,6 +3,10 @@ import useFsOprateEventCenter from "../hooks/useFsOprateEventCenter.js";
|
|||||||
|
|
||||||
const fsOprateEventCenter = useFsOprateEventCenter();
|
const fsOprateEventCenter = useFsOprateEventCenter();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取一个文件操作事件的结果
|
||||||
|
* @returns {any}
|
||||||
|
*/
|
||||||
export default function useQueryApi(app) {
|
export default function useQueryApi(app) {
|
||||||
app.get(api.query, (req, res) => {
|
app.get(api.query, (req, res) => {
|
||||||
const id = req.query.id;
|
const id = req.query.id;
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import { api, staticPath } from "../config.js";
|
||||||
|
import path from "path";
|
||||||
|
import useFsOprateEventCenter from "../hooks/useFsOprateEventCenter.js";
|
||||||
|
import fs from "fs";
|
||||||
|
import express from "express";
|
||||||
|
|
||||||
|
const fsOprateEventCenter = useFsOprateEventCenter();
|
||||||
|
|
||||||
|
function renamePath(targetPath, newName) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
fs.stat(targetPath, (err, stats) => {
|
||||||
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
return reject(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
const newTargetPath = path.join(path.dirname(targetPath), newName);
|
||||||
|
fs.rename(targetPath, newTargetPath, err => {
|
||||||
|
if (err) return reject(err);
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function useRenameApi(app) {
|
||||||
|
app.post(api.rename, express.json(), (req, res) => {
|
||||||
|
const decodedPath = decodeURIComponent(req.path).substring(api.rename.length - 1);
|
||||||
|
const targetPath = path.join(staticPath, decodedPath);
|
||||||
|
const newName = req.body.newName;
|
||||||
|
const eventId = fsOprateEventCenter.push(renamePath(targetPath, newName));
|
||||||
|
res.send({ eventId });
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -6,9 +6,11 @@ export const port = 17705;
|
|||||||
|
|
||||||
export const api = {
|
export const api = {
|
||||||
get: `/${"get"}`,
|
get: `/${"get"}`,
|
||||||
|
query: `/${"query"}`,
|
||||||
upload: `/${"upload"}/*`,
|
upload: `/${"upload"}/*`,
|
||||||
delete: `/${"delete"}/*`,
|
delete: `/${"delete"}/*`,
|
||||||
query: `/${"query"}`
|
rename: `/${"rename"}/*`,
|
||||||
|
cut: `/${"cut"}/*`,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default { staticPath, port, api }
|
export default { staticPath, port, api }
|
||||||
@@ -59,7 +59,7 @@ class FsOprateEventCenter {
|
|||||||
const res = {status}
|
const res = {status}
|
||||||
if( ['success', 'failed'].includes(status) ){
|
if( ['success', 'failed'].includes(status) ){
|
||||||
if(status === 'failed'){
|
if(status === 'failed'){
|
||||||
res.result = this.eventMap[id].event.failedEvent
|
res.failedEvent = this.eventMap[id].event.failedEvent
|
||||||
}
|
}
|
||||||
clearInterval(this.eventMap[id].timer)
|
clearInterval(this.eventMap[id].timer)
|
||||||
this.eventMap[id] = undefined
|
this.eventMap[id] = undefined
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import useGetApi from "./api/useGetApi.js";
|
|||||||
import useUploadApi from "./api/useUploadApi.js";
|
import useUploadApi from "./api/useUploadApi.js";
|
||||||
import useDeleteApi from "./api/useDeleteApi.js";
|
import useDeleteApi from "./api/useDeleteApi.js";
|
||||||
import useQueryApi from "./api/useQueryApi.js";
|
import useQueryApi from "./api/useQueryApi.js";
|
||||||
|
import useRenameApi from "./api/useRenameApi.js";
|
||||||
|
import useCutApi from "./api/useCutApi.js";
|
||||||
|
|
||||||
const { staticPath, port, api } = Config;
|
const { staticPath, port, api } = Config;
|
||||||
|
|
||||||
@@ -21,6 +23,8 @@ useGetApi(app);
|
|||||||
useUploadApi(app);
|
useUploadApi(app);
|
||||||
useDeleteApi(app);
|
useDeleteApi(app);
|
||||||
useQueryApi(app);
|
useQueryApi(app);
|
||||||
|
useRenameApi(app);
|
||||||
|
useCutApi(app);
|
||||||
|
|
||||||
app.listen(port, "::", () => {
|
app.listen(port, "::", () => {
|
||||||
const interfaces = os.networkInterfaces();
|
const interfaces = os.networkInterfaces();
|
||||||
|
|||||||
Reference in new issue
Block a user