musikr: introduce folder covers
Like cover.png, cover.jpg, etc.
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Auxio Project
|
||||
* CompatCovers.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 android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.ParcelFileDescriptor
|
||||
import android.provider.MediaStore
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.oxycblt.musikr.cover.Cover
|
||||
import org.oxycblt.musikr.cover.CoverResult
|
||||
import org.oxycblt.musikr.cover.Covers
|
||||
import org.oxycblt.musikr.cover.FileCover
|
||||
import org.oxycblt.musikr.cover.MutableCovers
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.metadata.Metadata
|
||||
|
||||
open class CompatCovers(private val context: Context, private val inner: Covers<FileCover>) :
|
||||
Covers<FileCover> {
|
||||
override suspend fun obtain(id: String): CoverResult<FileCover> {
|
||||
when (val innerResult = inner.obtain(id)) {
|
||||
is CoverResult.Hit -> return CoverResult.Hit(innerResult.cover)
|
||||
is CoverResult.Miss -> {
|
||||
if (!id.startsWith("compat:")) return CoverResult.Miss()
|
||||
val uri = Uri.parse(id.substringAfter("compat:"))
|
||||
return CoverResult.Hit(CompatCover(context, uri))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MutableCompatCovers(
|
||||
private val context: Context,
|
||||
private val inner: MutableCovers<FileCover>
|
||||
) : CompatCovers(context, inner), MutableCovers<FileCover> {
|
||||
override suspend fun create(file: DeviceFile, metadata: Metadata): CoverResult<FileCover> {
|
||||
when (val innerResult = inner.create(file, metadata)) {
|
||||
is CoverResult.Hit -> return CoverResult.Hit(innerResult.cover)
|
||||
is CoverResult.Miss -> {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
||||
return CoverResult.Miss()
|
||||
}
|
||||
val mediaStoreUri =
|
||||
MediaStore.getMediaUri(context, file.uri) ?: return CoverResult.Miss()
|
||||
val proj = arrayOf(MediaStore.MediaColumns._ID)
|
||||
val cursor = context.contentResolver.query(mediaStoreUri, proj, null, null, null)
|
||||
val uri =
|
||||
cursor.use {
|
||||
if (it == null || !it.moveToFirst()) {
|
||||
return CoverResult.Miss()
|
||||
}
|
||||
val id = it.getLong(it.getColumnIndexOrThrow(MediaStore.MediaColumns._ID))
|
||||
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.buildUpon().run {
|
||||
appendPath(id.toString())
|
||||
appendPath("albumart")
|
||||
build()
|
||||
}
|
||||
}
|
||||
return CoverResult.Hit(CompatCover(context, uri))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun cleanup(excluding: Collection<Cover>) {}
|
||||
}
|
||||
|
||||
class CompatCover(private val context: Context, private val uri: Uri) : FileCover {
|
||||
override val id = "compat:$uri"
|
||||
|
||||
override suspend fun fd(): ParcelFileDescriptor? {
|
||||
return context.contentResolver.openFileDescriptor(uri, "r")
|
||||
}
|
||||
|
||||
override suspend fun open() =
|
||||
withContext(Dispatchers.IO) { context.contentResolver.openInputStream(uri) }
|
||||
}
|
||||
@@ -28,14 +28,16 @@ import org.oxycblt.musikr.cover.CoverIdentifier
|
||||
import org.oxycblt.musikr.cover.CoverParams
|
||||
import org.oxycblt.musikr.cover.Covers
|
||||
import org.oxycblt.musikr.cover.FileCover
|
||||
import org.oxycblt.musikr.cover.FolderCovers
|
||||
import org.oxycblt.musikr.cover.MutableCovers
|
||||
import org.oxycblt.musikr.cover.MutableFolderCovers
|
||||
|
||||
interface SettingCovers {
|
||||
suspend fun mutate(context: Context, revision: UUID): MutableCovers<out Cover>
|
||||
|
||||
companion object {
|
||||
fun immutable(context: Context): Covers<out FileCover> =
|
||||
CompatCovers(context, BaseSiloedCovers(context))
|
||||
fun immutable(context: Context): Covers<FileCover> =
|
||||
Covers.chain(BaseSiloedCovers(context), FolderCovers(context))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +54,7 @@ constructor(private val imageSettings: ImageSettings, private val identifier: Co
|
||||
}
|
||||
|
||||
private suspend fun siloedCovers(context: Context, revision: UUID, with: CoverParams) =
|
||||
MutableCompatCovers(
|
||||
context, MutableSiloedCovers.from(context, CoverSilo(revision, with), identifier))
|
||||
MutableCovers.chain(
|
||||
MutableSiloedCovers.from(context, CoverSilo(revision, with), identifier),
|
||||
MutableFolderCovers(context))
|
||||
}
|
||||
|
||||
@@ -31,8 +31,8 @@ import org.oxycblt.musikr.cover.FileCover
|
||||
import org.oxycblt.musikr.cover.FileCovers
|
||||
import org.oxycblt.musikr.cover.MutableCovers
|
||||
import org.oxycblt.musikr.cover.MutableFileCovers
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.fs.app.AppFiles
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.metadata.Metadata
|
||||
|
||||
class BaseSiloedCovers(private val context: Context) : Covers<FileCover> {
|
||||
|
||||
Reference in New Issue
Block a user