110 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-06-27 21:00:24 +08:00
import config from '../config';
import * as path from 'path';
import * as fs from 'fs-extra';
import ProcessHelper from '../util/ProcesHelper';
import FileObj from '../vo/FileObj';
2022-04-07 16:35:17 +08:00
let numberSet = new Set(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
2021-06-27 21:00:24 +08:00
class FileService {
2021-12-06 23:20:34 +08:00
static async readPath(pathStr: string, showHidden: boolean): Promise<Array<FileObj>> {
pathStr = decodeURIComponent(pathStr);
let fileList = new Array();
if (pathStr.trim().length == 0) {
//获取根目录路径
if (config.isWindows) {
//windows下
let std: string = (await ProcessHelper.exec('wmic logicaldisk get caption')).replace('Caption', '');
fileList = std
.split('\r\n')
.filter((item) => item.trim().length > 0)
.map((item) => item.trim());
} else {
//linux下
pathStr = '/';
fileList = await fs.readdir(pathStr);
}
} else {
fileList = await fs.readdir(pathStr);
}
let folderList: Array<FileObj> = new Array();
let files: Array<FileObj> = new Array();
for (let index in fileList) {
try {
let stat = await fs.stat(path.join(pathStr, fileList[index]));
if (fileList[index].startsWith('.')) {
if (showHidden) {
(stat.isDirectory() ? folderList : files).push(
new FileObj(fileList[index], pathStr, stat.isDirectory(), stat.birthtime.getTime(), stat.mtime.getTime()),
);
}
} else {
(stat.isDirectory() ? folderList : files).push(
new FileObj(fileList[index], pathStr, stat.isDirectory(), stat.birthtime.getTime(), stat.mtime.getTime()),
);
}
} catch (e) {
console.error(e);
}
}
2022-04-07 16:35:17 +08:00
folderList.sort((a, b) => FileService.compareStr(a.name, b.name)).push(...files.sort((a, b) => FileService.compareStr(a.name, b.name)));
2021-12-06 23:20:34 +08:00
return folderList;
}
2021-06-27 21:00:24 +08:00
2021-12-06 23:20:34 +08:00
static async checkExist(pathStr: string) {
return await fs.pathExists(pathStr);
}
2022-04-07 16:35:17 +08:00
/**
*
* @param a str
* @param b str
*/
static compareStr(a: string, b: string) {
let an = a.length;
let bn = b.length;
for (let i = 0; i < an;) {
let charA = FileService.readChar(a, i, an);
let charB = FileService.readChar(b, i, bn);
if (charB.length == 0) {
return 1;
}
if (charA !== charB) {
//读取字符串不相等说明可以得到排序结果
//如果都为数字,按照数字的比较方法,否则按照字符串比较
return numberSet.has(charA.charAt(0)) && numberSet.has(charB.charAt(0)) ? Number(charA) - Number(charB) : charA.localeCompare(charB);
}
i += charA.length;
}
//排到最后都没分结果说明相等
return 0;
}
/**
*
* @param a a
* @param n
*/
static readChar(a: string, i: number, n: number) {
let res = "";
for (; i < n; i++) {
let char = a.charAt(i);
if (numberSet.has(char)) {
//如果当前字符是数字,添加到结果中
res += char;
} else {
//如果不为数字但是为第一个字符直接返回否则返回res
if (res.length == 0) {
return char;
} else {
return res;
}
}
}
return res;
}
2021-06-27 21:00:24 +08:00
}
export default FileService;