From 084eca3e069de5b71af8f54f184f30e35932fec5 Mon Sep 17 00:00:00 2001 From: zyk <474964724@qq.com> Date: Sun, 20 Mar 2022 16:39:41 +0800 Subject: [PATCH] =?UTF-8?q?'=E5=A2=9E=E5=8A=A0=E6=96=87=E4=BB=B6=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E6=A8=A1=E5=9D=97'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/tools/FileUtils.ts | 193 ++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 src/main/tools/FileUtils.ts diff --git a/src/main/tools/FileUtils.ts b/src/main/tools/FileUtils.ts new file mode 100644 index 0000000..0546fa5 --- /dev/null +++ b/src/main/tools/FileUtils.ts @@ -0,0 +1,193 @@ +import { + readFile, + writeFile, + mkdir, + unlink, + createReadStream, + createWriteStream, + existsSync, + rename, + access, + readdir, + stat, + readdirSync, + statSync, + unlinkSync, + rmdirSync, + Stats +} from 'fs' +import ElectronLog from 'electron-log' +import axios from 'axios' +import { extname, join } from 'path' + +class FileUtils { + // isBuffer: boolean + // isPath: boolean + // buffer: Buffer + // path: string + + + + // static fromBase64(base64: string) { + // const fileUtils = new FileUtils() + // fileUtils.buffer = Buffer.from(base64, 'base64') + // return fileUtils + // } + + // static async fromPath(path: string) { + // if (await this.access(path)) { + // const fileUtils = new FileUtils() + // fileUtils.path = path + // return fileUtils + // } + // } + + static async downloadFromHttp(sourcePath: string, targetPath: string): Promise { + return await new Promise(resolve => { + axios({ + method: 'get', + url: sourcePath, + responseType: 'stream' + }).then(response => { + try { + response.data.pipe(createWriteStream(targetPath)).on('end', async () => { + resolve(await this.access(targetPath)) + }) + } catch (err) { + this.error('downloadFromHttp stream err', err) + resolve(false) + } + }).catch(err => { + this.error('downloadFromHttp err', err) + resolve(false) + }) + }) + } + + static readFile (filePath: string, options = {}): Promise { + return new Promise(resolve => { + readFile(filePath, options, (err, data) => { + if (err) { + this.error('FileUtils readFile', err) + resolve(null) + } else { + resolve(data) + } + }) + }) + } + + static writeFile (filePath: string, buffer: any, options = {}): Promise { + return new Promise(resolve => { + writeFile(filePath, buffer, options, (err) => { + if (err) this.error('FileUtils writeFile', err) + resolve(!err) + }) + }) + } + + static mkdir (filePath: string, options = {}): Promise { + return new Promise(resolve => { + mkdir(filePath, options, (err) => { + if (err) this.error('FileUtils mkdir', err) + resolve(!err) + }) + }) + } + + static unlink (filePath: string):Promise { + return new Promise(resolve => { + unlink(filePath, (err) => { + if (err) this.error('FileUtils unlink', err) + resolve(!err) + }) + }) + } + + static copyFile (sourcePath: string, targetPath: string):Promise { + return new Promise(resolve => { + let error = false + const source = createReadStream(sourcePath) + const target = createWriteStream(targetPath) + source.on('error', () => { error = true }) + target + .on('error', () => { error = true }) + .on('close', () => { + const exists = existsSync(targetPath) + if (error) { + if (exists) { + unlinkSync(targetPath) + } + } + resolve(!error && exists) + }) + source.pipe(target) + }) + } + + static rename (oldPath: string, newPath: string) { + return new Promise(resolve => { + rename(oldPath, newPath, err => { + if (err) this.error('FileUtils rename', err) + resolve(!err) + }) + }) + } + + static access (filePath: string): Promise { + return new Promise(resolve => { + access(filePath, err => { + // if (err) this.error('FileUtils access', err) + resolve(!err) + }) + }) + } + + static existsSync (filePath: string) { + return existsSync(filePath) + } + + static readdir (filePath: string): Promise> { + return new Promise(resolve => { + readdir(filePath, (err, files) => { + if (err) { + this.error('FileUtils readdir', err) + resolve([]) + } else { + resolve(files) + } + }) + }) + } + + static stat (filePath: string): Promise { + return new Promise(resolve => { + stat(filePath, (err, stats) => { + if (err) { + this.error('FileUtils stat', err) + resolve(false) + } else { + resolve(stats) + } + }) + }) + } + + static rmRf(filePath: string) { + if (existsSync(filePath)) { + readdirSync(filePath).forEach(file => { + let curPath = filePath + '/' + file + if (statSync(curPath).isDirectory()) { + this.rmRf(curPath) + } else { + unlinkSync(curPath) + } + }) + rmdirSync(filePath) + } + } + + private static error(...data: any[]) { + ElectronLog.error(...data) + } +} \ No newline at end of file -- Gitee