diff --git a/apps/web/src/lib/heic-converter.ts b/apps/web/src/lib/heic-converter.ts index ca7653a1..d4971157 100644 --- a/apps/web/src/lib/heic-converter.ts +++ b/apps/web/src/lib/heic-converter.ts @@ -21,7 +21,7 @@ const heicCache: LRUCache = new LRUCache< string, ConversionResult >( - 5, // Smaller cache size for images as they might be larger + 10, // Smaller cache size for images as they might be larger (value, key, reason) => { try { URL.revokeObjectURL(value.url) @@ -33,18 +33,13 @@ const heicCache: LRUCache = new LRUCache< ) /** - * 生成文件的缓存键 + * 生成文件的缓存键(基于 src) */ -function generateCacheKey( - file: File | Blob, - options: HeicConversionOptions, -): string { - const { size } = file - const { type } = file +function generateCacheKey(src: string, options: HeicConversionOptions): string { const quality = options.quality || 1 const format = options.format || 'image/jpeg' - // 使用文件大小、类型和转换选项生成唯一键 - return `${type}-${size}-${quality}-${format}` + // 使用文件 src 和转换选项生成唯一键 + return `${src}-${quality}-${format}` } /** @@ -72,17 +67,18 @@ export const isBrowserSupportHeic = () => { */ export async function convertHeicImage( file: File | Blob, + src: string, options: HeicConversionOptions = {}, ): Promise { const { quality = 1, format = 'image/jpeg' } = options // 生成缓存键 - const cacheKey = generateCacheKey(file, options) + const cacheKey = generateCacheKey(src, options) // 检查缓存 const cachedResult = heicCache.get(cacheKey) if (cachedResult) { - console.info('Using cached HEIC conversion result') + console.info('Using cached HEIC conversion result', cachedResult) return cachedResult } @@ -159,12 +155,12 @@ export function getHeicCacheStats(): { } /** - * 根据文件和选项移除特定的 HEIC 缓存项 + * 根据 src 和选项移除特定的 HEIC 缓存项 */ -export function removeHeicCacheByFile( - file: File | Blob, +export function removeHeicCacheBySrc( + src: string, options: HeicConversionOptions = {}, ): boolean { - const cacheKey = generateCacheKey(file, options) + const cacheKey = generateCacheKey(src, options) return heicCache.delete(cacheKey) } diff --git a/apps/web/src/lib/image-loader-manager.ts b/apps/web/src/lib/image-loader-manager.ts index 50b6e37a..aa7d7c3e 100644 --- a/apps/web/src/lib/image-loader-manager.ts +++ b/apps/web/src/lib/image-loader-manager.ts @@ -203,7 +203,7 @@ export class ImageLoaderManager { }) if (shouldHeicTransformed) { - return await this.processHeicImage(blob, callbacks) + return await this.processHeicImage(blob, originalUrl, callbacks) } else { return this.processRegularImage(blob, originalUrl, callbacks) // 传递原始 URL } @@ -216,6 +216,7 @@ export class ImageLoaderManager { private async processHeicImage( blob: Blob, + originalUrl: string, callbacks: LoadingCallbacks, ): Promise { const { onError: _onError, onLoadingStateUpdate } = callbacks @@ -230,7 +231,7 @@ export class ImageLoaderManager { // 动态导入 heic-converter 模块 const { convertHeicImage } = await import('~/lib/heic-converter') - const conversionResult = await convertHeicImage(blob) + const conversionResult = await convertHeicImage(blob, originalUrl) // Hide loading indicator onLoadingStateUpdate?.({ @@ -271,7 +272,7 @@ export class ImageLoaderManager { // 检查缓存 const cachedResult = regularImageCache.get(cacheKey) if (cachedResult) { - console.info('Using cached regular image result') + console.info('Using cached regular image result', cachedResult) // Hide loading indicator onLoadingStateUpdate?.({ diff --git a/apps/web/src/lib/video-converter.ts b/apps/web/src/lib/video-converter.ts index 1de9c45b..570f4654 100644 --- a/apps/web/src/lib/video-converter.ts +++ b/apps/web/src/lib/video-converter.ts @@ -30,37 +30,6 @@ const videoCache: LRUCache = new LRUCache< } }) -// Export cache management functions -export function getVideoCacheSize(): number { - return videoCache.size() -} - -export function clearVideoCache(): void { - videoCache.clear() -} - -export function getCachedVideo(url: string): ConversionResult | undefined { - return videoCache.get(url) -} - -/** - * Remove a specific video from cache and clean up its blob URL - */ -export function removeCachedVideo(url: string): boolean { - return videoCache.delete(url) -} - -/** - * Get detailed cache statistics for debugging - */ -export function getVideoCacheStats(): { - size: number - maxSize: number - keys: string[] -} { - return videoCache.getStats() -} - // 检查 WebCodecs 支持 export function isWebCodecsSupported(): boolean { return (