forked from floraachy/ImageKnife
1.新增加载本地文件能力
Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
parent
884b06bb32
commit
cd390dcd6b
|
@ -17,106 +17,26 @@ 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';
|
||||
|
||||
// 数据加载器
|
||||
export class DownloadClient implements IDataFetch {
|
||||
private networkClient = new NetworkDownloadClient();
|
||||
private localFileClient = new LoadLocalFileClient();
|
||||
|
||||
|
||||
loadData(request: RequestOption, onCompleteFunction, onErrorFunction) {
|
||||
|
||||
let filename = Md5.hashStr(request.generateDataKey);
|
||||
let downloadFolder = request.getFilesPath() + "/" + request.networkCacheFolder;
|
||||
let allpath = request.getFilesPath() + "/" + request.networkCacheFolder + "/" + filename + ".img";
|
||||
|
||||
if (!FileUtils.getInstance().existFolder(downloadFolder)) {
|
||||
FileUtils.getInstance().createFolder(downloadFolder)
|
||||
}
|
||||
|
||||
if (FileUtils.getInstance().exist(allpath)) {
|
||||
FileUtils.getInstance().deleteFile(allpath)
|
||||
}
|
||||
|
||||
let desc = filename + ".img";
|
||||
var downloadConfig = {
|
||||
url: (request.loadSrc as string),
|
||||
filePath: allpath,
|
||||
};
|
||||
|
||||
let loadTask = null;
|
||||
loadRequest.download(globalThis.ImageKnife.getImageKnifeContext(), downloadConfig).then(downloadTask => {
|
||||
if (downloadTask) {
|
||||
loadTask = 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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
loadTask.on('complete', () => {
|
||||
let downloadPath = allpath;
|
||||
request.downloadFilePath = downloadPath;
|
||||
let arraybuffer = FileUtils.getInstance().readFilePic(downloadPath)
|
||||
onCompleteFunction(arraybuffer);
|
||||
FileUtils.getInstance().deleteFile(downloadPath);
|
||||
|
||||
loadTask.off('complete', () => {
|
||||
|
||||
})
|
||||
|
||||
loadTask.off('pause', () => {
|
||||
})
|
||||
|
||||
loadTask.off('remove', () => {
|
||||
})
|
||||
|
||||
loadTask.off('progress', () => {
|
||||
})
|
||||
|
||||
loadTask.off('fail', () => {
|
||||
})
|
||||
loadTask = null;
|
||||
})
|
||||
|
||||
loadTask.on('pause', () => {
|
||||
})
|
||||
|
||||
loadTask.on('remove', () => {
|
||||
})
|
||||
|
||||
loadTask.on('fail', (err) => {
|
||||
onErrorFunction('DownloadClient 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 => {
|
||||
loadTask = null;
|
||||
console.log('DownloadClient Download task fail err =' + err);
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
} else {
|
||||
onErrorFunction('DownloadClient downloadTask dismiss!')
|
||||
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)
|
||||
}
|
||||
}
|
||||
}).catch((err) => {
|
||||
onErrorFunction(err)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
import {RequestOption} from "../RequestOption"
|
||||
|
||||
// 网络接口
|
||||
// 资源加载接口
|
||||
export interface IDataFetch {
|
||||
loadData(request: RequestOption, onCompleteFunction, onErrorFunction);
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
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) {
|
||||
let filename = Md5.hashStr(request.generateDataKey);
|
||||
let downloadFolder = request.getFilesPath() + "/" + request.networkCacheFolder;
|
||||
let allpath = request.getFilesPath() + "/" + request.networkCacheFolder + "/" + filename + ".img";
|
||||
if (!FileUtils.getInstance().existFolder(downloadFolder)) {
|
||||
FileUtils.getInstance().createFolder(downloadFolder)
|
||||
}
|
||||
if (FileUtils.getInstance().exist(allpath)) {
|
||||
FileUtils.getInstance().deleteFile(allpath)
|
||||
}
|
||||
let desc = filename + ".img";
|
||||
var downloadConfig = {
|
||||
url: (request.loadSrc as string),
|
||||
filePath: allpath,
|
||||
};
|
||||
let loadTask = null;
|
||||
loadRequest.download(globalThis.ImageKnife.getImageKnifeContext(), downloadConfig).then(downloadTask => {
|
||||
if (downloadTask) {
|
||||
loadTask = 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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
loadTask.on('complete', () => {
|
||||
let downloadPath = allpath;
|
||||
request.downloadFilePath = downloadPath;
|
||||
let arraybuffer = FileUtils.getInstance().readFilePic(downloadPath)
|
||||
onCompleteFunction(arraybuffer);
|
||||
FileUtils.getInstance().deleteFile(downloadPath);
|
||||
|
||||
loadTask.off('complete', () => {
|
||||
|
||||
})
|
||||
|
||||
loadTask.off('pause', () => {
|
||||
})
|
||||
|
||||
loadTask.off('remove', () => {
|
||||
})
|
||||
|
||||
loadTask.off('progress', () => {
|
||||
})
|
||||
|
||||
loadTask.off('fail', () => {
|
||||
})
|
||||
loadTask = null;
|
||||
})
|
||||
|
||||
loadTask.on('pause', () => {
|
||||
})
|
||||
|
||||
loadTask.on('remove', () => {
|
||||
})
|
||||
|
||||
loadTask.on('fail', (err) => {
|
||||
onErrorFunction('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 => {
|
||||
loadTask = null;
|
||||
console.log('NetworkDownloadClient Download task fail err =' + err);
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
} else {
|
||||
onErrorFunction('NetworkDownloadClient downloadTask dismiss!')
|
||||
}
|
||||
}).catch((err) => {
|
||||
onErrorFunction(err)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue