46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
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';
|
|
|
|
class FileService {
|
|
|
|
static async readPath(pathStr: string): Promise<Array<FileObj>> {
|
|
let fileList = new Array();
|
|
if (pathStr.trim().length == 0) {
|
|
//获取根目录路径
|
|
if (config.isWindows) {
|
|
//windows下
|
|
let std: string = (await ProcessHelper.exec("wmic logicaldisk get caption") as Object)['stdout'].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 resList = new Array();
|
|
for (let index in fileList) {
|
|
try {
|
|
let stat = await fs.stat(path.join(pathStr, fileList[index]));
|
|
resList.push(new FileObj(fileList[index], stat.isDirectory(), stat.birthtime.getTime(), stat.mtime.getTime()));
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
}
|
|
return resList;
|
|
}
|
|
|
|
static async createPath(pathStr: string) {
|
|
await fs.ensureDir(pathStr);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
export default FileService;
|