1.FileUtils新增异步读取文件方法 readFilePicAsync

2.网络下载和本地文件读取部分全都换成异步读取文件方法

Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
zhoulisheng1 2023-05-24 11:01:19 +08:00
parent d043745098
commit eeb25ae8d1
3 changed files with 100 additions and 39 deletions

View File

@ -17,7 +17,6 @@ import fs from '@ohos.file.fs';
export class FileUtils {
base64Str: string = ''
private static sInstance: FileUtils;
public static getInstance(): FileUtils {
@ -54,6 +53,27 @@ export class FileUtils {
console.log("FileUtils deleteFile Method has error, err msg=" + err.message + " err code=" + err.code);
}
}
/**
* 异步删除文件
* @param path 文件绝对路径及文件名
*/
deleteFileAsync(path: string): Promise<void> {
return new Promise((resolve,reject)=>{
fs.access(path).then(fileExist =>{
if (fileExist) {
fs.unlink(path).then(()=>{
resolve();
}).catch(err=>{
reject(err)
})
}
}).catch(err=>{
reject(err);
})
})
}
/**
* 同步删除文件目录 必须保证文件夹里面没有文件
@ -159,22 +179,49 @@ export class FileUtils {
}
/**
* 读取路径path的文件
* 同步读取路径path的文件
*/
readFilePic(path: string): ArrayBuffer {
try {
let stat = fs.statSync(path)
let fd = fs.openSync(path, fs.OpenMode.READ_WRITE).fd;
let fd = fs.openSync(path, fs.OpenMode.READ_ONLY).fd;
let length = stat.size
let buf = new ArrayBuffer(length);
fs.readSync(fd, buf)
fs.closeSync(fd)
return buf
} catch (e) {
console.log("FileUtils - readFilePic " + e)
console.log("FileUtils - readFilePicSync " + e)
return new ArrayBuffer(0)
}
}
/**
* 异步读取路径path的文件
*/
readFilePicAsync(path: string): Promise<ArrayBuffer> {
return new Promise((resolve,reject)=>{
fs.open(path, fs.OpenMode.READ_ONLY).then((file) => {
let stat = fs.statSync(path)
let fd = file.fd;
let length = stat.size;
let buf = new ArrayBuffer(length);
fs.read(fd,buf).then((readLen)=>{
// 关闭文件
fs.closeSync(file);
resolve(buf);
}).catch(err=>{
reject(err);
})
}).catch(err=>{
reject(err);
})
})
}
/**
@ -285,7 +332,6 @@ export class FileUtils {
uint8ArrayToBuffer(array: Uint8Array): ArrayBuffer {
return array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset)
}
}
export interface AsyncCallback<T> {

View File

@ -20,12 +20,16 @@ import { FileUtils } from '../../cache/FileUtils'
export class LoadLocalFileClient implements IDataFetch {
loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) {
if (typeof request.loadSrc == 'string') {
let fileBuffer = FileUtils.getInstance().readFilePic(request.loadSrc)
FileUtils.getInstance().readFilePicAsync(request.loadSrc).then(fileBuffer=>{
if (fileBuffer == null || fileBuffer.byteLength <= 0) {
onError('LoadLocalFileClient loadLocalFileData The File Does Not Exist!Check The File!')
} else {
onComplete(fileBuffer);
}
}).catch(err=>{
onError('LoadLocalFileClient loadLocalFileData Error Msg ='+err?.message)
})
}
}
}

View File

@ -18,6 +18,7 @@ import { RequestOption } from '../RequestOption'
import { SparkMD5 } from '../../3rd_party/sparkmd5/spark-md5'
import { FileUtils } from '../../cache/FileUtils'
import loadRequest from '@ohos.request';
import { LogUtil } from '../utils/LogUtil'
// 数据加载器
export class NetworkDownloadClient implements IDataFetch {
@ -28,6 +29,8 @@ export class NetworkDownloadClient implements IDataFetch {
if (!FileUtils.getInstance().existFolder(downloadFolder)) {
FileUtils.getInstance().createFolder(downloadFolder)
}
// 理论上来讲不会进入这里来删除文件,但是如果存在文件,则需要同步删除保证后续文件下载没问题。
if (FileUtils.getInstance().exist(allpath)) {
FileUtils.getInstance().deleteFile(allpath)
}
@ -55,9 +58,17 @@ export class NetworkDownloadClient implements IDataFetch {
loadTask.on('complete', () => {
let downloadPath = allpath;
request.downloadFilePath = downloadPath;
let arraybuffer = FileUtils.getInstance().readFilePic(downloadPath)
FileUtils.getInstance().readFilePicAsync(downloadPath).then(arraybuffer=>{
onComplete(arraybuffer);
FileUtils.getInstance().deleteFile(downloadPath);
FileUtils.getInstance().deleteFileAsync(downloadPath).then(()=>{
LogUtil.log('文件名:'+downloadPath+" 文件删除成功!")
}).catch(err=>{
LogUtil.log('文件名:'+downloadPath+" 文件删除失败!")
});
}).catch(err=>{
onError('NetworkDownloadClient Read File Async Error Msg='+ err?.message)
})
loadTask.off('complete', () => {