From 921a61132c85e73c00a6f6f0a3c1bf3b71f2aba5 Mon Sep 17 00:00:00 2001 From: zhoulisheng1 Date: Thu, 5 Jan 2023 00:33:17 -0800 Subject: [PATCH] =?UTF-8?q?1.networkmanage=20=E6=96=87=E4=BB=B6=E5=A4=B9?= =?UTF-8?q?=E4=B8=8B=E7=9A=84=E4=BB=A3=E7=A0=81=E8=BF=9B=E8=A1=8C=E4=BA=86?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=202.=E5=B0=86IDataFetch=E4=B8=AD?= =?UTF-8?q?=E7=9A=84loadData=E6=8E=A5=E5=8F=A3=E7=9A=84=20onComplete=20onE?= =?UTF-8?q?rror=20=E5=A3=B0=E6=98=8E=E4=BA=86=E5=AF=B9=E8=B1=A1=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhoulisheng1 --- .../networkmanage/DownloadClient.ets | 31 +++++++++---------- .../imageknife/networkmanage/IDataFetch.ets | 4 +-- .../networkmanage/LoadLocalFileClient.ets | 22 ++++++------- .../networkmanage/NetworkDownloadClient.ets | 25 ++++++++------- .../requestmanage/RequestManager.ets | 5 ++- 5 files changed, 45 insertions(+), 42 deletions(-) diff --git a/imageknife/src/main/ets/components/imageknife/networkmanage/DownloadClient.ets b/imageknife/src/main/ets/components/imageknife/networkmanage/DownloadClient.ets index e94b7cc..485f1b3 100644 --- a/imageknife/src/main/ets/components/imageknife/networkmanage/DownloadClient.ets +++ b/imageknife/src/main/ets/components/imageknife/networkmanage/DownloadClient.ets @@ -13,12 +13,12 @@ * limitations under the License. */ -import { IDataFetch } from "../networkmanage/IDataFetch" -import { RequestOption } from "../RequestOption" -import { Md5 } from "../../cache/Md5" -import { FileUtils } from "../../cache/FileUtils" -import {NetworkDownloadClient} from'./NetworkDownloadClient' -import {LoadLocalFileClient} from'./LoadLocalFileClient' +import { IDataFetch } from '../networkmanage/IDataFetch' +import { RequestOption } from '../RequestOption' +import { Md5 } from '../../cache/Md5' +import { FileUtils } from '../../cache/FileUtils' +import { NetworkDownloadClient } from './NetworkDownloadClient' +import { LoadLocalFileClient } from './LoadLocalFileClient' import loadRequest from '@ohos.request'; // 数据加载器 @@ -26,17 +26,16 @@ export class DownloadClient implements IDataFetch { private networkClient = new NetworkDownloadClient(); private localFileClient = new LoadLocalFileClient(); - loadData(request: RequestOption, onCompleteFunction, onErrorFunction) { - if(typeof request.loadSrc == 'string') { - if (request.loadSrc.startsWith(globalThis.ImageKnife.getImageKnifeContext() - .filesDir) || request.loadSrc.startsWith(globalThis.ImageKnife.getImageKnifeContext().cacheDir)) { - // 本地沙盒 - this.localFileClient.loadLocalFileData(request,onCompleteFunction,onErrorFunction) - }else{ - // 网络下载 - this.networkClient.downloadNetworkData(request,onCompleteFunction,onErrorFunction) - } + if (typeof request.loadSrc == 'string') { + if (request.loadSrc.startsWith(globalThis.ImageKnife.getImageKnifeContext() + .filesDir) || request.loadSrc.startsWith(globalThis.ImageKnife.getImageKnifeContext().cacheDir)) { + // 本地沙盒 + this.localFileClient.loadData(request, onCompleteFunction, onErrorFunction) + } else { + // 网络下载 + this.networkClient.loadData(request, onCompleteFunction, onErrorFunction) } + } } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/networkmanage/IDataFetch.ets b/imageknife/src/main/ets/components/imageknife/networkmanage/IDataFetch.ets index 4a3340b..019d763 100644 --- a/imageknife/src/main/ets/components/imageknife/networkmanage/IDataFetch.ets +++ b/imageknife/src/main/ets/components/imageknife/networkmanage/IDataFetch.ets @@ -13,9 +13,9 @@ * limitations under the License. */ -import {RequestOption} from "../RequestOption" +import { RequestOption } from '../RequestOption' // 资源加载接口 export interface IDataFetch { - loadData(request: RequestOption, onCompleteFunction, onErrorFunction); + loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void); } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/networkmanage/LoadLocalFileClient.ets b/imageknife/src/main/ets/components/imageknife/networkmanage/LoadLocalFileClient.ets index 0218b77..4998b72 100644 --- a/imageknife/src/main/ets/components/imageknife/networkmanage/LoadLocalFileClient.ets +++ b/imageknife/src/main/ets/components/imageknife/networkmanage/LoadLocalFileClient.ets @@ -13,20 +13,20 @@ * limitations under the License. */ -import { IDataFetch } from "../networkmanage/IDataFetch" -import { RequestOption } from "../RequestOption" -import { Md5 } from "../../cache/Md5" -import { FileUtils } from "../../cache/FileUtils" +import { IDataFetch } from '../networkmanage/IDataFetch' +import { RequestOption } from '../RequestOption' +import { Md5 } from '../../cache/Md5' +import { FileUtils } from '../../cache/FileUtils' import loadRequest from '@ohos.request'; export class LoadLocalFileClient { - loadLocalFileData(request: RequestOption, onCompleteFunction, onErrorFunction) { - if(typeof request.loadSrc == 'string') { - let fileBuffer = FileUtils.getInstance().readFilePic(request.loadSrc) - if(fileBuffer == null || fileBuffer.byteLength <=0){ - onErrorFunction('LoadLocalFileClient loadLocalFileData The File Does Not Exist!Check The File!') - }else{ - onCompleteFunction(fileBuffer); + 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); } } } diff --git a/imageknife/src/main/ets/components/imageknife/networkmanage/NetworkDownloadClient.ets b/imageknife/src/main/ets/components/imageknife/networkmanage/NetworkDownloadClient.ets index a848e56..d7b33c5 100644 --- a/imageknife/src/main/ets/components/imageknife/networkmanage/NetworkDownloadClient.ets +++ b/imageknife/src/main/ets/components/imageknife/networkmanage/NetworkDownloadClient.ets @@ -13,15 +13,15 @@ * limitations under the License. */ -import { IDataFetch } from "../networkmanage/IDataFetch" -import { RequestOption } from "../RequestOption" -import { Md5 } from "../../cache/Md5" -import { FileUtils } from "../../cache/FileUtils" +import { IDataFetch } from '../networkmanage/IDataFetch' +import { RequestOption } from '../RequestOption' +import { Md5 } from '../../cache/Md5' +import { FileUtils } from '../../cache/FileUtils' import loadRequest from '@ohos.request'; // 数据加载器 -export class NetworkDownloadClient { - downloadNetworkData(request: RequestOption, onCompleteFunction, onErrorFunction) { +export class NetworkDownloadClient { + loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) { let filename = Md5.hashStr(request.generateDataKey); let downloadFolder = request.getFilesPath() + "/" + request.networkCacheFolder; let allpath = request.getFilesPath() + "/" + request.networkCacheFolder + "/" + filename + ".img"; @@ -42,7 +42,7 @@ export class NetworkDownloadClient { loadTask = downloadTask; loadTask.on('progress', (receivedSize, totalSize) => { - if(totalSize > 0) { + if (totalSize > 0) { // 并不是所有服务器都会返回totalSize 当没有文件大小的时候,下载进度没有百分比回调,只能知道目前下载了多少数据量 let percent = Math.round(((receivedSize * 1.0) / (totalSize * 1.0)) * 100) if (request.progressFunc) { @@ -55,7 +55,7 @@ export class NetworkDownloadClient { let downloadPath = allpath; request.downloadFilePath = downloadPath; let arraybuffer = FileUtils.getInstance().readFilePic(downloadPath) - onCompleteFunction(arraybuffer); + onComplete(arraybuffer); FileUtils.getInstance().deleteFile(downloadPath); loadTask.off('complete', () => { @@ -83,7 +83,7 @@ export class NetworkDownloadClient { }) loadTask.on('fail', (err) => { - onErrorFunction('NetworkDownloadClient Download task fail err =' + err) + onError('NetworkDownloadClient Download task fail err =' + err) if (loadTask) { loadTask.remove().then(result => { loadTask.off('complete', () => { @@ -109,10 +109,11 @@ export class NetworkDownloadClient { }) } else { - onErrorFunction('NetworkDownloadClient downloadTask dismiss!') + onError('NetworkDownloadClient downloadTask dismiss!') } - }).catch((err) => { - onErrorFunction(err) + }) + .catch((err) => { + onError("下载子系统download错误捕获,error="+err.message); }) } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets b/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets index 0a9a3a8..5ffb29d 100644 --- a/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets +++ b/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets @@ -190,7 +190,10 @@ export class RequestManager { let success = (arraybuffer) => { this.downloadSuccess(arraybuffer, onComplete, onError) } - this.mIDataFetch.loadData(request, success, onError); + let error = (errorMsg:string) =>{ + onError(errorMsg) + } + this.mIDataFetch.loadData(request, success, error); } // 加载本地资源