forked from mirror/MCSM-Daemon
新增 文件管理分页功能
This commit is contained in:
parent
2a85a5f542
commit
8061f23a25
@ -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);
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user