Fix migration of pages rotated with previous versions of the app

This commit is contained in:
Pierre-Yves Nicolas
2026-01-17 17:46:26 +01:00
parent 2eaede0713
commit 7d74c1cd46
2 changed files with 18 additions and 12 deletions

View File

@@ -85,7 +85,7 @@ class ImageRepository(
else -> else ->
filesOnDisk filesOnDisk
.sorted() .sorted()
.map { pageFromFileName(it) } .map { pageFromLegacyFileName(it) }
.toMutableList() .toMutableList()
} }
} }
@@ -104,20 +104,16 @@ class ImageRepository(
} }
private fun migrateFromV1(meta: DocumentMetadataV1): MutableList<PageV2> { private fun migrateFromV1(meta: DocumentMetadataV1): MutableList<PageV2> {
return meta.pages.map { return meta.pages.map { old ->
old -> pageFromFileName(old.file) pageFromLegacyFileName(old.file)
}.toMutableList() }.toMutableList()
} }
private fun pageFromFileName(fileName: String): PageV2 { private fun pageFromLegacyFileName(fileName: String): PageV2 {
val fileName = fileName.removeSuffix(".jpg") val name = fileName.removeSuffix(".jpg")
val dashIndex = fileName.lastIndexOf('-') val dashIndex = name.lastIndexOf('-')
val rotation = if (dashIndex >= 0) val id = if (dashIndex >= 0) name.substring(0, dashIndex) else name
fileName.substring(dashIndex + 1).toInt() return PageV2(id)
else
0
val id = if (dashIndex >= 0) fileName.substring(0, dashIndex) else fileName
return PageV2(id, manualRotationDegrees = rotation)
} }
private fun saveMetadata() { private fun saveMetadata() {

View File

@@ -156,6 +156,16 @@ class ImageRepositoryTest {
assertThat(jpegFiles).hasSize(1).allMatch { it?.name == "123.jpg" } assertThat(jpegFiles).hasSize(1).allMatch { it?.name == "123.jpg" }
} }
@Test
fun `should rename rotated files with no base file but listed in json`() {
writeDocumentDotJson("""{"pages":[{"file":"1-90.jpg"}]}""")
val bytes = byteArrayOf(105, 106, 107)
File(scanDir(), "1-90.jpg").writeBytes(bytes)
val repo = repo()
assertThat(repo.imageIds()).containsExactly("1")
assertThat(repo.jpegBytes("1")).isEqualTo(bytes)
}
@Test @Test
fun `should return null on invalid id`() { fun `should return null on invalid id`() {
val repo = repo() val repo = repo()