新增 文件管理分页功能

This commit is contained in:
Suwings 2022-04-30 19:23:02 +08:00
parent 2a85a5f542
commit 8061f23a25
2 changed files with 16 additions and 5 deletions

View File

@ -42,8 +42,9 @@ routerApp.use((event, ctx, data, next) => {
routerApp.on("file/list", (ctx, data) => {
try {
const fileManager = getFileManager(data.instanceUuid);
fileManager.cd(data.target);
const overview = fileManager.list();
const { page, pageSize, target } = data;
const overview = fileManager.list(page, pageSize);
fileManager.cd(target);
protocol.response(ctx, overview);
} catch (error) {
protocol.responseError(ctx, error);

View File

@ -69,8 +69,13 @@ export default class FileManager {
this.cwd = path.normalize(path.join(this.cwd, dirName));
}
list() {
const fileNames = fs.readdirSync(this.toAbsolutePath());
list(page: 0, pageSize = 40) {
if (pageSize > 100 || pageSize <= 0 || page < 0) throw new Error("Beyond the value limit");
let fileNames = fs.readdirSync(this.toAbsolutePath());
const total = fileNames.length;
const sliceStart = page * pageSize;
const sliceEnd = sliceStart + pageSize;
fileNames = fileNames.slice(sliceStart, sliceEnd);
const files: IFile[] = [];
const dirs: IFile[] = [];
fileNames.forEach((name) => {
@ -94,7 +99,12 @@ export default class FileManager {
files.sort((a, b) => (a.name > b.name ? 1 : -1));
dirs.sort((a, b) => (a.name > b.name ? 1 : -1));
const resultList = dirs.concat(files);
return resultList;
return {
items: resultList,
page,
pageSize,
total
};
}
async readFile(fileName: string) {