Modern Apps git

← Modern-Apps.git

notes: stream image hashing instead of loading whole file

Commit
a1ea0d731e1f9639736caa52d0ae12d1624be93e
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:11 UTC (28 days ago)
Parents
17dcbec
Symptom: importing a large photo could OOM-crash the app: after
copying the file, contentName() called readBytes() to compute the
content-addressed name, loading the entire (potentially tens of
MB) image into memory a second time.

Root cause: NoteImageStore.contentName hashed file.readBytes().

Fix: stream the file through MessageDigest in 64KB chunks. The
resulting SHA-256 names are byte-identical to before, so existing
content-addressed files and relay blob keys are unaffected.

Verification: existing NoteStoresTest import/migration cases pin
the exact hashed names and dedupe behaviour; full notes unit-test
suite run at the end of this stack.

Diff

notes/src/main/java/com/vayunmathur/notes/platform/NoteImageStore.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/platform/NoteImageStore.kt b/notes/src/main/java/com/vayunmathur/notes/platform/NoteImageStore.ktindex 1cc2d74f20..f9d9f05e20 100644--- a/notes/src/main/java/com/vayunmathur/notes/platform/NoteImageStore.kt+++ b/notes/src/main/java/com/vayunmathur/notes/platform/NoteImageStore.kt@@ -135,10 +135,18 @@ object NoteImageStore {         }     } -    private fun contentName(file: File): String = "${sha256Hex(file.readBytes())}.jpg"--    private fun sha256Hex(bytes: ByteArray): String {+    // Streamed, not readBytes(): imported photos can be tens of MB, and loading+    // the whole file to name it risks OOM on the import path.+    private fun contentName(file: File): String {         val digest = MessageDigest.getInstance("SHA-256")-        return digest.digest(bytes).joinToString("") { "%02x".format(it) }+        file.inputStream().use { input ->+            val buffer = ByteArray(64 * 1024)+            while (true) {+                val read = input.read(buffer)+                if (read < 0) break+                digest.update(buffer, 0, read)+            }+        }+        return "${digest.digest().joinToString("") { "%02x".format(it) }}.jpg"     } }