commit 5c557bc87d5775823f122f965daf9a4479ea39ed
parent af407f7eeaddd817c301abdc4c44176505dde2a1
Author: William Casarin <jb55@jb55.com>
Date: Wed, 3 May 2023 17:26:33 -0700
Don't process blurhash if we already have image cached
Changelog-Fixed: Don't process blurhash if we already have image cached
Diffstat:
2 files changed, 14 insertions(+), 27 deletions(-)
diff --git a/damus/Util/EventCache.swift b/damus/Util/EventCache.swift
@@ -385,10 +385,6 @@ func preload_event(plan: PreloadPlan, state: DamusState) async {
print("Preloading event \(plan.event.content)")
- for meta in plan.img_metadata {
- process_image_metadata(cache: state.events, meta: meta, ev: plan.event)
- }
-
preload_pfp(profiles: profiles, pubkey: plan.event.pubkey)
if let inner_ev = plan.event.get_inner_event(cache: state.events), inner_ev.pubkey != plan.event.pubkey {
preload_pfp(profiles: profiles, pubkey: inner_ev.pubkey)
diff --git a/damus/Util/Images/ImageMetadata.swift b/damus/Util/Images/ImageMetadata.swift
@@ -7,6 +7,7 @@
import Foundation
import UIKit
+import Kingfisher
struct ImageMetaDim: Equatable, StringCodable {
init(width: Int, height: Int) {
@@ -191,39 +192,29 @@ func process_image_metadatas(cache: EventCache, ev: NostrEvent) {
continue
}
+ // We don't need blurhash if we already have the source image cached
+ if ImageCache.default.isCached(forKey: meta.url.absoluteString) {
+ continue
+ }
+
let state = ImageMetadataState(state: meta.blurhash == nil ? .not_needed : .processing, meta: meta)
cache.store_img_metadata(url: meta.url, meta: state)
- guard let blurhash = meta.blurhash else {
+ guard let blurhash = state.meta.blurhash else {
return
}
Task {
- guard let img = await process_blurhash(blurhash: blurhash, size: meta.dim?.size) else {
- return
- }
+ let img = await process_blurhash(blurhash: blurhash, size: state.meta.dim?.size)
+
Task { @MainActor in
- state.state = .processed(img)
+ if let img {
+ state.state = .processed(img)
+ } else {
+ state.state = .failed
+ }
}
}
}
}
-func process_image_metadata(cache: EventCache, meta: ImageMetadata, ev: NostrEvent) {
- guard let blurhash = meta.blurhash else {
- return
- }
- Task {
- let img = await process_blurhash(blurhash: blurhash, size: meta.dim?.size)
-
- DispatchQueue.main.async {
- if let img {
- let state = ImageMetadataState(state: .processed(img), meta: meta)
- cache.store_img_metadata(url: meta.url, meta: state)
- } else {
- let state = ImageMetadataState(state: .failed, meta: meta)
- cache.store_img_metadata(url: meta.url, meta: state)
- }
- }
- }
-}