1.补充Imageknife相关public接口
2.支持文件路径支持设置非context.cacheDir的路径 Signed-off-by: madixin <madixin@huawei.com>
This commit is contained in:
parent
2c54685897
commit
3fa2d61b6e
|
@ -20,7 +20,6 @@ import { FileCache } from './utils/FileCache';
|
||||||
import { ImageKnifeDispatcher } from './ImageKnifeDispatcher';
|
import { ImageKnifeDispatcher } from './ImageKnifeDispatcher';
|
||||||
import { IEngineKey } from './key/IEngineKey';
|
import { IEngineKey } from './key/IEngineKey';
|
||||||
import { HeaderOptions, ImageKnifeOption } from './ImageKnifeOption';
|
import { HeaderOptions, ImageKnifeOption } from './ImageKnifeOption';
|
||||||
import { DefaultEngineKey } from './key/DefaultEngineKey';
|
|
||||||
import { FileTypeUtil } from './utils/FileTypeUtil';
|
import { FileTypeUtil } from './utils/FileTypeUtil';
|
||||||
import { util } from '@kit.ArkTS';
|
import { util } from '@kit.ArkTS';
|
||||||
import { image } from '@kit.ImageKit';
|
import { image } from '@kit.ImageKit';
|
||||||
|
@ -37,8 +36,8 @@ export class ImageKnife {
|
||||||
private dispatcher: ImageKnifeDispatcher = new ImageKnifeDispatcher()
|
private dispatcher: ImageKnifeDispatcher = new ImageKnifeDispatcher()
|
||||||
// 配置全局是否在子线程加载图片请求
|
// 配置全局是否在子线程加载图片请求
|
||||||
private _isRequestInSubThread: boolean = true;
|
private _isRequestInSubThread: boolean = true;
|
||||||
|
//定义全局网络请求header map
|
||||||
headerMap: Map<string, Object> = new Map<string, Object>(); //定义全局map
|
headerMap: Map<string, Object> = new Map<string, Object>();
|
||||||
|
|
||||||
public static getInstance(): ImageKnife {
|
public static getInstance(): ImageKnife {
|
||||||
if (!ImageKnife.instance) {
|
if (!ImageKnife.instance) {
|
||||||
|
@ -58,12 +57,12 @@ export class ImageKnife {
|
||||||
return this._isRequestInSubThread;
|
return this._isRequestInSubThread;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化文件缓存
|
* 初始化文件缓存个数,大小,以及路径
|
||||||
* @param context 上下文
|
* @param context 上下文
|
||||||
* @param size 缓存数量
|
* @param size 缓存数量
|
||||||
* @param memory 内存大小
|
* @param memory 内存大小
|
||||||
|
* @param path 文件目录
|
||||||
*/
|
*/
|
||||||
async initFileCache(context: Context, size: number = 256, memory: number = 256 * 1024 * 1024,path?: string) {
|
async initFileCache(context: Context, size: number = 256, memory: number = 256 * 1024 * 1024,path?: string) {
|
||||||
this.fileCache = new FileCache(context, size, memory)
|
this.fileCache = new FileCache(context, size, memory)
|
||||||
|
@ -75,7 +74,7 @@ export class ImageKnife {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断文件缓存是否初始化
|
* 判断文件缓存是否已完成初始化
|
||||||
* @returns 是否初始化
|
* @returns 是否初始化
|
||||||
*/
|
*/
|
||||||
public isFileCacheInit(): boolean {
|
public isFileCacheInit(): boolean {
|
||||||
|
@ -83,16 +82,16 @@ export class ImageKnife {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全局添加单个请求头
|
* 全局添加单个请求头header
|
||||||
* @param key 请求头属性
|
* @param key 请求头属性
|
||||||
* @param value 请求头值
|
* @param value 请求头值
|
||||||
*/
|
*/
|
||||||
addHeader(key: string, value: Object) {
|
addHeader(key: string, value: Object) {
|
||||||
this.headerMap.set(key, value);
|
this.headerMap.set(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全局设置请求头调用方法
|
* 全局设置请求头header
|
||||||
* @param options 请求头数组
|
* @param options 请求头数组
|
||||||
*/
|
*/
|
||||||
serHeaderOptions(options: Array<HeaderOptions>) {
|
serHeaderOptions(options: Array<HeaderOptions>) {
|
||||||
|
@ -102,11 +101,11 @@ export class ImageKnife {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除单个请求头属性
|
* 删除单个请求头header
|
||||||
* @param key 请求头属性
|
* @param key 请求头属性
|
||||||
*/
|
*/
|
||||||
deleteHeader(key: string) {
|
deleteHeader(key: string) {
|
||||||
this.headerMap.delete(key);
|
this.headerMap.delete(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -114,7 +113,7 @@ export class ImageKnife {
|
||||||
* @param newMemoryCache 自定义内存缓存
|
* @param newMemoryCache 自定义内存缓存
|
||||||
*/
|
*/
|
||||||
initMemoryCache(newMemoryCache: IMemoryCache): void {
|
initMemoryCache(newMemoryCache: IMemoryCache): void {
|
||||||
this.memoryCache
|
this.memoryCache = newMemoryCache
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -123,9 +122,11 @@ export class ImageKnife {
|
||||||
removeAllMemoryCache(): void {
|
removeAllMemoryCache(): void {
|
||||||
this.memoryCache.removeAll()
|
this.memoryCache.removeAll()
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
|
/**
|
||||||
* 清除指定内存缓存
|
* 清除指定内存缓存
|
||||||
* */
|
* @param url 待清除的url路径或ImageKnifeOption
|
||||||
|
*/
|
||||||
removeMemoryCache(url: string | ImageKnifeOption) {
|
removeMemoryCache(url: string | ImageKnifeOption) {
|
||||||
let imageKnifeOption = new ImageKnifeOption();
|
let imageKnifeOption = new ImageKnifeOption();
|
||||||
if (typeof url == 'string') {
|
if (typeof url == 'string') {
|
||||||
|
@ -137,33 +138,9 @@ export class ImageKnife {
|
||||||
this.memoryCache.remove(key);
|
this.memoryCache.remove(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
loadFromMemoryCache(key: string): ImageKnifeData | undefined {
|
|
||||||
if (key !== "") {
|
|
||||||
return this.memoryCache.get(key)
|
|
||||||
}
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
saveMemoryCache(key: string, data: ImageKnifeData): void {
|
|
||||||
if (key !== "") {
|
|
||||||
this.memoryCache.put(key, data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
loadFromFileCache(key: string): ArrayBuffer | undefined {
|
|
||||||
return this.fileCache?.get(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
saveFileCache(key: string, data: ArrayBuffer): void {
|
|
||||||
this.fileCache?.put(key, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
getFileCache(): FileCache {
|
|
||||||
return this.fileCache as FileCache
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 预加载到文件缓存
|
* 预加载图片到文件缓存
|
||||||
* @param loadSrc 图片地址url
|
* @param loadSrc 图片地址url
|
||||||
* @returns 返回文件缓存路径
|
* @returns 返回文件缓存路径
|
||||||
*/
|
*/
|
||||||
|
@ -233,7 +210,7 @@ export class ImageKnife {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* 预加载缓存(用于外部已获取pixelmap,需要加入imageknife缓存的场景)
|
||||||
* @param url 图片地址url
|
* @param url 图片地址url
|
||||||
* @param pixelMap 图片
|
* @param pixelMap 图片
|
||||||
* @param cacheType 缓存策略
|
* @param cacheType 缓存策略
|
||||||
|
@ -258,6 +235,74 @@ export class ImageKnife {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除所有文件缓存
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
async removeAllFileCache(): Promise<void> {
|
||||||
|
if (this.fileCache !== undefined) {
|
||||||
|
await this.fileCache.removeAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* 清除指定文件缓存
|
||||||
|
* */
|
||||||
|
removeFileCache(url: string | ImageKnifeOption) {
|
||||||
|
let imageKnifeOption: ImageKnifeOption;
|
||||||
|
if (url instanceof ImageKnifeOption) {
|
||||||
|
imageKnifeOption = url;
|
||||||
|
} else {
|
||||||
|
imageKnifeOption = {
|
||||||
|
loadSrc: url
|
||||||
|
};
|
||||||
|
}
|
||||||
|
let key = this.getEngineKeyImpl().generateFileKey(imageKnifeOption.loadSrc, imageKnifeOption.signature);
|
||||||
|
if (this.fileCache !== undefined) {
|
||||||
|
this.fileCache.remove(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置taskpool默认并发数量
|
||||||
|
* @param concurrency 默认并发数量,默认为8
|
||||||
|
*/
|
||||||
|
setMaxRequests(concurrency: number): void {
|
||||||
|
this.dispatcher.setMaxRequests(concurrency)
|
||||||
|
}
|
||||||
|
|
||||||
|
getFileCacheByFile(context: Context, key: string): ArrayBuffer | undefined {
|
||||||
|
if (this.fileCache !== undefined) {
|
||||||
|
return FileCache.getFileCacheByFile(context, key)
|
||||||
|
}
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
loadFromMemoryCache(key: string): ImageKnifeData | undefined {
|
||||||
|
if (key !== "") {
|
||||||
|
return this.memoryCache.get(key)
|
||||||
|
}
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
saveMemoryCache(key: string, data: ImageKnifeData): void {
|
||||||
|
if (key !== "") {
|
||||||
|
this.memoryCache.put(key, data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loadFromFileCache(key: string): ArrayBuffer | undefined {
|
||||||
|
return this.fileCache?.get(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
saveFileCache(key: string, data: ArrayBuffer): void {
|
||||||
|
this.fileCache?.put(key, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
getFileCache(): FileCache {
|
||||||
|
return this.fileCache as FileCache
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private pixelMapToArrayBuffer(pixelMap: PixelMap): ArrayBuffer {
|
private pixelMapToArrayBuffer(pixelMap: PixelMap): ArrayBuffer {
|
||||||
let imageInfo = pixelMap.getImageInfoSync();
|
let imageInfo = pixelMap.getImageInfoSync();
|
||||||
let readBuffer: ArrayBuffer = new ArrayBuffer(imageInfo.size.height * imageInfo.size.width * 4);
|
let readBuffer: ArrayBuffer = new ArrayBuffer(imageInfo.size.height * imageInfo.size.width * 4);
|
||||||
|
@ -308,33 +353,6 @@ export class ImageKnife {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 清除所有文件缓存
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async removeAllFileCache(): Promise<void> {
|
|
||||||
if (this.fileCache !== undefined) {
|
|
||||||
await this.fileCache.removeAll()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
* 清除指定文件缓存
|
|
||||||
* */
|
|
||||||
removeFileCache(url: string | ImageKnifeOption) {
|
|
||||||
let imageKnifeOption: ImageKnifeOption;
|
|
||||||
if (url instanceof ImageKnifeOption) {
|
|
||||||
imageKnifeOption = url;
|
|
||||||
} else {
|
|
||||||
imageKnifeOption = {
|
|
||||||
loadSrc: url
|
|
||||||
};
|
|
||||||
}
|
|
||||||
let key = this.getEngineKeyImpl().generateFileKey(imageKnifeOption.loadSrc, imageKnifeOption.signature);
|
|
||||||
if (this.fileCache !== undefined) {
|
|
||||||
this.fileCache.remove(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
saveWithoutWriteFile(key: string, bufferSize: number): void {
|
saveWithoutWriteFile(key: string, bufferSize: number): void {
|
||||||
this.fileCache?.putWithoutWriteFile(key, bufferSize)
|
this.fileCache?.putWithoutWriteFile(key, bufferSize)
|
||||||
}
|
}
|
||||||
|
@ -346,13 +364,6 @@ export class ImageKnife {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
getFileCacheByFile(context: Context, key: string): ArrayBuffer | undefined {
|
|
||||||
if (this.fileCache !== undefined) {
|
|
||||||
return FileCache.getFileCacheByFile(context, key)
|
|
||||||
}
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
async execute(request: ImageKnifeRequest): Promise<void> {
|
async execute(request: ImageKnifeRequest): Promise<void> {
|
||||||
LogUtil.log("ImageKnife_DataTime_execute.start:"+request.imageKnifeOption.loadSrc)
|
LogUtil.log("ImageKnife_DataTime_execute.start:"+request.imageKnifeOption.loadSrc)
|
||||||
if (this.headerMap.size > 0) {
|
if (this.headerMap.size > 0) {
|
||||||
|
@ -362,10 +373,6 @@ export class ImageKnife {
|
||||||
LogUtil.log("ImageKnife_DataTime_execute.end:"+request.imageKnifeOption.loadSrc)
|
LogUtil.log("ImageKnife_DataTime_execute.end:"+request.imageKnifeOption.loadSrc)
|
||||||
}
|
}
|
||||||
|
|
||||||
setMaxRequests(concurrency: number): void {
|
|
||||||
this.dispatcher.setMaxRequests(concurrency)
|
|
||||||
}
|
|
||||||
|
|
||||||
setEngineKeyImpl(impl: IEngineKey): void {
|
setEngineKeyImpl(impl: IEngineKey): void {
|
||||||
this.dispatcher.setEngineKeyImpl(impl);
|
this.dispatcher.setEngineKeyImpl(impl);
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,8 +60,12 @@ export class FileCache {
|
||||||
if (this.isInited) {
|
if (this.isInited) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (this.context && path.startsWith(this.context.cacheDir) === false) {
|
||||||
|
this.path = path
|
||||||
|
} else {
|
||||||
FileCache.CACHE_FOLDER = path
|
FileCache.CACHE_FOLDER = path
|
||||||
this.path = this.context?.cacheDir + FileUtils.SEPARATOR + FileCache.CACHE_FOLDER + FileUtils.SEPARATOR
|
this.path = this.context?.cacheDir + FileUtils.SEPARATOR + FileCache.CACHE_FOLDER + FileUtils.SEPARATOR
|
||||||
|
}
|
||||||
await FileUtils.getInstance().createFolder(this.path)
|
await FileUtils.getInstance().createFolder(this.path)
|
||||||
// 遍历缓存目录下的文件,按照时间顺序加入缓存
|
// 遍历缓存目录下的文件,按照时间顺序加入缓存
|
||||||
let filenames: string[] = await FileUtils.getInstance().ListFile(this.path)
|
let filenames: string[] = await FileUtils.getInstance().ListFile(this.path)
|
||||||
|
|
Loading…
Reference in New Issue