musikr: resolve playlist entries by probe-and-lock against candidate roots
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Auxio Project
|
||||
* PlaylistPathResolver.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.playlist
|
||||
|
||||
import org.oxycblt.musikr.fs.Components
|
||||
import org.oxycblt.musikr.fs.Path
|
||||
|
||||
/**
|
||||
* Resolves playlist file entries against the indexed library by probe-and-lock: probe entries
|
||||
* against the candidate roots in order until one resolves, lock that root, then resolve every
|
||||
* entry against it. All entries in a playlist are assumed to share one root.
|
||||
*
|
||||
* Unresolvable entries drop out of the result. A null result means no entry resolved anywhere —
|
||||
* the only case a playlist fails to resolve.
|
||||
*
|
||||
* @author OxygenCobalt (Fork)
|
||||
*/
|
||||
internal class PlaylistPathResolver {
|
||||
/**
|
||||
* A single playlist entry.
|
||||
*
|
||||
* @property components The path as written in the playlist, split into components.
|
||||
* @property absolute Whether the path was written absolute.
|
||||
*/
|
||||
data class Entry(val components: Components, val absolute: Boolean)
|
||||
|
||||
/**
|
||||
* Resolve [entries] against [roots] (in precedence order), using [exists] to test whether a
|
||||
* resolved path is in the library.
|
||||
*
|
||||
* @return The resolved paths in entry order, or null if no entry resolved under any root.
|
||||
*/
|
||||
fun resolve(entries: List<Entry>, roots: List<Path>, exists: (Path) -> Boolean): List<Path>? {
|
||||
var lockedRoot: Path? = null
|
||||
probe@ for (entry in entries) {
|
||||
for (root in roots) {
|
||||
if (exists(Path(root.volume, root.components.child(entry.components)))) {
|
||||
lockedRoot = root
|
||||
break@probe
|
||||
}
|
||||
}
|
||||
}
|
||||
if (lockedRoot == null) {
|
||||
return null
|
||||
}
|
||||
return entries.mapNotNull { entry ->
|
||||
Path(lockedRoot.volume, lockedRoot.components.child(entry.components)).takeIf(exists)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Auxio Project
|
||||
* PlaylistPathResolverTest.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.playlist
|
||||
|
||||
import android.content.Context
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.oxycblt.musikr.fs.Components
|
||||
import org.oxycblt.musikr.fs.Path
|
||||
import org.oxycblt.musikr.fs.Volume
|
||||
|
||||
class PlaylistPathResolverTest {
|
||||
@Test
|
||||
fun resolve_relativeEntries_locksMusicLocationRoot() {
|
||||
val volume = FakeVolume()
|
||||
val playlistDir = Path(volume, Components.parseUnix("Music/Playlists"))
|
||||
val musicRoot = Path(volume, Components.parseUnix("Music"))
|
||||
val library =
|
||||
setOf(
|
||||
Path(volume, Components.parseUnix("Music/Library/Artist/a.mp3")),
|
||||
Path(volume, Components.parseUnix("Music/Library/Artist/b.mp3")),
|
||||
)
|
||||
val entries =
|
||||
listOf(
|
||||
PlaylistPathResolver.Entry(
|
||||
Components.parseUnix("Library/Artist/a.mp3"), absolute = false
|
||||
),
|
||||
PlaylistPathResolver.Entry(
|
||||
Components.parseUnix("Library/Artist/b.mp3"), absolute = false
|
||||
),
|
||||
)
|
||||
|
||||
val resolved =
|
||||
PlaylistPathResolver().resolve(entries, listOf(playlistDir, musicRoot)) {
|
||||
it in library
|
||||
}
|
||||
|
||||
assertEquals(
|
||||
listOf(
|
||||
Path(volume, Components.parseUnix("Music/Library/Artist/a.mp3")),
|
||||
Path(volume, Components.parseUnix("Music/Library/Artist/b.mp3")),
|
||||
),
|
||||
resolved,
|
||||
)
|
||||
}
|
||||
|
||||
private class FakeVolume : Volume.External {
|
||||
override val mediaStoreName = "fake"
|
||||
override val id = "fake"
|
||||
override val components = Components.parseUnix("storage/emulated/0")
|
||||
|
||||
override fun resolveName(context: Context) = "fake"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user