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); } // 加载本地资源