diff --git a/README.md b/README.md index 23624dd..82f9bfa 100644 --- a/README.md +++ b/README.md @@ -320,6 +320,7 @@ Clear the component content in the **aboutToRecycle** lifecycle and trigger imag | onComplete | (event:EventImage \| undefined)=>void | Callback for image loading completion. Optional. | | onLoadListener | onLoadStart:()=>void,onLoadSuccess:(data:string\|Pixelmap)=>void | Callback for image loading events. Optional. | | downsampleOf | DownsampleStrategy | 降采样(可选) | +| caPath | String | Set the path of a custom certificate. Optional. | ### 降采样类型 | 类型 | 相关描述 | |---------------------|-------------------| diff --git a/README_zh.md b/README_zh.md index 97522da..2ea610e 100644 --- a/README_zh.md +++ b/README_zh.md @@ -297,17 +297,18 @@ ImageKnifeAnimatorComponent({ | errorholderObjectFit | ImageFit | 错误图填充效果(可选) | | writeCacheStrategy | CacheStrategyType | 写入缓存策略(可选) | | onlyRetrieveFromCache | boolean | 是否跳过网络和本地请求(可选) | -| customGetImage | (context: Context, src: string | 自定义下载图片(可选) | | Resource | 错误占位图数据源 | +| customGetImage | (context: Context, src: string | 自定义下载图片(可选) | | border | BorderOptions | 边框圆角(可选) | -| priority | taskpool.Priority | 加载优先级(可选) | +| priority | taskpool.Priority | 加载优先级(可选) | | context | common.UIAbilityContext | 上下文(可选) | | progressListener | (progress: number)=>void | 进度(可选) | -| signature | String | 自定义缓存关键字(可选) | +| signature | String | 自定义缓存关键字(可选) | | headerOption | Array | 设置请求头(可选) | | transformation | PixelMapTransformation | 图片变换(可选) | -| drawingColorFilter | ColorFilter | drawing.ColorFilter | 图片变换(可选) | -| onComplete | (event:EventImage | undefined) => voi | 颜色滤镜效果(可选) | -| onLoadListener | onLoadStart: () => void、onLoadSuccess: (data: string | PixelMap | undefined) => void、onLoadFailed: (err: string) => void| 监听图片加载成功与失败 | +| drawingColorFilter | ColorFilter | drawing.ColorFilter | +| onComplete | (event:EventImage | undefined) => voi | +| onLoadListener | onLoadStart: () => void、onLoadSuccess: (data: string | PixelMap | +| caPath | String | 设置自定义证书路径(可选) | ### ImageKnife接口 diff --git a/entry/src/main/ets/pages/SingleImage.ets b/entry/src/main/ets/pages/SingleImage.ets index 3671c6c..3ad5802 100644 --- a/entry/src/main/ets/pages/SingleImage.ets +++ b/entry/src/main/ets/pages/SingleImage.ets @@ -76,7 +76,9 @@ struct SingleImage { placeholderSrc: $r("app.media.loading"), errorholderSrc: $r("app.media.failed"), objectFit: ImageFit.Contain, - progressListener:(progress:number)=>{console.info("ImageKnife:: call back progress = " + progress)} + progressListener:(progress:number)=>{console.info("ImageKnife:: call back progress = " + progress)}, + // 通过https协议进行连接时,默认使用系统预设CA证书。若希望使用自定义证书进行https连接,则需要将自定义证书上传至应用沙箱目录中,并在caPath中指定该证书所在的应用沙箱路径 + // caPath: "/data/storage/el1/bundle/ca.pem", }) }).width(100).height(100) Text($r('app.string.Custom_network_download')) diff --git a/library/src/main/ets/ImageKnifeDispatcher.ets b/library/src/main/ets/ImageKnifeDispatcher.ets index e2e1466..a3e334c 100644 --- a/library/src/main/ets/ImageKnifeDispatcher.ets +++ b/library/src/main/ets/ImageKnifeDispatcher.ets @@ -188,6 +188,7 @@ export class ImageKnifeDispatcher { targetWidth: currentRequest.componentWidth, targetHeight: currentRequest.componentHeight, downsampType: currentRequest.imageKnifeOption.downsampleOf==undefined?DownsampleStrategy.NONE:currentRequest.imageKnifeOption.downsampleOf, + caPath: currentRequest.imageKnifeOption.caPath, } if(request.customGetImage == undefined) { diff --git a/library/src/main/ets/ImageKnifeLoader.ets b/library/src/main/ets/ImageKnifeLoader.ets index b00e5fb..fbe0fed 100644 --- a/library/src/main/ets/ImageKnifeLoader.ets +++ b/library/src/main/ets/ImageKnifeLoader.ets @@ -289,26 +289,50 @@ export class ImageKnifeLoader { } }) } - let promise = httpRequest.requestInStream(request.src, { - header: headerObj, - method: http.RequestMethod.GET, - expectDataType: http.HttpDataType.ARRAY_BUFFER, - connectTimeout: 60000, - readTimeout: 0, - // usingProtocol:http.HttpProtocol.HTTP1_1 - // header: new Header('application/json') - }); - await promise.then((data: number) => { - if (data == 200 || data == 206 || data == 204) { - resBuf = combineArrayBuffers(arrayBuffers) - } else { - throw new Error("HttpDownloadClient has error, http code =" + JSON.stringify(data)) - } - }).catch((err: Error) => { - throw new Error("HttpDownloadClient download ERROR : err = " + JSON.stringify(err)) - }); - LogUtil.log("HttpDownloadClient.end:" + request.src) + if(request.caPath === undefined) { //采用默认证书 + let promise = httpRequest.requestInStream(request.src, { + header: headerObj, + method: http.RequestMethod.GET, + expectDataType: http.HttpDataType.ARRAY_BUFFER, + connectTimeout: 60000, + readTimeout: 0, + // usingProtocol:http.HttpProtocol.HTTP1_1 + // header: new Header('application/json') + }); + await promise.then((data: number) => { + if (data == 200 || data == 206 || data == 204) { + resBuf = combineArrayBuffers(arrayBuffers) + } else { + throw new Error("HttpDownloadClient has error, http code =" + JSON.stringify(data)) + } + }).catch((err: Error) => { + throw new Error("HttpDownloadClient download ERROR : err = " + JSON.stringify(err)) + }); + LogUtil.log("HttpDownloadClient.end:" + request.src) + } else { //采用自定义证书 + let promise = httpRequest.requestInStream(request.src, { + header: headerObj, + method: http.RequestMethod.GET, + expectDataType: http.HttpDataType.ARRAY_BUFFER, + connectTimeout: 60000, + readTimeout: 0, + // usingProtocol:http.HttpProtocol.HTTP1_1 + // header: new Header('application/json') + caPath: request.caPath, + }); + await promise.then((data: number) => { + if (data == 200 || data == 206 || data == 204) { + resBuf = combineArrayBuffers(arrayBuffers) + } else { + throw new Error("HttpDownloadClient has error, http code =" + JSON.stringify(data)) + } + }).catch((err: Error) => { + throw new Error("HttpDownloadClient download ERROR : err = " + JSON.stringify(err)) + }); + LogUtil.log("HttpDownloadClient.end:" + request.src) + } + // 保存文件缓存 if (resBuf !== undefined && request.writeCacheStrategy !== CacheStrategy.Memory) { LogUtil.log("ImageKnife_DataTime_requestJob_saveFileCacheOnlyFile.start:"+request.src) diff --git a/library/src/main/ets/model/ImageKnifeData.ets b/library/src/main/ets/model/ImageKnifeData.ets index 8fb3b61..b050b6f 100644 --- a/library/src/main/ets/model/ImageKnifeData.ets +++ b/library/src/main/ets/model/ImageKnifeData.ets @@ -108,5 +108,6 @@ export interface RequestJobRequest { targetWidth: number targetHeight: number downsampType: DownsampleStrategy + caPath?: string, } diff --git a/library/src/main/ets/model/ImageKnifeOption.ets b/library/src/main/ets/model/ImageKnifeOption.ets index 4e42b00..9f2d148 100644 --- a/library/src/main/ets/model/ImageKnifeOption.ets +++ b/library/src/main/ets/model/ImageKnifeOption.ets @@ -92,6 +92,8 @@ interface ImageOption { onComplete?:(event:EventImage | undefined) => void drawingColorFilter?: ColorFilter | drawing.ColorFilter downsampleOf?: DownsampleStrategy + // 自定义证书路径 + caPath?: string } @ObservedV2 export class ImageKnifeOption { @@ -125,6 +127,8 @@ export class ImageKnifeOption { drawingColorFilter?: ColorFilter | drawing.ColorFilter // 下采样 @Trace downsampleOf: DownsampleStrategy = DownsampleStrategy.NONE + // 自定义证书路径 + caPath?: string constructor(option?:ImageOption) { this.loadSrc = option?.loadSrc == undefined ? "" : option?.loadSrc this.placeholderSrc = option?.placeholderSrc @@ -146,6 +150,7 @@ export class ImageKnifeOption { this.onComplete = option?.onComplete this.drawingColorFilter = option?.drawingColorFilter this.downsampleOf = option?.downsampleOf==undefined?DownsampleStrategy.NONE:option?.downsampleOf + this.caPath = option?.caPath } }