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

View File

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

View File

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

View File

@ -12,15 +12,15 @@
* 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 {RequestOption} from '../../imageknife/RequestOption' import { RequestOption } from '../../imageknife/RequestOption'
export interface EngineKeyInterface { 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)) { .filesDir) || request.loadSrc.startsWith(globalThis.ImageKnife.getImageKnifeContext().cacheDir)) {
// 本地沙盒 // 本地沙盒
this.localFileClient.loadData(request, onCompleteFunction, onErrorFunction) this.localFileClient.loadData(request, onCompleteFunction, onErrorFunction)
}else if(request.loadSrc.startsWith('datashare://')){ } else if (request.loadSrc.startsWith('datashare://')) {
this.dataShareFileClient.loadData(request, onCompleteFunction, onErrorFunction) this.dataShareFileClient.loadData(request, onCompleteFunction, onErrorFunction)
} else { } else {
// 网络下载 // 网络下载

View File

@ -18,19 +18,19 @@ import fs from '@ohos.file.fs';
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) {
if (typeof request.loadSrc == 'string') { 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 stat = fs.statSync(file.fd);
let buf = new ArrayBuffer(stat.size); let buf = new ArrayBuffer(stat.size);
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 => {
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 => {
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)
}) })
} }
} }

View File

@ -20,7 +20,7 @@ import { FileUtils } from '../../cache/FileUtils'
import loadRequest from '@ohos.request'; import loadRequest from '@ohos.request';
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') {
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) {

View File

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