Compare commits
4
Commits
d18ffaafc6
...
serve
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
afc57bc229 | ||
|
|
28ee6daab4 | ||
|
|
59dbbb288c | ||
|
|
e0febdfa07 |
No files matched your search
@@ -0,0 +1,33 @@
|
|||||||
|
import { api, staticPath } from "../config.js";
|
||||||
|
import path from "path";
|
||||||
|
import useFsOperateEventCenter from "../hooks/useFsOperateEventCenter.js";
|
||||||
|
import fs from "fs";
|
||||||
|
import express from "express";
|
||||||
|
|
||||||
|
const fsOperateEventCenter = useFsOperateEventCenter();
|
||||||
|
|
||||||
|
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 = fsOperateEventCenter.push(cutPath(targetPath, newPath));
|
||||||
|
res.send({ eventId });
|
||||||
|
});
|
||||||
|
}
|
||||||
+11
-17
@@ -1,36 +1,30 @@
|
|||||||
import { api, staticPath } from "../config.js";
|
import { api, staticPath } from "../config.js";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import useFsOprateEventCenter from "../hooks/useFsOprateEventCenter.js";
|
import useFsOperateEventCenter from "../hooks/useFsOperateEventCenter.js";
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
|
|
||||||
const fsOprateEventCenter = useFsOprateEventCenter()
|
const fsOperateEventCenter = useFsOperateEventCenter()
|
||||||
|
|
||||||
function deletePath(targetPath) {
|
function deletePath(targetPath) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
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.unlink(targetPath, (err) => {
|
|
||||||
if (err) return reject(err);
|
|
||||||
resolve(); // 删除成功
|
|
||||||
});
|
|
||||||
// console.log('delete file')
|
|
||||||
} 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();
|
||||||
|
});
|
||||||
|
} else if (stats.isDirectory()) {
|
||||||
|
fs.rm(targetPath, { recursive: true }, (err) => {
|
||||||
|
if (err) return reject(err);
|
||||||
|
resolve();
|
||||||
});
|
});
|
||||||
// console.log('delete directory')
|
|
||||||
} else {
|
} else {
|
||||||
// console.log('unknow file')
|
reject('unknow file');
|
||||||
// 其他类型(如符号链接等),可以根据需要处理
|
|
||||||
reject('unknow file'); // 这里选择不处理其他类型
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -40,7 +34,7 @@ export default function useDeleteApi(app) {
|
|||||||
app.post(api.delete, (req, res) => {
|
app.post(api.delete, (req, res) => {
|
||||||
const decodedPath = decodeURIComponent(req.path).substring(api.delete.length - 1);
|
const decodedPath = decodeURIComponent(req.path).substring(api.delete.length - 1);
|
||||||
const targetPath = path.join(staticPath, decodedPath);
|
const targetPath = path.join(staticPath, decodedPath);
|
||||||
const eventId = fsOprateEventCenter.push(deletePath(targetPath))
|
const eventId = fsOperateEventCenter.push(deletePath(targetPath))
|
||||||
res.send({ eventId });
|
res.send({ eventId });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -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' })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
+7
-3
@@ -1,11 +1,15 @@
|
|||||||
import { api } from "../config.js";
|
import { api } from "../config.js";
|
||||||
import useFsOprateEventCenter from "../hooks/useFsOprateEventCenter.js";
|
import useFsOperateEventCenter from "../hooks/useFsOperateEventCenter.js";
|
||||||
|
|
||||||
const fsOprateEventCenter = useFsOprateEventCenter();
|
const fsOperateEventCenter = useFsOperateEventCenter();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取一个文件操作事件的结果
|
||||||
|
* @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;
|
||||||
res.json(fsOprateEventCenter.query(id));
|
res.json(fsOperateEventCenter.query(id));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import { api, staticPath } from "../config.js";
|
||||||
|
import path from "path";
|
||||||
|
import useFsOperateEventCenter from "../hooks/useFsOperateEventCenter.js";
|
||||||
|
import fs from "fs";
|
||||||
|
import express from "express";
|
||||||
|
|
||||||
|
const fsOperateEventCenter = useFsOperateEventCenter();
|
||||||
|
|
||||||
|
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 = fsOperateEventCenter.push(renamePath(targetPath, newName));
|
||||||
|
res.send({ eventId });
|
||||||
|
});
|
||||||
|
}
|
||||||
+4
-6
@@ -3,22 +3,20 @@ import fs from "fs";
|
|||||||
import multer from "multer";
|
import multer from "multer";
|
||||||
import { api, staticPath } from "../config.js";
|
import { api, staticPath } from "../config.js";
|
||||||
|
|
||||||
const api_upload = api.upload
|
|
||||||
|
|
||||||
const storage = multer.memoryStorage();
|
const storage = multer.memoryStorage();
|
||||||
const upload = multer({ storage: storage }).single('file');
|
const upload = multer({ storage: storage }).single('file');
|
||||||
|
|
||||||
export default function useUploadApi(app) {
|
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 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)
|
const savePath = path.join(staticPath, decodedPath, fileName)
|
||||||
// 覆盖写or追加写
|
// 覆盖写or追加写
|
||||||
const oprateType = req.body.oprateType === 'write' ? 'write' : 'append'
|
const operateType = req.body.operateType === 'write' ? 'write' : 'append'
|
||||||
const data = req.file.buffer
|
const data = req.file.buffer
|
||||||
if (oprateType === 'write') {
|
if (operateType === 'write') {
|
||||||
// 覆盖写入
|
// 覆盖写入
|
||||||
fs.writeFile(savePath, data, (err) => {
|
fs.writeFile(savePath, data, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|||||||
@@ -5,10 +5,13 @@ export const staticPath = path.join("./static");
|
|||||||
export const port = 17705;
|
export const port = 17705;
|
||||||
|
|
||||||
export const api = {
|
export const api = {
|
||||||
get: `/${"get"}`,
|
get: `/${'get'}`,
|
||||||
upload: `/${"upload"}/*`,
|
query: `/${'query'}`,
|
||||||
delete: `/${"delete"}/*`,
|
upload: `/${'upload'}/*`,
|
||||||
query: `/${"query"}`
|
delete: `/${'delete'}/*`,
|
||||||
|
rename: `/${'rename'}/*`,
|
||||||
|
mkdir: `/${'mkdir'}/*`,
|
||||||
|
cut: `/${'cut'}/*`
|
||||||
}
|
}
|
||||||
|
|
||||||
export default { staticPath, port, api }
|
export default { staticPath, port, api }
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import generateId from "./generateId.js"
|
import generateId from "./generateId.js"
|
||||||
|
|
||||||
// 一个文件操作事件
|
// 一个文件操作事件
|
||||||
class FsOprateEvent {
|
class FsOperateEvent {
|
||||||
constructor(event){
|
constructor(event){
|
||||||
this.status = 'progress'
|
this.status = 'progress'
|
||||||
this.uid = generateId()
|
this.uid = generateId()
|
||||||
@@ -31,7 +31,7 @@ class FsOprateEventCenter {
|
|||||||
* @returns {number} 事件id
|
* @returns {number} 事件id
|
||||||
*/
|
*/
|
||||||
push(event){
|
push(event){
|
||||||
const ev = new FsOprateEvent(event)
|
const ev = new FsOperateEvent(event)
|
||||||
const id = ev.uid
|
const id = ev.uid
|
||||||
this.eventMap[id] = {
|
this.eventMap[id] = {
|
||||||
event: ev,
|
event: ev,
|
||||||
@@ -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,9 @@ 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";
|
||||||
|
import useMkdirApi from "./api/useMkdirApi.js";
|
||||||
|
|
||||||
const { staticPath, port, api } = Config;
|
const { staticPath, port, api } = Config;
|
||||||
|
|
||||||
@@ -21,6 +24,9 @@ useGetApi(app);
|
|||||||
useUploadApi(app);
|
useUploadApi(app);
|
||||||
useDeleteApi(app);
|
useDeleteApi(app);
|
||||||
useQueryApi(app);
|
useQueryApi(app);
|
||||||
|
useRenameApi(app);
|
||||||
|
useCutApi(app);
|
||||||
|
useMkdirApi(app);
|
||||||
|
|
||||||
app.listen(port, "::", () => {
|
app.listen(port, "::", () => {
|
||||||
const interfaces = os.networkInterfaces();
|
const interfaces = os.networkInterfaces();
|
||||||
|
|||||||
Reference in new issue
Block a user