Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit 8061f23

Browse files
committed
新增 文件管理分页功能
1 parent 2a85a5f commit 8061f23

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/routers/file_router.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ routerApp.use((event, ctx, data, next) => {
4242
routerApp.on("file/list", (ctx, data) => {
4343
try {
4444
const fileManager = getFileManager(data.instanceUuid);
45-
fileManager.cd(data.target);
46-
const overview = fileManager.list();
45+
const { page, pageSize, target } = data;
46+
const overview = fileManager.list(page, pageSize);
47+
fileManager.cd(target);
4748
protocol.response(ctx, overview);
4849
} catch (error) {
4950
protocol.responseError(ctx, error);

src/service/system_file.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ export default class FileManager {
6969
this.cwd = path.normalize(path.join(this.cwd, dirName));
7070
}
7171

72-
list() {
73-
const fileNames = fs.readdirSync(this.toAbsolutePath());
72+
list(page: 0, pageSize = 40) {
73+
if (pageSize > 100 || pageSize <= 0 || page < 0) throw new Error("Beyond the value limit");
74+
let fileNames = fs.readdirSync(this.toAbsolutePath());
75+
const total = fileNames.length;
76+
const sliceStart = page * pageSize;
77+
const sliceEnd = sliceStart + pageSize;
78+
fileNames = fileNames.slice(sliceStart, sliceEnd);
7479
const files: IFile[] = [];
7580
const dirs: IFile[] = [];
7681
fileNames.forEach((name) => {
@@ -94,7 +99,12 @@ export default class FileManager {
9499
files.sort((a, b) => (a.name > b.name ? 1 : -1));
95100
dirs.sort((a, b) => (a.name > b.name ? 1 : -1));
96101
const resultList = dirs.concat(files);
97-
return resultList;
102+
return {
103+
items: resultList,
104+
page,
105+
pageSize,
106+
total
107+
};
98108
}
99109

100110
async readFile(fileName: string) {

0 commit comments

Comments
 (0)