1.FileUtils新增异步读取文件方法 readFilePicAsync
2.网络下载和本地文件读取部分全都换成异步读取文件方法 Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
parent
d043745098
commit
eeb25ae8d1
|
@ -16,11 +16,10 @@
|
|||
import fs from '@ohos.file.fs';
|
||||
|
||||
export class FileUtils {
|
||||
base64Str: string= ''
|
||||
|
||||
base64Str: string = ''
|
||||
private static sInstance: FileUtils;
|
||||
|
||||
public static getInstance(): FileUtils{
|
||||
public static getInstance(): FileUtils {
|
||||
if (!this.sInstance) {
|
||||
this.sInstance = new FileUtils();
|
||||
}
|
||||
|
@ -32,34 +31,55 @@ export class FileUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* 新建文件
|
||||
* @param path 文件绝对路径及文件名
|
||||
* @return number 文件句柄id
|
||||
*/
|
||||
createFile(path: string): number{
|
||||
* 新建文件
|
||||
* @param path 文件绝对路径及文件名
|
||||
* @return number 文件句柄id
|
||||
*/
|
||||
createFile(path: string): number {
|
||||
return fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE).fd
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param path 文件绝对路径及文件名
|
||||
*/
|
||||
deleteFile(path: string):void {
|
||||
* 删除文件
|
||||
* @param path 文件绝对路径及文件名
|
||||
*/
|
||||
deleteFile(path: string): void {
|
||||
try {
|
||||
let fileExist = fs.accessSync(path);
|
||||
if(fileExist) {
|
||||
if (fileExist) {
|
||||
fs.unlinkSync(path);
|
||||
}
|
||||
}catch (err){
|
||||
console.log("FileUtils deleteFile Method has error, err msg="+err.message + " err code="+err.code);
|
||||
} catch (err) {
|
||||
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);
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 同步删除文件目录 必须保证文件夹里面没有文件
|
||||
* @param path 待删除目录的绝对路径
|
||||
*/
|
||||
deleteFolderSync(path: string):void {
|
||||
deleteFolderSync(path: string): void {
|
||||
if (this.existFolder(path)) {
|
||||
fs.rmdirSync(path);
|
||||
}
|
||||
|
@ -87,7 +107,7 @@ export class FileUtils {
|
|||
/**
|
||||
* 清空已有文件数据
|
||||
*/
|
||||
clearFile(path: string):number {
|
||||
clearFile(path: string): number {
|
||||
return fs.openSync(path, fs.OpenMode.TRUNC).fd
|
||||
}
|
||||
|
||||
|
@ -113,7 +133,7 @@ export class FileUtils {
|
|||
try {
|
||||
let fd = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE).fd
|
||||
let stat = fs.statSync(path)
|
||||
console.info("FileUtils - writeData size = " + stat.size +" path="+path);
|
||||
console.info("FileUtils - writeData size = " + stat.size + " path=" + path);
|
||||
fs.writeSync(fd, content, { offset: stat.size })
|
||||
fs.closeSync(fd)
|
||||
} catch (e) {
|
||||
|
@ -124,7 +144,7 @@ export class FileUtils {
|
|||
/**
|
||||
* 判断path文件是否存在
|
||||
*/
|
||||
exist(path: string): boolean{
|
||||
exist(path: string): boolean {
|
||||
try {
|
||||
let stat = fs.statSync(path)
|
||||
return stat.isFile()
|
||||
|
@ -148,7 +168,7 @@ export class FileUtils {
|
|||
/**
|
||||
* 获取path的文件大小
|
||||
*/
|
||||
getFileSize(path: string): number{
|
||||
getFileSize(path: string): number {
|
||||
try {
|
||||
let stat = fs.statSync(path)
|
||||
return stat.size
|
||||
|
@ -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);
|
||||
})
|
||||
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
@ -231,7 +278,7 @@ export class FileUtils {
|
|||
* 判断文件夹是否存在
|
||||
* @param 文件夹绝对路径
|
||||
*/
|
||||
existFolder(path: string): boolean{
|
||||
existFolder(path: string): boolean {
|
||||
try {
|
||||
let stat = fs.statSync(path)
|
||||
return stat.isDirectory()
|
||||
|
@ -243,9 +290,9 @@ export class FileUtils {
|
|||
|
||||
/**
|
||||
* 如果文件夹不存在则创建一个文件夹 然后在其中创建文件 并且将数据写入进文件
|
||||
* @param folder 文件夹绝对路径
|
||||
* @param file 文件绝对路径
|
||||
* @param content 文件内容数据
|
||||
* @param folder 文件夹绝对路径
|
||||
* @param file 文件绝对路径
|
||||
* @param content 文件内容数据
|
||||
*/
|
||||
createFileProcess(folder: string, file: string, content: ArrayBuffer | string) {
|
||||
//创建文件夹
|
||||
|
@ -260,7 +307,7 @@ export class FileUtils {
|
|||
* string 转 Uint8Array
|
||||
* @param str 输入String
|
||||
*/
|
||||
stringToUint8Array(str): Uint8Array{
|
||||
stringToUint8Array(str): Uint8Array {
|
||||
var arr = [];
|
||||
for (var i = 0, j = str.length; i < j; ++i) {
|
||||
arr.push(str.charCodeAt(i));
|
||||
|
@ -271,7 +318,7 @@ export class FileUtils {
|
|||
|
||||
/**
|
||||
* int 转 byte[]
|
||||
* @param n 输入int
|
||||
* @param n 输入int
|
||||
*/
|
||||
intTobytes2(n) {
|
||||
var bytes = [];
|
||||
|
@ -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> {
|
||||
|
|
|
@ -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)
|
||||
if (fileBuffer == null || fileBuffer.byteLength <= 0) {
|
||||
onError('LoadLocalFileClient loadLocalFileData The File Does Not Exist!Check The File!')
|
||||
} else {
|
||||
onComplete(fileBuffer);
|
||||
}
|
||||
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)
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
onComplete(arraybuffer);
|
||||
FileUtils.getInstance().deleteFile(downloadPath);
|
||||
FileUtils.getInstance().readFilePicAsync(downloadPath).then(arraybuffer=>{
|
||||
onComplete(arraybuffer);
|
||||
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', () => {
|
||||
|
||||
|
|
Loading…
Reference in New Issue