musikr: revamp cover system
Retains the stateless attributes of the older system but massively simplifies it compared to prior.
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Auxio Project
|
||||
* CoverModule.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.auxio.image.covers
|
||||
|
||||
import dagger.Binds
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import org.oxycblt.musikr.covers.embedded.CoverIdentifier
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
interface CoverModule {
|
||||
@Binds fun configCovers(impl: SettingCoversImpl): SettingCovers
|
||||
}
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
class CoverProvidesModule {
|
||||
@Provides fun identifier(): CoverIdentifier = CoverIdentifier.md5()
|
||||
}
|
||||
@@ -19,28 +19,6 @@
|
||||
package org.oxycblt.auxio.image.covers
|
||||
|
||||
import java.util.UUID
|
||||
import org.oxycblt.musikr.covers.embedded.CoverParams
|
||||
|
||||
data class CoverSilo(val revision: UUID, val params: CoverParams?) {
|
||||
override fun toString() =
|
||||
"${revision}${params?.let { ".${params.resolution}.${params.quality}" } ?: "" }"
|
||||
|
||||
companion object {
|
||||
fun parse(silo: String): CoverSilo? {
|
||||
val parts = silo.split('.')
|
||||
if (parts.size != 1 && parts.size != 3) {
|
||||
return null
|
||||
}
|
||||
val revision = parts[0].toUuidOrNull() ?: return null
|
||||
if (parts.size > 1) {
|
||||
val resolution = parts[1].toIntOrNull() ?: return null
|
||||
val quality = parts[2].toIntOrNull() ?: return null
|
||||
return CoverSilo(revision, CoverParams.of(resolution, quality))
|
||||
}
|
||||
return CoverSilo(revision, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun String.toUuidOrNull(): UUID? =
|
||||
try {
|
||||
|
||||
@@ -18,20 +18,20 @@
|
||||
|
||||
package org.oxycblt.auxio.image.covers
|
||||
|
||||
import android.content.Context
|
||||
import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.covers.CoverResult
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.covers.stored.CoverStorage
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.metadata.Metadata
|
||||
|
||||
class NullCovers(private val context: Context) : MutableCovers<NullCover> {
|
||||
class NullCovers(private val storage: CoverStorage) : MutableCovers<NullCover> {
|
||||
override suspend fun obtain(id: String) = CoverResult.Hit(NullCover)
|
||||
|
||||
override suspend fun create(file: DeviceFile, metadata: Metadata) = CoverResult.Hit(NullCover)
|
||||
|
||||
override suspend fun cleanup(excluding: Collection<Cover>) {
|
||||
context.coversDir().listFiles()?.forEach { it.deleteRecursively() }
|
||||
storage.ls(setOf()).map { storage.rm(it) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+7
-7
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Auxio Project
|
||||
* CoverUtil.kt is part of Auxio.
|
||||
* Copyright (c) 2025 Auxio Project
|
||||
* RevisionedTranscoding.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
|
||||
@@ -18,9 +18,9 @@
|
||||
|
||||
package org.oxycblt.auxio.image.covers
|
||||
|
||||
import android.content.Context
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.UUID
|
||||
import org.oxycblt.musikr.covers.stored.Transcoding
|
||||
|
||||
suspend fun Context.coversDir() =
|
||||
withContext(Dispatchers.IO) { filesDir.resolve("covers").apply { mkdirs() } }
|
||||
class RevisionedTranscoding(revision: UUID, private val inner: Transcoding) : Transcoding by inner {
|
||||
override val tag = "_$revision${inner.tag}"
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
package org.oxycblt.auxio.image.covers
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
import org.oxycblt.auxio.image.CoverMode
|
||||
@@ -28,16 +29,21 @@ import org.oxycblt.musikr.covers.Covers
|
||||
import org.oxycblt.musikr.covers.FDCover
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.covers.embedded.CoverIdentifier
|
||||
import org.oxycblt.musikr.covers.embedded.CoverParams
|
||||
import org.oxycblt.musikr.covers.embedded.EmbeddedCovers
|
||||
import org.oxycblt.musikr.covers.fs.FSCovers
|
||||
import org.oxycblt.musikr.covers.fs.MutableFSCovers
|
||||
import org.oxycblt.musikr.covers.stored.Compress
|
||||
import org.oxycblt.musikr.covers.stored.CoverStorage
|
||||
import org.oxycblt.musikr.covers.stored.MutableStoredCovers
|
||||
import org.oxycblt.musikr.covers.stored.NoTranscoding
|
||||
import org.oxycblt.musikr.covers.stored.StoredCovers
|
||||
|
||||
interface SettingCovers {
|
||||
suspend fun mutate(context: Context, revision: UUID): MutableCovers<out Cover>
|
||||
|
||||
companion object {
|
||||
fun immutable(context: Context): Covers<FDCover> =
|
||||
Covers.chain(BaseSiloedCovers(context), FSCovers(context))
|
||||
suspend fun immutable(context: Context): Covers<FDCover> =
|
||||
Covers.chain(StoredCovers(CoverStorage.at(context.coversDir())), FSCovers(context))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,17 +51,22 @@ class SettingCoversImpl
|
||||
@Inject
|
||||
constructor(private val imageSettings: ImageSettings, private val identifier: CoverIdentifier) :
|
||||
SettingCovers {
|
||||
override suspend fun mutate(context: Context, revision: UUID): MutableCovers<out Cover> =
|
||||
when (imageSettings.coverMode) {
|
||||
CoverMode.OFF -> NullCovers(context)
|
||||
CoverMode.SAVE_SPACE -> siloedCovers(context, revision, CoverParams.of(500, 70))
|
||||
CoverMode.BALANCED -> siloedCovers(context, revision, CoverParams.of(750, 85))
|
||||
CoverMode.HIGH_QUALITY -> siloedCovers(context, revision, CoverParams.of(1000, 100))
|
||||
CoverMode.AS_IS -> siloedCovers(context, revision, null)
|
||||
}
|
||||
|
||||
private suspend fun siloedCovers(context: Context, revision: UUID, with: CoverParams?) =
|
||||
MutableCovers.chain(
|
||||
MutableSiloedCovers.from(context, CoverSilo(revision, with), identifier),
|
||||
MutableFSCovers(context))
|
||||
override suspend fun mutate(context: Context, revision: UUID): MutableCovers<out Cover> {
|
||||
val coverStorage = CoverStorage.at(context.coversDir())
|
||||
val transcoding =
|
||||
when (imageSettings.coverMode) {
|
||||
CoverMode.OFF -> return NullCovers(coverStorage)
|
||||
CoverMode.SAVE_SPACE -> Compress(Bitmap.CompressFormat.JPEG, 500, 70)
|
||||
CoverMode.BALANCED -> Compress(Bitmap.CompressFormat.JPEG, 750, 85)
|
||||
CoverMode.HIGH_QUALITY -> Compress(Bitmap.CompressFormat.JPEG, 1000, 100)
|
||||
CoverMode.AS_IS -> NoTranscoding
|
||||
}
|
||||
val revisionedTranscoding = RevisionedTranscoding(revision, transcoding)
|
||||
val storedCovers =
|
||||
MutableStoredCovers(EmbeddedCovers(identifier), coverStorage, revisionedTranscoding)
|
||||
val fsCovers = MutableFSCovers(context)
|
||||
return MutableCovers.chain(storedCovers, fsCovers)
|
||||
}
|
||||
}
|
||||
|
||||
private fun Context.coversDir() = filesDir.resolve("covers")
|
||||
|
||||
@@ -1,137 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Auxio Project
|
||||
* SiloedCovers.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.auxio.image.covers
|
||||
|
||||
import android.content.Context
|
||||
import java.io.File
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.covers.CoverResult
|
||||
import org.oxycblt.musikr.covers.Covers
|
||||
import org.oxycblt.musikr.covers.FDCover
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.covers.embedded.CoverFormat
|
||||
import org.oxycblt.musikr.covers.embedded.CoverIdentifier
|
||||
import org.oxycblt.musikr.covers.embedded.EmbeddedCovers
|
||||
import org.oxycblt.musikr.covers.embedded.MutableEmbeddedCovers
|
||||
import org.oxycblt.musikr.fs.app.AppFS
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.metadata.Metadata
|
||||
|
||||
class BaseSiloedCovers(private val context: Context) : Covers<FDCover> {
|
||||
override suspend fun obtain(id: String): CoverResult<FDCover> {
|
||||
val siloedId = SiloedCoverId.parse(id) ?: return CoverResult.Miss()
|
||||
val core = SiloCore.from(context, siloedId.silo)
|
||||
val embeddedCovers = EmbeddedCovers(core.files, core.format)
|
||||
return when (val result = embeddedCovers.obtain(siloedId.id)) {
|
||||
is CoverResult.Hit -> CoverResult.Hit(SiloedCover(siloedId.silo, result.cover))
|
||||
is CoverResult.Miss -> CoverResult.Miss()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
open class SiloedCovers(private val silo: CoverSilo, private val embeddedCovers: EmbeddedCovers) :
|
||||
Covers<FDCover> {
|
||||
override suspend fun obtain(id: String): CoverResult<FDCover> {
|
||||
val coverId = SiloedCoverId.parse(id) ?: return CoverResult.Miss()
|
||||
if (silo != coverId.silo) return CoverResult.Miss()
|
||||
return when (val result = embeddedCovers.obtain(coverId.id)) {
|
||||
is CoverResult.Hit -> CoverResult.Hit(SiloedCover(silo, result.cover))
|
||||
is CoverResult.Miss -> CoverResult.Miss()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
suspend fun from(context: Context, silo: CoverSilo): SiloedCovers {
|
||||
val core = SiloCore.from(context, silo)
|
||||
return SiloedCovers(silo, EmbeddedCovers(core.files, core.format))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MutableSiloedCovers
|
||||
private constructor(
|
||||
private val rootDir: File,
|
||||
private val silo: CoverSilo,
|
||||
private val fileCovers: MutableEmbeddedCovers
|
||||
) : SiloedCovers(silo, fileCovers), MutableCovers<FDCover> {
|
||||
override suspend fun create(file: DeviceFile, metadata: Metadata): CoverResult<FDCover> =
|
||||
when (val result = fileCovers.create(file, metadata)) {
|
||||
is CoverResult.Hit -> CoverResult.Hit(SiloedCover(silo, result.cover))
|
||||
is CoverResult.Miss -> CoverResult.Miss()
|
||||
}
|
||||
|
||||
override suspend fun cleanup(excluding: Collection<Cover>) {
|
||||
fileCovers.cleanup(excluding.filterIsInstance<SiloedCover>().map { it.innerCover })
|
||||
|
||||
// Destroy old revisions no longer being used.
|
||||
withContext(Dispatchers.IO) {
|
||||
val exclude = silo.toString()
|
||||
rootDir.listFiles { file -> file.name != exclude }?.forEach { it.deleteRecursively() }
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
suspend fun from(
|
||||
context: Context,
|
||||
silo: CoverSilo,
|
||||
coverIdentifier: CoverIdentifier
|
||||
): MutableSiloedCovers {
|
||||
val core = SiloCore.from(context, silo)
|
||||
return MutableSiloedCovers(
|
||||
core.rootDir, silo, MutableEmbeddedCovers(core.files, core.format, coverIdentifier))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class SiloedCover(private val silo: CoverSilo, val innerCover: FDCover) :
|
||||
FDCover by innerCover {
|
||||
private val innerId = SiloedCoverId(silo, innerCover.id)
|
||||
override val id = innerId.toString()
|
||||
}
|
||||
|
||||
data class SiloedCoverId(val silo: CoverSilo, val id: String) {
|
||||
override fun toString() = "$id@$silo"
|
||||
|
||||
companion object {
|
||||
fun parse(id: String): SiloedCoverId? {
|
||||
val parts = id.split('@')
|
||||
if (parts.size != 2) return null
|
||||
val silo = CoverSilo.parse(parts[1]) ?: return null
|
||||
return SiloedCoverId(silo, parts[0])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private data class SiloCore(val rootDir: File, val files: AppFS, val format: CoverFormat) {
|
||||
companion object {
|
||||
suspend fun from(context: Context, silo: CoverSilo): SiloCore {
|
||||
val rootDir: File
|
||||
val revisionDir: File
|
||||
withContext(Dispatchers.IO) {
|
||||
rootDir = context.coversDir()
|
||||
revisionDir = rootDir.resolve(silo.toString()).apply { mkdirs() }
|
||||
}
|
||||
val files = AppFS.at(revisionDir)
|
||||
val format = silo.params?.let(CoverFormat::jpeg) ?: CoverFormat.asIs()
|
||||
return SiloCore(rootDir, files, format)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user