1.ArkTs整改9 整改imageknife->networkmanage
Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
parent
af84eb8ab4
commit
a0b6e19f8a
|
@ -13,7 +13,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type { IDataFetch } from '../networkmanage/IDataFetch'
|
import { IDataFetch } from '../networkmanage/IDataFetch'
|
||||||
import { RequestOption } from '../RequestOption'
|
import { RequestOption } from '../RequestOption'
|
||||||
import { NetworkDownloadClient } from './NetworkDownloadClient'
|
import { NetworkDownloadClient } from './NetworkDownloadClient'
|
||||||
import { LoadLocalFileClient } from './LoadLocalFileClient'
|
import { LoadLocalFileClient } from './LoadLocalFileClient'
|
||||||
|
@ -27,7 +27,7 @@ export class DownloadClient implements IDataFetch {
|
||||||
private localFileClient = new LoadLocalFileClient();
|
private localFileClient = new LoadLocalFileClient();
|
||||||
private dataShareFileClient = new LoadDataShareFileClient();
|
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') {
|
if (typeof request.loadSrc == 'string') {
|
||||||
let fileDir:string = (ImageKnifeGlobal.getInstance().getHapContext() as Record<string,Object>).filesDir as 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
|
let cacheDir:string = (ImageKnifeGlobal.getInstance().getHapContext() as Record<string,Object>).cacheDir as string
|
||||||
|
|
|
@ -17,5 +17,5 @@ import { RequestOption } from '../RequestOption'
|
||||||
|
|
||||||
// 资源加载接口
|
// 资源加载接口
|
||||||
export interface IDataFetch {
|
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;
|
||||||
}
|
}
|
|
@ -12,10 +12,10 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
import type { IDataFetch } from '../networkmanage/IDataFetch'
|
import { IDataFetch } from '../networkmanage/IDataFetch'
|
||||||
import { RequestOption } from '../RequestOption'
|
import { RequestOption } from '../RequestOption'
|
||||||
import fs from '@ohos.file.fs';
|
import fs from '@ohos.file.fs';
|
||||||
|
import { BusinessError } from '@ohos.base'
|
||||||
|
|
||||||
export class LoadDataShareFileClient implements IDataFetch {
|
export class LoadDataShareFileClient implements IDataFetch {
|
||||||
loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) {
|
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) => {
|
fs.read(file.fd, buf).then((readLen) => {
|
||||||
onComplete(buf);
|
onComplete(buf);
|
||||||
fs.close(file.fd);
|
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)
|
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)
|
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)
|
onError('LoadDataShareFileClient fs.open err happend uri=' + request.loadSrc + " err.msg=" + err?.message + " err.code=" + err?.code)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,10 +13,10 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type { IDataFetch } from '../networkmanage/IDataFetch'
|
import { IDataFetch } from '../networkmanage/IDataFetch'
|
||||||
import { RequestOption } from '../RequestOption'
|
import { RequestOption } from '../RequestOption'
|
||||||
import { FileUtils } from '../../cache/FileUtils'
|
import { FileUtils } from '../../cache/FileUtils'
|
||||||
|
import { BusinessError } from '@ohos.base'
|
||||||
export class LoadLocalFileClient implements IDataFetch {
|
export class LoadLocalFileClient implements IDataFetch {
|
||||||
loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) {
|
loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) {
|
||||||
if (typeof request.loadSrc == 'string') {
|
if (typeof request.loadSrc == 'string') {
|
||||||
|
@ -26,7 +26,7 @@ export class LoadLocalFileClient implements IDataFetch {
|
||||||
} else {
|
} else {
|
||||||
onComplete(fileBuffer);
|
onComplete(fileBuffer);
|
||||||
}
|
}
|
||||||
}).catch(err=>{
|
}).catch((err:BusinessError)=>{
|
||||||
onError('LoadLocalFileClient loadLocalFileData Error Msg ='+err?.message)
|
onError('LoadLocalFileClient loadLocalFileData Error Msg ='+err?.message)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type { IDataFetch } from '../networkmanage/IDataFetch'
|
import { IDataFetch } from '../networkmanage/IDataFetch'
|
||||||
import { RequestOption } from '../RequestOption'
|
import { RequestOption } from '../RequestOption'
|
||||||
import { SparkMD5 } from '../../3rd_party/sparkmd5/spark-md5'
|
import { SparkMD5 } from '../../3rd_party/sparkmd5/spark-md5'
|
||||||
import { FileUtils } from '../../cache/FileUtils'
|
import { FileUtils } from '../../cache/FileUtils'
|
||||||
|
@ -21,6 +21,7 @@ import loadRequest from '@ohos.request';
|
||||||
import { LogUtil } from '../utils/LogUtil'
|
import { LogUtil } from '../utils/LogUtil'
|
||||||
import { ImageKnifeGlobal } from '../ImageKnifeGlobal'
|
import { ImageKnifeGlobal } from '../ImageKnifeGlobal'
|
||||||
import common from '@ohos.app.ability.common'
|
import common from '@ohos.app.ability.common'
|
||||||
|
import { BusinessError } from '@ohos.base'
|
||||||
// 数据加载器
|
// 数据加载器
|
||||||
export class NetworkDownloadClient implements IDataFetch {
|
export class NetworkDownloadClient implements IDataFetch {
|
||||||
loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) {
|
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)) {
|
if (FileUtils.getInstance().exist(allpath)) {
|
||||||
FileUtils.getInstance().deleteFile(allpath)
|
FileUtils.getInstance().deleteFile(allpath)
|
||||||
}
|
}
|
||||||
var downloadConfig = {
|
let downloadConfig:loadRequest.DownloadConfig = {
|
||||||
url: (request.loadSrc as string),
|
url: (request.loadSrc as string),
|
||||||
filePath: allpath,
|
filePath: allpath,
|
||||||
// 允许计费流量下载
|
// 允许计费流量下载
|
||||||
enableMetered: true,
|
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) {
|
if (downloadTask) {
|
||||||
loadTask = downloadTask;
|
let loadTask:loadRequest.DownloadTask | null = 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) {
|
||||||
request.progressFunc(percent);
|
request.progressFunc.asyncSuccess(percent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -63,69 +64,50 @@ export class NetworkDownloadClient implements IDataFetch {
|
||||||
onComplete(arraybuffer);
|
onComplete(arraybuffer);
|
||||||
FileUtils.getInstance().deleteFileAsync(downloadPath).then(()=>{
|
FileUtils.getInstance().deleteFileAsync(downloadPath).then(()=>{
|
||||||
LogUtil.log('文件名:'+downloadPath+" 文件删除成功!")
|
LogUtil.log('文件名:'+downloadPath+" 文件删除成功!")
|
||||||
}).catch(err=>{
|
}).catch((err:BusinessError)=>{
|
||||||
LogUtil.log('文件名:'+downloadPath+" 文件删除失败!")
|
LogUtil.log('文件名:'+downloadPath+" 文件删除失败!")
|
||||||
});
|
});
|
||||||
}).catch(err=>{
|
}).catch((err:BusinessError)=>{
|
||||||
onError('NetworkDownloadClient Read File Async Error Msg='+ err?.message)
|
onError('NetworkDownloadClient Read File Async Error Msg='+ (err as BusinessError)?.message)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if(loadTask != null) {
|
||||||
loadTask.off('complete', () => {
|
loadTask.off('complete', () => {})
|
||||||
|
loadTask.off('pause', () => {})
|
||||||
})
|
loadTask.off('remove', () => {})
|
||||||
|
loadTask.off('progress', () => {})
|
||||||
loadTask.off('pause', () => {
|
loadTask.off('fail', () => {})
|
||||||
})
|
loadTask = null;
|
||||||
|
}
|
||||||
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) => {
|
loadTask.on('fail', (err) => {
|
||||||
onError('NetworkDownloadClient Download task fail err =' + err)
|
onError('NetworkDownloadClient Download task fail err =' + err)
|
||||||
if (loadTask) {
|
if (loadTask!=null) {
|
||||||
loadTask.remove().then(result => {
|
loadTask.delete().then(result => {
|
||||||
loadTask.off('complete', () => {
|
if(loadTask != undefined) {
|
||||||
})
|
loadTask.off('complete', () => {})
|
||||||
|
loadTask.off('pause', () => {})
|
||||||
loadTask.off('pause', () => {
|
loadTask.off('remove', () => {})
|
||||||
})
|
loadTask.off('progress', () => {})
|
||||||
|
loadTask.off('fail', () => {})
|
||||||
loadTask.off('remove', () => {
|
loadTask = null
|
||||||
})
|
}
|
||||||
|
}).catch((err:BusinessError) => {
|
||||||
loadTask.off('progress', () => {
|
|
||||||
})
|
|
||||||
|
|
||||||
loadTask.off('fail', () => {
|
|
||||||
})
|
|
||||||
loadTask = null
|
|
||||||
}).catch(err => {
|
|
||||||
loadTask = null;
|
loadTask = null;
|
||||||
console.log('NetworkDownloadClient Download task fail err =' + err);
|
console.log('NetworkDownloadClient Download task fail err =' + err);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
onError('NetworkDownloadClient downloadTask dismiss!')
|
onError('NetworkDownloadClient downloadTask dismiss!')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err:BusinessError)=> {
|
||||||
onError("下载子系统download错误捕获,error=" + err.message);
|
onError("下载子系统download错误捕获,error=" + err.message);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue