Pre Merge pull request !326 from zgf/3.x

This commit is contained in:
zgf 2024-07-12 08:10:36 +00:00 committed by Gitee
commit 61b4d3bec7
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 25 additions and 9 deletions

View File

@ -175,7 +175,7 @@ export class ImageKnifeDispatcher {
requestSource: requestSource,
isWatchProgress: isWatchProgress,
memoryKey: memoryKey,
fileCacheFolder: ImageKnife.getInstance().getFileCache().getCacheFolder()
fileCacheFolder: ImageKnife.getInstance().getFileCache()?.getCacheFolder()
}

View File

@ -60,13 +60,17 @@ export class FileCache {
if (this.isInited) {
return
}
if (this.context && path.startsWith(this.context.cacheDir) === true) {
this.path = path
if( path.includes("/") ) {
if ( await FileUtils.getInstance().createFolder(path+"/")) {
throw Error("Failed to create the directory")
} else {
this.path = path
}
} else {
FileCache.CACHE_FOLDER = path
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)
@ -112,7 +116,11 @@ export class FileCache {
}
public getCacheFolder(): string {
return FileCache.CACHE_FOLDER
if(this.context && this.path.startsWith(this.context.cacheDir) == true) {
return FileCache.CACHE_FOLDER
} else {
return this.path
}
}
// 添加缓存键值对,同时写文件
@ -254,9 +262,11 @@ export class FileCache {
* @param value
*/
static saveFileCacheOnlyFile(context: Context, key: string, value: ArrayBuffer, folder: string = FileCache.CACHE_FOLDER): boolean {
let path = context.cacheDir + FileUtils.SEPARATOR + folder
path = folder.includes("/") ? folder : path
// 写文件
FileUtils.getInstance()
.writeFileSync(context.cacheDir + FileUtils.SEPARATOR + folder + FileUtils.SEPARATOR + key, value)
.writeFileSync(path + FileUtils.SEPARATOR + key, value)
return true
}
@ -267,9 +277,11 @@ export class FileCache {
* @returns
*/
static getFileCacheByFile(context: Context, key: string, folder: string = FileCache.CACHE_FOLDER): ArrayBuffer | undefined {
let path = context.cacheDir + FileUtils.SEPARATOR + folder
path = folder.includes("/") ? folder : path
// 从文件获取查看是否有缓存
return FileUtils.getInstance()
.readFileSync(context.cacheDir + FileUtils.SEPARATOR + folder + FileUtils.SEPARATOR + key)
.readFileSync(path + FileUtils.SEPARATOR + key)
}
/**

View File

@ -15,7 +15,11 @@
import { ImageKnife } from '@ohos/imageknife'
import common from '@ohos.app.ability.common'
export class InitImageKnife{
static async init(entryContext:common.UIAbilityContext){
await ImageKnife.getInstance().initFileCache(entryContext, 256, 256 * 1024 * 1024,"ImageKnifeCache")
static async init(entryContext:common.UIAbilityContext,flag: boolean = true){
if ( flag ) {
await ImageKnife.getInstance().initFileCache(entryContext, 256, 256 * 1024 * 1024,"ImageKnifeCache")
} else {
await ImageKnife.getInstance().initFileCache(entryContext, 256, 256 * 1024 * 1024,"/data/storage/el2/100/hap/files/ImageKnife")
}
}
}