1.ArkTs整改9 整改imageknife->networkmanage

Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
zhoulisheng1 2023-09-19 11:34:20 +08:00
parent af84eb8ab4
commit a0b6e19f8a
5 changed files with 43 additions and 61 deletions

View File

@ -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<string,Object>).filesDir as string;
let cacheDir:string = (ImageKnifeGlobal.getInstance().getHapContext() as Record<string,Object>).cacheDir as string

View File

@ -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;
}

View File

@ -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)
})
}

View File

@ -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)
})

View File

@ -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);
})
}