1
0
mirror of git://jb55.com/damus synced 2024-09-19 19:46:51 +00:00

Don't process blurhash if we already have image cached

Changelog-Fixed: Don't process blurhash if we already have image cached
This commit is contained in:
William Casarin 2023-05-03 17:26:33 -07:00
parent af407f7eea
commit 5c557bc87d
2 changed files with 14 additions and 27 deletions

View File

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

View File

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