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,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 });
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user