diff --git a/imageknife/src/main/ets/components/imageknife/networkmanage/DownloadClient.ets b/imageknife/src/main/ets/components/imageknife/networkmanage/DownloadClient.ets index 1cc92fb..2ddf334 100644 --- a/imageknife/src/main/ets/components/imageknife/networkmanage/DownloadClient.ets +++ b/imageknife/src/main/ets/components/imageknife/networkmanage/DownloadClient.ets @@ -13,7 +13,7 @@ * limitations under the License. */ -import type { IDataFetch } from '../networkmanage/IDataFetch' +import { IDataFetch } from '../networkmanage/IDataFetch' import { RequestOption } from '../RequestOption' import { NetworkDownloadClient } from './NetworkDownloadClient' import { LoadLocalFileClient } from './LoadLocalFileClient' @@ -27,7 +27,7 @@ export class DownloadClient implements IDataFetch { private localFileClient = new LoadLocalFileClient(); private dataShareFileClient = new LoadDataShareFileClient(); - loadData(request: RequestOption, onCompleteFunction, onErrorFunction) { + loadData(request: RequestOption, onCompleteFunction:(img: ArrayBuffer) => void, onErrorFunction: (err: string) => void) { if (typeof request.loadSrc == 'string') { let fileDir:string = (ImageKnifeGlobal.getInstance().getHapContext() as Record).filesDir as string; let cacheDir:string = (ImageKnifeGlobal.getInstance().getHapContext() as Record).cacheDir as string diff --git a/imageknife/src/main/ets/components/imageknife/networkmanage/IDataFetch.ets b/imageknife/src/main/ets/components/imageknife/networkmanage/IDataFetch.ets index 019d763..1fafeb0 100644 --- a/imageknife/src/main/ets/components/imageknife/networkmanage/IDataFetch.ets +++ b/imageknife/src/main/ets/components/imageknife/networkmanage/IDataFetch.ets @@ -17,5 +17,5 @@ import { RequestOption } from '../RequestOption' // 资源加载接口 export interface IDataFetch { - loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void); + loadData:(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void)=>void; } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/networkmanage/LoadDataShareFileClient.ets b/imageknife/src/main/ets/components/imageknife/networkmanage/LoadDataShareFileClient.ets index 0e69f6f..6ba18ab 100644 --- a/imageknife/src/main/ets/components/imageknife/networkmanage/LoadDataShareFileClient.ets +++ b/imageknife/src/main/ets/components/imageknife/networkmanage/LoadDataShareFileClient.ets @@ -12,10 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import type { IDataFetch } from '../networkmanage/IDataFetch' +import { IDataFetch } from '../networkmanage/IDataFetch' import { RequestOption } from '../RequestOption' import fs from '@ohos.file.fs'; - +import { BusinessError } from '@ohos.base' export class LoadDataShareFileClient implements IDataFetch { loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) { @@ -26,13 +26,13 @@ export class LoadDataShareFileClient implements IDataFetch { fs.read(file.fd, buf).then((readLen) => { onComplete(buf); fs.close(file.fd); - }).catch(err => { + }).catch((err:BusinessError) => { onError('LoadDataShareFileClient fs.read err happend uri=' + request.loadSrc + " err.msg=" + err?.message + " err.code=" + err?.code) }) - }).catch(err => { + }).catch((err:BusinessError) => { onError('LoadDataShareFileClient fs.stat err happend uri=' + request.loadSrc + " err.msg=" + err?.message + " err.code=" + err?.code) }) - }).catch(err => { + }).catch((err:BusinessError) => { onError('LoadDataShareFileClient fs.open err happend uri=' + request.loadSrc + " err.msg=" + err?.message + " err.code=" + err?.code) }) } diff --git a/imageknife/src/main/ets/components/imageknife/networkmanage/LoadLocalFileClient.ets b/imageknife/src/main/ets/components/imageknife/networkmanage/LoadLocalFileClient.ets index 23f7030..89240af 100644 --- a/imageknife/src/main/ets/components/imageknife/networkmanage/LoadLocalFileClient.ets +++ b/imageknife/src/main/ets/components/imageknife/networkmanage/LoadLocalFileClient.ets @@ -13,10 +13,10 @@ * limitations under the License. */ -import type { IDataFetch } from '../networkmanage/IDataFetch' +import { IDataFetch } from '../networkmanage/IDataFetch' import { RequestOption } from '../RequestOption' import { FileUtils } from '../../cache/FileUtils' - +import { BusinessError } from '@ohos.base' export class LoadLocalFileClient implements IDataFetch { loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) { if (typeof request.loadSrc == 'string') { @@ -26,7 +26,7 @@ export class LoadLocalFileClient implements IDataFetch { } else { onComplete(fileBuffer); } - }).catch(err=>{ + }).catch((err:BusinessError)=>{ onError('LoadLocalFileClient loadLocalFileData Error Msg ='+err?.message) }) diff --git a/imageknife/src/main/ets/components/imageknife/networkmanage/NetworkDownloadClient.ets b/imageknife/src/main/ets/components/imageknife/networkmanage/NetworkDownloadClient.ets index d3cf5ab..5dc5ec7 100644 --- a/imageknife/src/main/ets/components/imageknife/networkmanage/NetworkDownloadClient.ets +++ b/imageknife/src/main/ets/components/imageknife/networkmanage/NetworkDownloadClient.ets @@ -13,7 +13,7 @@ * limitations under the License. */ -import type { IDataFetch } from '../networkmanage/IDataFetch' +import { IDataFetch } from '../networkmanage/IDataFetch' import { RequestOption } from '../RequestOption' import { SparkMD5 } from '../../3rd_party/sparkmd5/spark-md5' import { FileUtils } from '../../cache/FileUtils' @@ -21,6 +21,7 @@ import loadRequest from '@ohos.request'; import { LogUtil } from '../utils/LogUtil' import { ImageKnifeGlobal } from '../ImageKnifeGlobal' import common from '@ohos.app.ability.common' +import { BusinessError } from '@ohos.base' // 数据加载器 export class NetworkDownloadClient implements IDataFetch { loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) { @@ -35,23 +36,23 @@ export class NetworkDownloadClient implements IDataFetch { if (FileUtils.getInstance().exist(allpath)) { FileUtils.getInstance().deleteFile(allpath) } - var downloadConfig = { + let downloadConfig:loadRequest.DownloadConfig = { url: (request.loadSrc as string), filePath: allpath, // 允许计费流量下载 enableMetered: true, }; - let loadTask = null; - loadRequest.downloadFile( (ImageKnifeGlobal.getInstance().getHapContext() as common.BaseContext ), downloadConfig).then(downloadTask => { + + loadRequest.downloadFile( (ImageKnifeGlobal.getInstance().getHapContext() as common.BaseContext ), downloadConfig).then((downloadTask:loadRequest.DownloadTask) => { if (downloadTask) { - loadTask = downloadTask; + let loadTask:loadRequest.DownloadTask | null = downloadTask; loadTask.on('progress', (receivedSize, totalSize) => { if (totalSize > 0) { // 并不是所有服务器都会返回totalSize 当没有文件大小的时候,下载进度没有百分比回调,只能知道目前下载了多少数据量 let percent = Math.round(((receivedSize * 1.0) / (totalSize * 1.0)) * 100) if (request.progressFunc) { - request.progressFunc(percent); + request.progressFunc.asyncSuccess(percent); } } }); @@ -63,69 +64,50 @@ export class NetworkDownloadClient implements IDataFetch { onComplete(arraybuffer); FileUtils.getInstance().deleteFileAsync(downloadPath).then(()=>{ LogUtil.log('文件名:'+downloadPath+" 文件删除成功!") - }).catch(err=>{ + }).catch((err:BusinessError)=>{ LogUtil.log('文件名:'+downloadPath+" 文件删除失败!") }); - }).catch(err=>{ - onError('NetworkDownloadClient Read File Async Error Msg='+ err?.message) + }).catch((err:BusinessError)=>{ + onError('NetworkDownloadClient Read File Async Error Msg='+ (err as BusinessError)?.message) }) - - loadTask.off('complete', () => { - - }) - - loadTask.off('pause', () => { - }) - - loadTask.off('remove', () => { - }) - - loadTask.off('progress', () => { - }) - - loadTask.off('fail', () => { - }) - loadTask = null; + if(loadTask != null) { + loadTask.off('complete', () => {}) + loadTask.off('pause', () => {}) + loadTask.off('remove', () => {}) + loadTask.off('progress', () => {}) + loadTask.off('fail', () => {}) + loadTask = null; + } }) - loadTask.on('pause', () => { - }) + loadTask.on('pause', () => {}) - loadTask.on('remove', () => { - }) + loadTask.on('remove', () => {}) loadTask.on('fail', (err) => { onError('NetworkDownloadClient Download task fail err =' + err) - if (loadTask) { - loadTask.remove().then(result => { - loadTask.off('complete', () => { - }) - - loadTask.off('pause', () => { - }) - - loadTask.off('remove', () => { - }) - - loadTask.off('progress', () => { - }) - - loadTask.off('fail', () => { - }) - loadTask = null - }).catch(err => { + if (loadTask!=null) { + loadTask.delete().then(result => { + if(loadTask != undefined) { + loadTask.off('complete', () => {}) + loadTask.off('pause', () => {}) + loadTask.off('remove', () => {}) + loadTask.off('progress', () => {}) + loadTask.off('fail', () => {}) + loadTask = null + } + }).catch((err:BusinessError) => { loadTask = null; console.log('NetworkDownloadClient Download task fail err =' + err); }) } }) - } else { onError('NetworkDownloadClient downloadTask dismiss!') } }) - .catch((err) => { + .catch((err:BusinessError)=> { onError("下载子系统download错误捕获,error=" + err.message); }) }