Compute image hashes without having to load the entire file in memory

This commit is contained in:
Vitor Pamplona
2026-04-02 17:48:31 -04:00
parent 15a3a028b7
commit 9893279b03
@@ -109,7 +109,7 @@ import com.vitorpamplona.quartz.nip19Bech32.Nip19Parser
import com.vitorpamplona.quartz.nip19Bech32.entities.NEvent import com.vitorpamplona.quartz.nip19Bech32.entities.NEvent
import com.vitorpamplona.quartz.nip94FileMetadata.tags.DimensionTag import com.vitorpamplona.quartz.nip94FileMetadata.tags.DimensionTag
import com.vitorpamplona.quartz.utils.Log import com.vitorpamplona.quartz.utils.Log
import com.vitorpamplona.quartz.utils.sha256.sha256 import com.vitorpamplona.quartz.utils.sha256.sha256StreamWithCount
import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
@@ -1035,8 +1035,8 @@ private fun verifyHash(content: MediaUrlContent): Boolean? {
if (content.hash == null) return null if (content.hash == null) return null
Amethyst.instance.diskCache.openSnapshot(content.url)?.use { snapshot -> Amethyst.instance.diskCache.openSnapshot(content.url)?.use { snapshot ->
val hash = sha256(snapshot.data.toFile().readBytes()).toHexKey() val (hashBytes, _) = sha256StreamWithCount(snapshot.data.toFile().inputStream())
return hash == content.hash return hashBytes.toHexKey() == content.hash
} }
return null return null