diff --git a/musikr/src/main/java/org/oxycblt/musikr/covers/fs/FSCovers.kt b/musikr/src/main/java/org/oxycblt/musikr/covers/fs/FSCovers.kt index 6a9c26d3f..2e6dc2c92 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/covers/fs/FSCovers.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/covers/fs/FSCovers.kt @@ -50,6 +50,15 @@ private const val PREFIX = "mcf:" * @param context The [Context] to use to access the filesystem and check for ID validity. */ class FSCovers(private val context: Context) : Covers { + companion object { + internal fun restore(context: Context, id: String): FDCover? { + if (!id.startsWith(PREFIX)) { + return null + } + return FolderCoverImpl(context, id.substring(PREFIX.length).toUri()) + } + } + override suspend fun obtain(id: String): CoverResult { if (!id.startsWith(PREFIX)) { return CoverResult.Miss() diff --git a/musikr/src/main/java/org/oxycblt/musikr/covers/stored/CoverStorage.kt b/musikr/src/main/java/org/oxycblt/musikr/covers/stored/CoverStorage.kt index 428653899..1c79430c5 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/covers/stored/CoverStorage.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/covers/stored/CoverStorage.kt @@ -70,6 +70,8 @@ interface CoverStorage { suspend fun rm(name: String) companion object { + internal fun restore(dir: File, name: String): FDCover = FSStoredCover(File(dir, name)) + /** * Create a [CoverStorage] implementation at some directory. Covers will be written in that * location. diff --git a/musikr/src/main/java/org/oxycblt/musikr/covers/stored/StoredCovers.kt b/musikr/src/main/java/org/oxycblt/musikr/covers/stored/StoredCovers.kt index c23bb8cb4..df99fa3e1 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/covers/stored/StoredCovers.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/covers/stored/StoredCovers.kt @@ -40,6 +40,15 @@ private const val PREFIX = "mcs:" * @param coverStorage The [CoverStorage] to use to obtain the cover data. */ class StoredCovers(private val coverStorage: CoverStorage) : Covers { + companion object { + internal fun restore(id: String, directory: java.io.File): FDCover? { + if (!id.startsWith(PREFIX)) { + return null + } + return StoredCover(CoverStorage.restore(directory, id.substring(PREFIX.length))) + } + } + override suspend fun obtain(id: String): CoverResult { if (!id.startsWith(PREFIX)) { return CoverResult.Miss() diff --git a/musikr/src/main/java/org/oxycblt/musikr/snapshot/LibrarySnapshotCovers.kt b/musikr/src/main/java/org/oxycblt/musikr/snapshot/LibrarySnapshotCovers.kt new file mode 100644 index 000000000..74d57640c --- /dev/null +++ b/musikr/src/main/java/org/oxycblt/musikr/snapshot/LibrarySnapshotCovers.kt @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2026 Auxio Project + * LibrarySnapshotCovers.kt is part of Auxio. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.oxycblt.musikr.snapshot + +import android.content.Context +import java.io.File +import org.oxycblt.musikr.covers.FDCover +import org.oxycblt.musikr.covers.fs.FSCovers +import org.oxycblt.musikr.covers.stored.StoredCovers + +object LibrarySnapshotCovers { + fun restore(id: String?, storedCoverDirectory: File, context: Context): FDCover? = + id?.let { + StoredCovers.restore(it, storedCoverDirectory) ?: FSCovers.restore(context, it) + } +} diff --git a/musikr/src/test/java/org/oxycblt/musikr/snapshot/LibrarySnapshotCoversTest.kt b/musikr/src/test/java/org/oxycblt/musikr/snapshot/LibrarySnapshotCoversTest.kt new file mode 100644 index 000000000..c7f163ce5 --- /dev/null +++ b/musikr/src/test/java/org/oxycblt/musikr/snapshot/LibrarySnapshotCoversTest.kt @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2026 Auxio Project + * LibrarySnapshotCoversTest.kt is part of Auxio. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.oxycblt.musikr.snapshot + +import android.net.Uri +import java.io.File +import java.io.FileNotFoundException +import java.nio.file.Files +import kotlinx.coroutines.runBlocking +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertThrows +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.oxycblt.musikr.covers.CoverResult +import org.oxycblt.musikr.covers.fs.FSCovers +import org.oxycblt.musikr.covers.stored.CoverStorage +import org.oxycblt.musikr.covers.stored.StoredCovers +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [30]) +class LibrarySnapshotCoversTest { + @Test + fun absentCover_restore_returnsNullWithoutCreatingStorage() { + val root = Files.createTempDirectory("auxio-snapshot-covers").toFile().apply { deleteOnExit() } + val storedCoverDirectory = File(root, "covers") + val context = RuntimeEnvironment.getApplication() + + val restored = + LibrarySnapshotCovers.restore( + id = null, + storedCoverDirectory = storedCoverDirectory, + context = context, + ) + + assertNull(restored) + assertFalse(storedCoverDirectory.exists()) + } + + @Test + fun storedCover_restore_preservesLiveIdentityEqualityAndHashCode() { + val storedCoverDirectory = + Files.createTempDirectory("auxio-stored-cover").toFile().apply { deleteOnExit() } + File(storedCoverDirectory, "cover.webp").writeBytes(byteArrayOf(1, 2, 3)) + val id = "mcs:cover.webp" + val context = RuntimeEnvironment.getApplication() + val live = + runBlocking { + when (val result = StoredCovers(CoverStorage.at(storedCoverDirectory)).obtain(id)) { + is CoverResult.Hit -> result.cover + is CoverResult.Miss -> error("Expected existing stored cover") + } + } + + val restored = + requireNotNull( + LibrarySnapshotCovers.restore( + id = id, + storedCoverDirectory = storedCoverDirectory, + context = context, + ) + ) + + assertEquals(id, restored.id) + assertEquals(live, restored) + assertEquals(restored, live) + assertEquals(live.hashCode(), restored.hashCode()) + } + + @Test + fun folderCover_restoreAfterDeletion_preservesLiveIdentityEqualityAndHashCode() { + val root = Files.createTempDirectory("auxio-folder-cover").toFile().apply { deleteOnExit() } + val storedCoverDirectory = File(root, "covers") + val coverFile = File(root, "folder.jpg").apply { writeBytes(byteArrayOf(4, 5, 6)) } + val id = "mcf:${Uri.fromFile(coverFile)}" + val context = RuntimeEnvironment.getApplication() + val live = + runBlocking { + when (val result = FSCovers(context).obtain(id)) { + is CoverResult.Hit -> result.cover + is CoverResult.Miss -> error("Expected existing folder cover") + } + } + assertTrue(coverFile.delete()) + + val restored = + requireNotNull( + LibrarySnapshotCovers.restore( + id = id, + storedCoverDirectory = storedCoverDirectory, + context = context, + ) + ) + + assertFalse(coverFile.exists()) + assertFalse(storedCoverDirectory.exists()) + assertEquals(id, restored.id) + assertEquals(live, restored) + assertEquals(restored, live) + assertEquals(live.hashCode(), restored.hashCode()) + } + + @Test + fun deletedStoredCover_restore_succeedsAndOpenFailsLazily() { + val storedCoverDirectory = + Files.createTempDirectory("auxio-deleted-cover").toFile().apply { deleteOnExit() } + val coverFile = File(storedCoverDirectory, "deleted.webp") + coverFile.writeBytes(byteArrayOf(7, 8, 9)) + assertTrue(coverFile.delete()) + val id = "mcs:deleted.webp" + val context = RuntimeEnvironment.getApplication() + + val restored = + requireNotNull( + LibrarySnapshotCovers.restore( + id = id, + storedCoverDirectory = storedCoverDirectory, + context = context, + ) + ) + + assertEquals(id, restored.id) + assertThrows(FileNotFoundException::class.java) { runBlocking { restored.open() } } + } +}