1.reformat code with files contains(CacheRuleChangedPage.ets CustomEngineKeyImpl.ets dataShareUriLoadPage.ets DownloadClient.ets EngineKeyInterface.ets EntryAbility.ts LoadDataShareFileClient.ets LoadLocalFileClient.ets NetworkDownloadClient.ets)

Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
zhoulisheng1 2023-03-21 11:30:08 +08:00
parent be3c4eb03a
commit 49a9bfff2d
9 changed files with 68 additions and 49 deletions

View File

@ -1,48 +1,60 @@
import { EngineKeyInterface,EngineKeyFactories, RequestOption } from '@ohos/imageknife'
/*
* Copyright (C) 2023 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 { EngineKeyFactories, EngineKeyInterface, RequestOption } from '@ohos/imageknife'
export class CustomEngineKeyImpl implements EngineKeyInterface {
redefineUrl:(loadSrc:string)=>string;
addOtherInfo:string = "Version=1.0.0;"
redefineUrl: (loadSrc: string) => string;
addOtherInfo: string = "Version=1.0.0;"
constructor() {
this.redefineUrl = this.urlNeedClearToken.bind(this);
}
// request只读
generateMemoryCacheKey(loadSrc:string,size:string,transformed:string,dontAnimate:boolean):string {
return EngineKeyFactories.createMemoryCacheKey(loadSrc,size,transformed,dontAnimate,this.redefineUrl,this.addOtherInfo);
generateMemoryCacheKey(loadSrc: string, size: string, transformed: string, dontAnimate: boolean): string {
return EngineKeyFactories.createMemoryCacheKey(loadSrc, size, transformed, dontAnimate, this.redefineUrl, this.addOtherInfo);
}
generateTransformedDiskCacheKey(loadSrc:string,size:string,transformed:string,dontAnimate:boolean):string {
return EngineKeyFactories.createTransformedDiskCacheKey(loadSrc,size,transformed,dontAnimate,this.redefineUrl,this.addOtherInfo);
generateTransformedDiskCacheKey(loadSrc: string, size: string, transformed: string, dontAnimate: boolean): string {
return EngineKeyFactories.createTransformedDiskCacheKey(loadSrc, size, transformed, dontAnimate, this.redefineUrl, this.addOtherInfo);
}
generateOriginalDiskCacheKey(loadSrc:string):string {
return EngineKeyFactories.createOriginalDiskCacheKey(loadSrc,this.redefineUrl,this.addOtherInfo);
generateOriginalDiskCacheKey(loadSrc: string): string {
return EngineKeyFactories.createOriginalDiskCacheKey(loadSrc, this.redefineUrl, this.addOtherInfo);
}
// 需求场景: 请求图片可能 请求中存在token需要清除 可以把输入的url清除token后作为key的一部分这样token发生变化也能命中缓存。
urlNeedClearToken(url:string):string {
urlNeedClearToken(url: string): string {
if (this.isHttpRequest(url)) {
return this.clearToken(url)
} else {
return url;
return url;
}
}
isHttpRequest(loadSrc:string){
if(typeof loadSrc == "string" && loadSrc.toLowerCase().startsWith("http")){
return true;
}
isHttpRequest(loadSrc: string) {
if (typeof loadSrc == "string" && loadSrc.toLowerCase().startsWith("http")) {
return true;
}
return false;
}
// 清除url里面中携带的token
clearToken(url:string):string{
let retUrl = url.replace(this.findTokenParam(url),"")
clearToken(url: string): string {
let retUrl = url.replace(this.findTokenParam(url), "")
return retUrl;
}

View File

@ -1,3 +1,17 @@
/*
* Copyright (C) 2023 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 UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';

View File

@ -68,8 +68,8 @@ struct CacheRuleChangedPage {
Scroll() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Text('下图默认加载网络图片不带?token=').margin({top:20})
Button('点击加载网络图片?token=').margin({top:5}).onClick(() => {
Text('下图默认加载网络图片不带?token=').margin({ top: 20 })
Button('点击加载网络图片?token=').margin({ top: 5 }).onClick(() => {
this.imageKnifeOption4 = {
loadSrc: "https://hbimg.huabanimg.com/cc6af25f8d782d3cf3122bef4e61571378271145735e9-vEVggB?token=fsdafsfsafsafsdaf111111",
@ -83,9 +83,9 @@ struct CacheRuleChangedPage {
.width(300)
.height(300)
Text('下图默认加载网络图片不带&token=').margin({top:20})
Text('下图默认加载网络图片不带&token=').margin({ top: 20 })
Button('点击加载网络图片&token=').margin({top:5}).onClick(() => {
Button('点击加载网络图片&token=').margin({ top: 5 }).onClick(() => {
this.imageKnifeOption5 = {
loadSrc: "https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp?mama=sdafsfasdfsdfsdaf&token=fsdafsfsafsafsdaf111111&baba=sdfsafsafsd",
placeholderSrc: $r('app.media.icon_loading'),

View File

@ -13,15 +13,12 @@
* limitations under the License.
*/
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
import {
ImageKnifeComponent,
ImageKnifeOption,
} from '@ohos/imageknife'
import { ImageKnifeComponent, ImageKnifeOption, } from '@ohos/imageknife'
import ArkWorker from '@ohos.worker'
@Entry
@Component
struct DataShareUriLoadPage {
private globalGifWorker:any = undefined
@State imageKnifeOption1: ImageKnifeOption =
{
loadSrc: $r('app.media.icon'),
@ -29,8 +26,7 @@ struct DataShareUriLoadPage {
placeholderSrc: $r('app.media.icon_loading'),
errorholderSrc: $r('app.media.icon_failed')
};
private globalGifWorker: any = undefined
build() {
Scroll() {
@ -77,7 +73,7 @@ struct DataShareUriLoadPage {
console.log('get first object failed with error: ' + error);
return;
}
console.log("fileAsset id="+fileAsset.id + " fileAsset uri="+fileAsset.uri + " fileAsset displayName="+fileAsset.displayName)
console.log("fileAsset id=" + fileAsset.id + " fileAsset uri=" + fileAsset.uri + " fileAsset displayName=" + fileAsset.displayName)
// 加载图库第一张图片的uri
this.imageKnifeOption1 = {
@ -93,12 +89,9 @@ struct DataShareUriLoadPage {
ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }).width(300).height(300)
}.width('100%').backgroundColor(Color.Pink)
}
}
.width('100%')
.height('100%')
}
}

View File

@ -12,15 +12,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {RequestOption} from '../../imageknife/RequestOption'
import { RequestOption } from '../../imageknife/RequestOption'
export interface EngineKeyInterface {
// 生成内存缓存
generateMemoryCacheKey(loadSrc:string,size:string,transformed:string,dontAnimate:boolean) : string
generateMemoryCacheKey(loadSrc: string, size: string, transformed: string, dontAnimate: boolean): string
// 生成原图变换后的图片的磁盘缓存
generateTransformedDiskCacheKey(loadSrc:string,size:string,transformed:string,dontAnimate:boolean) : string
generateTransformedDiskCacheKey(loadSrc: string, size: string, transformed: string, dontAnimate: boolean): string
// 生成原图的磁盘缓存
generateOriginalDiskCacheKey(loadSrc:string) : string
generateOriginalDiskCacheKey(loadSrc: string): string
}

View File

@ -34,7 +34,7 @@ export class DownloadClient implements IDataFetch {
.filesDir) || request.loadSrc.startsWith(globalThis.ImageKnife.getImageKnifeContext().cacheDir)) {
// 本地沙盒
this.localFileClient.loadData(request, onCompleteFunction, onErrorFunction)
}else if(request.loadSrc.startsWith('datashare://')){
} else if (request.loadSrc.startsWith('datashare://')) {
this.dataShareFileClient.loadData(request, onCompleteFunction, onErrorFunction)
} else {
// 网络下载

View File

@ -18,19 +18,19 @@ import fs from '@ohos.file.fs';
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) {
if (typeof request.loadSrc == 'string') {
fs.open(request.loadSrc,fs.OpenMode.READ_ONLY).then((file)=>{
fs.open(request.loadSrc, fs.OpenMode.READ_ONLY).then((file) => {
let stat = fs.statSync(file.fd);
let buf = new ArrayBuffer(stat.size);
fs.read(file.fd, buf).then((readLen)=>{
fs.read(file.fd, buf).then((readLen) => {
onComplete(buf);
fs.close(file.fd);
}).catch(err=>{
onError('LoadDataShareFileClient fs.read err happend uri='+request.loadSrc+" err.msg="+err.message+" err.code="+err.code)
}).catch(err => {
onError('LoadDataShareFileClient fs.read err happend uri=' + request.loadSrc + " err.msg=" + err.message + " err.code=" + err.code)
})
}).catch(err=>{
onError('LoadDataShareFileClient fs.open err happend uri='+request.loadSrc+" err.msg="+err.message+" err.code="+err.code)
}).catch(err => {
onError('LoadDataShareFileClient fs.open err happend uri=' + request.loadSrc + " err.msg=" + err.message + " err.code=" + err.code)
})
}
}

View File

@ -20,7 +20,7 @@ import { FileUtils } from '../../cache/FileUtils'
import loadRequest from '@ohos.request';
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') {
let fileBuffer = FileUtils.getInstance().readFilePic(request.loadSrc)
if (fileBuffer == null || fileBuffer.byteLength <= 0) {

View File

@ -114,7 +114,7 @@ export class NetworkDownloadClient {
}
})
.catch((err) => {
onError("下载子系统download错误捕获,error="+err.message);
})
onError("下载子系统download错误捕获,error=" + err.message);
})
}
}