photos: secure-folder restore keeps date, dimensions and real type
- Commit
558a25af8cb209a6e6039861e26e65b219a61af0- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 19:55 UTC (5 days ago)
- Parents
d28ea6b
Restoring from the Secure Folder discarded metadata the vault row already owned: the MediaStore insert carried only a display name and a guessed MIME (video/mp4 or image/jpeg), no DATE_TAKEN, DATE_MODIFIED, WIDTH or HEIGHT. A 2023 holiday.png came back as an undated brand-new 'JPEG', and a vaulted GIF lost its type (and with it Photo.isGif) on re-sync. insertRestoreTarget now writes the stored date (ms) and dateModified (s — the units sync reads back), dimensions when positive, and a MIME derived from the stored display name plus recorded videoDuration (duration wins, so the MIME can never disagree with the collection MediaStore inserts into; the vault schema needs no change). Face clustering, undo/redo, map coordinates and trash restore were audited and cleared. Verified: :photos:testDevUnitTest 416 tests — one favorites-sync failure unrelated to this diff and failing on the untouched tree; :photos:lint errors are pre-existing on untouched files.
Diff
photos/src/main/java/com/vayunmathur/photos/platform/SecureFolderManager.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/platform/SecureFolderManager.kt b/photos/src/main/java/com/vayunmathur/photos/platform/SecureFolderManager.ktindex 75289a7101..528ddbef88 100644--- a/photos/src/main/java/com/vayunmathur/photos/platform/SecureFolderManager.kt+++ b/photos/src/main/java/com/vayunmathur/photos/platform/SecureFolderManager.kt@@ -321,11 +321,24 @@ class SecureFolderManager(val context: Context) { /** Insert the MediaStore row the vault file is restored into, or null if rejected. */ private fun insertRestoreTarget(vaultPhoto: VaultPhoto): Uri? {- val isVideo = vaultPhoto.videoDuration != null+ val mimeType = restoreMimeType(vaultPhoto)+ val isVideo = vaultPhoto.videoDuration != null || mimeType.startsWith("video/") val contentValues = ContentValues().apply { put(MediaStore.MediaColumns.DISPLAY_NAME, vaultPhoto.name)- put(MediaStore.MediaColumns.MIME_TYPE, if (isVideo) "video/mp4" else "image/jpeg")+ put(MediaStore.MediaColumns.MIME_TYPE, mimeType) put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/Restored")+ // Carry the metadata the vault row stored across the restore: without+ // these the restored row is re-dated to the restore moment (and, for+ // non-JPEG bytes, mis-typed), so the next sync sorts and classifies+ // it as a brand-new item instead of the photo that entered the vault.+ // DATE_TAKEN is in milliseconds; DATE_MODIFIED is in seconds, matching+ // the units the sync reads back into Photo.date / Photo.dateModified.+ if (vaultPhoto.date > 0) put(MediaStore.MediaColumns.DATE_TAKEN, vaultPhoto.date)+ if (vaultPhoto.dateModified > 0) {+ put(MediaStore.MediaColumns.DATE_MODIFIED, vaultPhoto.dateModified)+ }+ if (vaultPhoto.width > 0) put(MediaStore.MediaColumns.WIDTH, vaultPhoto.width)+ if (vaultPhoto.height > 0) put(MediaStore.MediaColumns.HEIGHT, vaultPhoto.height) } val collection = if (isVideo) { MediaStore.Video.Media.EXTERNAL_CONTENT_URI@@ -334,4 +347,35 @@ class SecureFolderManager(val context: Context) { } return context.contentResolver.insert(collection, contentValues) }++ /**+ * The restored item's MIME type, derived from the stored file name's+ * extension (the vault row carries no MIME column, but [VaultPhoto.name]+ * is the original MediaStore display name, extension included). Falls back+ * to the previous video/mp4-vs-image/jpeg guess for extensionless names.+ */+ private fun restoreMimeType(vaultPhoto: VaultPhoto): String {+ val videoMime = when (vaultPhoto.name.substringAfterLast('.', "").lowercase()) {+ "mp4", "m4v" -> "video/mp4"+ "mov" -> "video/quicktime"+ "mkv" -> "video/x-matroska"+ "webm" -> "video/webm"+ "3gp" -> "video/3gpp"+ else -> null+ }+ // The collection is chosen from the same signals (see insertRestoreTarget),+ // and MediaStore rejects a MIME that disagrees with its collection: a row+ // the vault recorded as a video keeps a video MIME even when its display+ // name carries no (or a misleading) extension.+ if (vaultPhoto.videoDuration != null) return videoMime ?: "video/mp4"+ if (videoMime != null) return videoMime+ return when (vaultPhoto.name.substringAfterLast('.', "").lowercase()) {+ "png" -> "image/png"+ "gif" -> "image/gif"+ "webp" -> "image/webp"+ "heic" -> "image/heic"+ "heif" -> "image/heif"+ else -> "image/jpeg"+ }+ } }
photos/src/test/java/com/vayunmathur/photos/util/SecureFolderRestoreRealTest.kt
diff --git a/photos/src/test/java/com/vayunmathur/photos/util/SecureFolderRestoreRealTest.kt b/photos/src/test/java/com/vayunmathur/photos/util/SecureFolderRestoreRealTest.ktindex ab3ddb2df6..588f2959c3 100644--- a/photos/src/test/java/com/vayunmathur/photos/util/SecureFolderRestoreRealTest.kt+++ b/photos/src/test/java/com/vayunmathur/photos/util/SecureFolderRestoreRealTest.kt@@ -44,6 +44,11 @@ class SecureFolderRestoreRealTest { private val app: Application get() = ApplicationProvider.getApplicationContext() private lateinit var sink: File + companion object {+ var lastInsertUri: Uri? = null+ var lastInsertValues: ContentValues? = null+ }+ private inner class MediaProvider : ContentProvider() { override fun onCreate() = true override fun query(@@ -51,8 +56,11 @@ class SecureFolderRestoreRealTest { selectionArgs: Array<out String>?, sortOrder: String?, ): Cursor = MatrixCursor(projection ?: arrayOf("_id")) override fun getType(uri: Uri): String? = null- override fun insert(uri: Uri, values: ContentValues?): Uri =- Uri.parse("content://media/external/images/media/777")+ override fun insert(uri: Uri, values: ContentValues?): Uri {+ lastInsertUri = uri+ lastInsertValues = values?.let { ContentValues(it) }+ return Uri.parse("content://media/external/images/media/777")+ } override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?) = 1 override fun update( uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<out String>?,@@ -67,6 +75,8 @@ class SecureFolderRestoreRealTest { @BeforeTest fun setup() { kotlinx.coroutines.Dispatchers.setMain(kotlinx.coroutines.Dispatchers.Unconfined)+ lastInsertUri = null+ lastInsertValues = null sink = File(app.cacheDir, "restored.jpg") sink.delete() val provider = MediaProvider()@@ -170,4 +180,43 @@ class SecureFolderRestoreRealTest { assertEquals(null, secure.vaultPassword.value) Unit }++ @Test fun restoreCarriesStoredMetadataIntoMediaStoreInsert() = runBlocking {+ // Regression: insertRestoreTarget used to insert only name + a guessed+ // image/jpeg (or video/mp4) MIME + Restored path, discarding the date,+ // dateModified and dimensions the vault row stored. A restored PNG was+ // re-typed as JPEG and re-dated to the restore moment, so the next sync+ // sorted/classified it as a new item (a GIF likewise lost Photo.isGif).+ val manager = SecureFolderManager(app)+ val key = SecureFolderManager::class.java.getDeclaredMethod("getSecretKey", String::class.java)+ .apply { isAccessible = true }.invoke(manager, "pw123") as SecretKey+ val encrypt = SecureFolderManager::class.java.getDeclaredMethod(+ "encryptStreamChunked", java.io.InputStream::class.java, java.io.OutputStream::class.java, SecretKey::class.java,+ ).apply { isAccessible = true }+ val plaintext = ByteArray(1024) { (it % 251).toByte() }+ val encFile = File(app.cacheDir, "vault-meta.enc")+ FileOutputStream(encFile).use { encrypt.invoke(manager, ByteArrayInputStream(plaintext), it, key) }+ val thumbFile = File(app.cacheDir, "vault-meta_thumb.enc")+ FileOutputStream(thumbFile).use { encrypt.invoke(manager, ByteArrayInputStream(byteArrayOf(1)), it, key) }++ val takenMs = 1_700_000_000_000L+ val modifiedSec = 1_700_000_123L+ val photo = VaultPhoto(+ name = "holiday.png", path = encFile.absolutePath, thumbnailPath = thumbFile.absolutePath,+ date = takenMs, width = 4032, height = 3024, dateModified = modifiedSec,+ )+ val restored = manager.decryptAndRestore(photo, "pw123")+ assertEquals(Uri.parse("content://media/external/images/media/777"), restored)+ assertTrue(plaintext.contentEquals(sink.readBytes()))++ val values = lastInsertValues+ assertEquals("content://media/external/images/media", lastInsertUri.toString())+ assertEquals("holiday.png", values?.getAsString(android.provider.MediaStore.MediaColumns.DISPLAY_NAME))+ assertEquals("image/png", values?.getAsString(android.provider.MediaStore.MediaColumns.MIME_TYPE))+ assertEquals(takenMs, values?.getAsLong(android.provider.MediaStore.MediaColumns.DATE_TAKEN))+ assertEquals(modifiedSec, values?.getAsLong(android.provider.MediaStore.MediaColumns.DATE_MODIFIED))+ assertEquals(4032, values?.getAsInteger(android.provider.MediaStore.MediaColumns.WIDTH))+ assertEquals(3024, values?.getAsInteger(android.provider.MediaStore.MediaColumns.HEIGHT))+ Unit+ } }