musikr: normalize dot segments when resolving playlist entries
This commit is contained in:
@@ -50,7 +50,7 @@ internal class PlaylistPathResolver {
|
||||
var lockedRoot: Path? = null
|
||||
probe@ for (entry in entries) {
|
||||
for (root in roots) {
|
||||
if (exists(Path(root.volume, root.components.child(entry.components)))) {
|
||||
if (exists(Path(root.volume, entry.components.joinedTo(root.components)))) {
|
||||
lockedRoot = root
|
||||
break@probe
|
||||
}
|
||||
@@ -60,7 +60,22 @@ internal class PlaylistPathResolver {
|
||||
return null
|
||||
}
|
||||
return entries.mapNotNull { entry ->
|
||||
Path(lockedRoot.volume, lockedRoot.components.child(entry.components)).takeIf(exists)
|
||||
Path(lockedRoot.volume, entry.components.joinedTo(lockedRoot.components))
|
||||
.takeIf(exists)
|
||||
}
|
||||
}
|
||||
|
||||
/** Join these components onto [base], resolving `.` and `..` segments along the way. */
|
||||
private fun Components.joinedTo(base: Components): Components {
|
||||
var result = base
|
||||
for (component in components) {
|
||||
result =
|
||||
when (component) {
|
||||
".." -> result.parent()
|
||||
"." -> result
|
||||
else -> result.child(component)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +60,40 @@ class PlaylistPathResolverTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resolve_dotPrefixedEntries_resolveLikeBareOnes() {
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user