Add no-I/O library snapshot cover restoration
This commit is contained in:
@@ -50,6 +50,15 @@ private const val PREFIX = "mcf:"
|
|||||||
* @param context The [Context] to use to access the filesystem and check for ID validity.
|
* @param context The [Context] to use to access the filesystem and check for ID validity.
|
||||||
*/
|
*/
|
||||||
class FSCovers(private val context: Context) : Covers<FDCover> {
|
class FSCovers(private val context: Context) : Covers<FDCover> {
|
||||||
|
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<FDCover> {
|
override suspend fun obtain(id: String): CoverResult<FDCover> {
|
||||||
if (!id.startsWith(PREFIX)) {
|
if (!id.startsWith(PREFIX)) {
|
||||||
return CoverResult.Miss()
|
return CoverResult.Miss()
|
||||||
|
|||||||
@@ -70,6 +70,8 @@ interface CoverStorage {
|
|||||||
suspend fun rm(name: String)
|
suspend fun rm(name: String)
|
||||||
|
|
||||||
companion object {
|
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
|
* Create a [CoverStorage] implementation at some directory. Covers will be written in that
|
||||||
* location.
|
* location.
|
||||||
|
|||||||
@@ -40,6 +40,15 @@ private const val PREFIX = "mcs:"
|
|||||||
* @param coverStorage The [CoverStorage] to use to obtain the cover data.
|
* @param coverStorage The [CoverStorage] to use to obtain the cover data.
|
||||||
*/
|
*/
|
||||||
class StoredCovers(private val coverStorage: CoverStorage) : Covers<FDCover> {
|
class StoredCovers(private val coverStorage: CoverStorage) : Covers<FDCover> {
|
||||||
|
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<FDCover> {
|
override suspend fun obtain(id: String): CoverResult<FDCover> {
|
||||||
if (!id.startsWith(PREFIX)) {
|
if (!id.startsWith(PREFIX)) {
|
||||||
return CoverResult.Miss()
|
return CoverResult.Miss()
|
||||||
|
|||||||
@@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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() } }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user