forked from floraachy/ImageKnife
1.networkmanage 文件夹下的代码进行了格式化
2.将IDataFetch中的loadData接口的 onComplete onError 声明了对象类型 Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
parent
619103145c
commit
921a61132c
|
@ -13,12 +13,12 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { IDataFetch } from "../networkmanage/IDataFetch"
|
import { IDataFetch } from '../networkmanage/IDataFetch'
|
||||||
import { RequestOption } from "../RequestOption"
|
import { RequestOption } from '../RequestOption'
|
||||||
import { Md5 } from "../../cache/Md5"
|
import { Md5 } from '../../cache/Md5'
|
||||||
import { FileUtils } from "../../cache/FileUtils"
|
import { FileUtils } from '../../cache/FileUtils'
|
||||||
import {NetworkDownloadClient} from'./NetworkDownloadClient'
|
import { NetworkDownloadClient } from './NetworkDownloadClient'
|
||||||
import {LoadLocalFileClient} from'./LoadLocalFileClient'
|
import { LoadLocalFileClient } from './LoadLocalFileClient'
|
||||||
import loadRequest from '@ohos.request';
|
import loadRequest from '@ohos.request';
|
||||||
|
|
||||||
// 数据加载器
|
// 数据加载器
|
||||||
|
@ -26,16 +26,15 @@ export class DownloadClient implements IDataFetch {
|
||||||
private networkClient = new NetworkDownloadClient();
|
private networkClient = new NetworkDownloadClient();
|
||||||
private localFileClient = new LoadLocalFileClient();
|
private localFileClient = new LoadLocalFileClient();
|
||||||
|
|
||||||
|
|
||||||
loadData(request: RequestOption, onCompleteFunction, onErrorFunction) {
|
loadData(request: RequestOption, onCompleteFunction, onErrorFunction) {
|
||||||
if(typeof request.loadSrc == 'string') {
|
if (typeof request.loadSrc == 'string') {
|
||||||
if (request.loadSrc.startsWith(globalThis.ImageKnife.getImageKnifeContext()
|
if (request.loadSrc.startsWith(globalThis.ImageKnife.getImageKnifeContext()
|
||||||
.filesDir) || request.loadSrc.startsWith(globalThis.ImageKnife.getImageKnifeContext().cacheDir)) {
|
.filesDir) || request.loadSrc.startsWith(globalThis.ImageKnife.getImageKnifeContext().cacheDir)) {
|
||||||
// 本地沙盒
|
// 本地沙盒
|
||||||
this.localFileClient.loadLocalFileData(request,onCompleteFunction,onErrorFunction)
|
this.localFileClient.loadData(request, onCompleteFunction, onErrorFunction)
|
||||||
}else{
|
} else {
|
||||||
// 网络下载
|
// 网络下载
|
||||||
this.networkClient.downloadNetworkData(request,onCompleteFunction,onErrorFunction)
|
this.networkClient.loadData(request, onCompleteFunction, onErrorFunction)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,9 +13,9 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {RequestOption} from "../RequestOption"
|
import { RequestOption } from '../RequestOption'
|
||||||
|
|
||||||
// 资源加载接口
|
// 资源加载接口
|
||||||
export interface IDataFetch {
|
export interface IDataFetch {
|
||||||
loadData(request: RequestOption, onCompleteFunction, onErrorFunction);
|
loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void);
|
||||||
}
|
}
|
|
@ -13,20 +13,20 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { IDataFetch } from "../networkmanage/IDataFetch"
|
import { IDataFetch } from '../networkmanage/IDataFetch'
|
||||||
import { RequestOption } from "../RequestOption"
|
import { RequestOption } from '../RequestOption'
|
||||||
import { Md5 } from "../../cache/Md5"
|
import { Md5 } from '../../cache/Md5'
|
||||||
import { FileUtils } from "../../cache/FileUtils"
|
import { FileUtils } from '../../cache/FileUtils'
|
||||||
import loadRequest from '@ohos.request';
|
import loadRequest from '@ohos.request';
|
||||||
|
|
||||||
export class LoadLocalFileClient {
|
export class LoadLocalFileClient {
|
||||||
loadLocalFileData(request: RequestOption, onCompleteFunction, onErrorFunction) {
|
loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) {
|
||||||
if(typeof request.loadSrc == 'string') {
|
if (typeof request.loadSrc == 'string') {
|
||||||
let fileBuffer = FileUtils.getInstance().readFilePic(request.loadSrc)
|
let fileBuffer = FileUtils.getInstance().readFilePic(request.loadSrc)
|
||||||
if(fileBuffer == null || fileBuffer.byteLength <=0){
|
if (fileBuffer == null || fileBuffer.byteLength <= 0) {
|
||||||
onErrorFunction('LoadLocalFileClient loadLocalFileData The File Does Not Exist!Check The File!')
|
onError('LoadLocalFileClient loadLocalFileData The File Does Not Exist!Check The File!')
|
||||||
}else{
|
} else {
|
||||||
onCompleteFunction(fileBuffer);
|
onComplete(fileBuffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,15 +13,15 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { IDataFetch } from "../networkmanage/IDataFetch"
|
import { IDataFetch } from '../networkmanage/IDataFetch'
|
||||||
import { RequestOption } from "../RequestOption"
|
import { RequestOption } from '../RequestOption'
|
||||||
import { Md5 } from "../../cache/Md5"
|
import { Md5 } from '../../cache/Md5'
|
||||||
import { FileUtils } from "../../cache/FileUtils"
|
import { FileUtils } from '../../cache/FileUtils'
|
||||||
import loadRequest from '@ohos.request';
|
import loadRequest from '@ohos.request';
|
||||||
|
|
||||||
// 数据加载器
|
// 数据加载器
|
||||||
export class NetworkDownloadClient {
|
export class NetworkDownloadClient {
|
||||||
downloadNetworkData(request: RequestOption, onCompleteFunction, onErrorFunction) {
|
loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) {
|
||||||
let filename = Md5.hashStr(request.generateDataKey);
|
let filename = Md5.hashStr(request.generateDataKey);
|
||||||
let downloadFolder = request.getFilesPath() + "/" + request.networkCacheFolder;
|
let downloadFolder = request.getFilesPath() + "/" + request.networkCacheFolder;
|
||||||
let allpath = request.getFilesPath() + "/" + request.networkCacheFolder + "/" + filename + ".img";
|
let allpath = request.getFilesPath() + "/" + request.networkCacheFolder + "/" + filename + ".img";
|
||||||
|
@ -42,7 +42,7 @@ export class NetworkDownloadClient {
|
||||||
loadTask = downloadTask;
|
loadTask = downloadTask;
|
||||||
|
|
||||||
loadTask.on('progress', (receivedSize, totalSize) => {
|
loadTask.on('progress', (receivedSize, totalSize) => {
|
||||||
if(totalSize > 0) {
|
if (totalSize > 0) {
|
||||||
// 并不是所有服务器都会返回totalSize 当没有文件大小的时候,下载进度没有百分比回调,只能知道目前下载了多少数据量
|
// 并不是所有服务器都会返回totalSize 当没有文件大小的时候,下载进度没有百分比回调,只能知道目前下载了多少数据量
|
||||||
let percent = Math.round(((receivedSize * 1.0) / (totalSize * 1.0)) * 100)
|
let percent = Math.round(((receivedSize * 1.0) / (totalSize * 1.0)) * 100)
|
||||||
if (request.progressFunc) {
|
if (request.progressFunc) {
|
||||||
|
@ -55,7 +55,7 @@ export class NetworkDownloadClient {
|
||||||
let downloadPath = allpath;
|
let downloadPath = allpath;
|
||||||
request.downloadFilePath = downloadPath;
|
request.downloadFilePath = downloadPath;
|
||||||
let arraybuffer = FileUtils.getInstance().readFilePic(downloadPath)
|
let arraybuffer = FileUtils.getInstance().readFilePic(downloadPath)
|
||||||
onCompleteFunction(arraybuffer);
|
onComplete(arraybuffer);
|
||||||
FileUtils.getInstance().deleteFile(downloadPath);
|
FileUtils.getInstance().deleteFile(downloadPath);
|
||||||
|
|
||||||
loadTask.off('complete', () => {
|
loadTask.off('complete', () => {
|
||||||
|
@ -83,7 +83,7 @@ export class NetworkDownloadClient {
|
||||||
})
|
})
|
||||||
|
|
||||||
loadTask.on('fail', (err) => {
|
loadTask.on('fail', (err) => {
|
||||||
onErrorFunction('NetworkDownloadClient Download task fail err =' + err)
|
onError('NetworkDownloadClient Download task fail err =' + err)
|
||||||
if (loadTask) {
|
if (loadTask) {
|
||||||
loadTask.remove().then(result => {
|
loadTask.remove().then(result => {
|
||||||
loadTask.off('complete', () => {
|
loadTask.off('complete', () => {
|
||||||
|
@ -109,10 +109,11 @@ export class NetworkDownloadClient {
|
||||||
})
|
})
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
onErrorFunction('NetworkDownloadClient downloadTask dismiss!')
|
onError('NetworkDownloadClient downloadTask dismiss!')
|
||||||
}
|
}
|
||||||
}).catch((err) => {
|
})
|
||||||
onErrorFunction(err)
|
.catch((err) => {
|
||||||
|
onError("下载子系统download错误捕获,error="+err.message);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -190,7 +190,10 @@ export class RequestManager {
|
||||||
let success = (arraybuffer) => {
|
let success = (arraybuffer) => {
|
||||||
this.downloadSuccess(arraybuffer, onComplete, onError)
|
this.downloadSuccess(arraybuffer, onComplete, onError)
|
||||||
}
|
}
|
||||||
this.mIDataFetch.loadData(request, success, onError);
|
let error = (errorMsg:string) =>{
|
||||||
|
onError(errorMsg)
|
||||||
|
}
|
||||||
|
this.mIDataFetch.loadData(request, success, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载本地资源
|
// 加载本地资源
|
||||||
|
|
Loading…
Reference in New Issue