新增https自定义证书功能

Signed-off-by: jinzhao <jinzhao@kaihong.com>
This commit is contained in:
jinzhao 2024-10-23 15:26:25 +08:00
parent a7fa5de2de
commit 56c63b5637
7 changed files with 61 additions and 26 deletions

View File

@ -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. |
### 降采样类型
| 类型 | 相关描述 |
|---------------------|-------------------|

View File

@ -297,7 +297,7 @@ ImageKnifeAnimatorComponent({
| errorholderObjectFit | ImageFit | 错误图填充效果(可选) |
| writeCacheStrategy | CacheStrategyType | 写入缓存策略(可选) |
| onlyRetrieveFromCache | boolean | 是否跳过网络和本地请求(可选) |
| customGetImage | (context: Context, src: string | 自定义下载图片(可选) | | Resource | 错误占位图数据源 |
| customGetImage | (context: Context, src: string | 自定义下载图片(可选) |
| border | BorderOptions | 边框圆角(可选) |
| priority | taskpool.Priority | 加载优先级(可选) |
| context | common.UIAbilityContext | 上下文(可选) |
@ -305,9 +305,10 @@ ImageKnifeAnimatorComponent({
| signature | String | 自定义缓存关键字(可选) |
| headerOption | Array<HeaderOptions> | 设置请求头(可选) |
| 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接口

View File

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

View File

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

View File

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

View File

@ -108,5 +108,6 @@ export interface RequestJobRequest {
targetWidth: number
targetHeight: number
downsampType: DownsampleStrategy
caPath?: string,
}

View File

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