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

View File

@@ -156,6 +156,16 @@ class ImageRepositoryTest {
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
fun `should return null on invalid id`() {
val repo = repo()