fix: accept POST form posts on every Subsonic endpoint
Clients that use the formPost extension (Feishin, Symfonium) send requests as application/x-www-form-urlencoded POSTs, not only GET. Register both methods for every route and cover it in the smoke test.
This commit is contained in:
1 parent
4d313a2846
commit
1571ec65ff
9 files changed
+168
-113
No files matched your search
@@ -20,7 +20,7 @@ import win.taptap.musicmanager.subsonic.respondSubsonicError
|
||||
|
||||
fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
|
||||
subsonicGet(users, "/getMusicFolders") { call, user ->
|
||||
subsonicEndpoint(users, "/getMusicFolders") { call, user ->
|
||||
val visible = user.visibleFolderIds(library)
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
@@ -41,7 +41,7 @@ fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
)
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getIndexes") { call, user ->
|
||||
subsonicEndpoint(users, "/getIndexes") { call, user ->
|
||||
val artists = LibraryRepository.artists(user.visibleFolderIds(library), user.name)
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
@@ -54,7 +54,7 @@ fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
)
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getArtists") { call, user ->
|
||||
subsonicEndpoint(users, "/getArtists") { call, user ->
|
||||
val artists = LibraryRepository.artists(user.visibleFolderIds(library), user.name)
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
@@ -66,64 +66,64 @@ fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
)
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getArtist") { call, user ->
|
||||
subsonicEndpoint(users, "/getArtist") { call, user ->
|
||||
val id = call.subsonicParams()["id"]
|
||||
if (id.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val folders = user.visibleFolderIds(library)
|
||||
val artist = LibraryRepository.artist(id, folders, user.name)
|
||||
if (artist == null) {
|
||||
call.respondSubsonicError(70, "Artist not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val albums = LibraryRepository.albumsForArtist(id, folders, user.name)
|
||||
call.respondSubsonic(buildJsonObject { put("artist", artistJson(artist, albums)) })
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getAlbum") { call, user ->
|
||||
subsonicEndpoint(users, "/getAlbum") { call, user ->
|
||||
val id = call.subsonicParams()["id"]
|
||||
if (id.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val folders = user.visibleFolderIds(library)
|
||||
val album = LibraryRepository.album(id, folders, user.name)
|
||||
if (album == null) {
|
||||
call.respondSubsonicError(70, "Album not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val songs = LibraryRepository.albumSongs(id, folders, user.name)
|
||||
call.respondSubsonic(buildJsonObject { put("album", albumJson(album, songs)) })
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getSong") { call, user ->
|
||||
subsonicEndpoint(users, "/getSong") { call, user ->
|
||||
val id = call.subsonicParams()["id"]
|
||||
if (id.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val song = LibraryRepository.song(id, user.visibleFolderIds(library), user.name)
|
||||
if (song == null) {
|
||||
call.respondSubsonicError(70, "Song not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
call.respondSubsonic(buildJsonObject { put("song", songJson(song)) })
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getMusicDirectory") { call, user ->
|
||||
subsonicEndpoint(users, "/getMusicDirectory") { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val id = parameters["id"]
|
||||
if (id.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val folders = user.effectiveFolderIds(library, parameters["musicFolderId"])
|
||||
@@ -131,7 +131,7 @@ fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
val album = LibraryRepository.album(id, folders, user.name)
|
||||
if (album == null) {
|
||||
call.respondSubsonicError(70, "Album not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val songs = LibraryRepository.albumSongs(id, folders, user.name)
|
||||
@@ -146,18 +146,18 @@ fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
})
|
||||
},
|
||||
)
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val folderId = id.toIntOrNull()
|
||||
if (folderId == null || folderId !in folders) {
|
||||
call.respondSubsonicError(70, "Directory not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val folder = library.byId(folderId) ?: run {
|
||||
call.respondSubsonicError(70, "Directory not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
val albums = LibraryRepository.albumList(
|
||||
type = "alphabeticalByName",
|
||||
@@ -206,12 +206,12 @@ fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
)
|
||||
|
||||
for (endpoint in listOf("/getAlbumList2", "/getAlbumList")) {
|
||||
subsonicGet(users, endpoint) { call, user ->
|
||||
subsonicEndpoint(users, endpoint) { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val type = parameters["type"] ?: "newest"
|
||||
if (type !in albumListTypes) {
|
||||
call.respondSubsonicError(10, "Invalid album list type: $type")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val size = parameters.intParam("size", 10)
|
||||
@@ -242,7 +242,7 @@ fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
}
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getRandomSongs") { call, user ->
|
||||
subsonicEndpoint(users, "/getRandomSongs") { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val songs = LibraryRepository.randomSongs(
|
||||
size = parameters.intParam("size", 10),
|
||||
@@ -262,7 +262,7 @@ fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
)
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getGenres") { call, user ->
|
||||
subsonicEndpoint(users, "/getGenres") { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val genres = LibraryRepository.genres(
|
||||
user.effectiveFolderIds(library, parameters["musicFolderId"]),
|
||||
@@ -277,12 +277,12 @@ fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
)
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getSongsByGenre") { call, user ->
|
||||
subsonicEndpoint(users, "/getSongsByGenre") { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val genre = parameters["genre"]
|
||||
if (genre.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: genre")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val songs = LibraryRepository.songsByGenre(
|
||||
@@ -303,7 +303,7 @@ fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
}
|
||||
|
||||
for (endpoint in listOf("/getStarred2", "/getStarred")) {
|
||||
subsonicGet(users, endpoint) { call, user ->
|
||||
subsonicEndpoint(users, endpoint) { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val starred = LibraryRepository.starred(
|
||||
userName = user.name,
|
||||
@@ -325,7 +325,7 @@ fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
|
||||
// No external metadata provider yet, but clients expect a valid empty response.
|
||||
for (endpoint in listOf("/getArtistInfo2", "/getArtistInfo")) {
|
||||
subsonicGet(users, endpoint) { call, _ ->
|
||||
subsonicEndpoint(users, endpoint) { call, _ ->
|
||||
val key = if (endpoint == "/getArtistInfo2") "artistInfo2" else "artistInfo"
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
@@ -339,14 +339,14 @@ fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
}
|
||||
|
||||
for (endpoint in listOf("/getAlbumInfo2", "/getAlbumInfo")) {
|
||||
subsonicGet(users, endpoint) { call, _ ->
|
||||
subsonicEndpoint(users, endpoint) { call, _ ->
|
||||
val key = if (endpoint == "/getAlbumInfo2") "albumInfo2" else "albumInfo"
|
||||
call.respondSubsonic(buildJsonObject { put(key, buildJsonObject { }) })
|
||||
}
|
||||
}
|
||||
|
||||
for (endpoint in listOf("/getSimilarSongs2", "/getSimilarSongs")) {
|
||||
subsonicGet(users, endpoint) { call, _ ->
|
||||
subsonicEndpoint(users, endpoint) { call, _ ->
|
||||
val key = if (endpoint == "/getSimilarSongs2") "similarSongs2" else "similarSongs"
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
@@ -356,7 +356,7 @@ fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
}
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getTopSongs") { call, _ ->
|
||||
subsonicEndpoint(users, "/getTopSongs") { call, _ ->
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
put("topSongs", buildJsonObject { put("song", buildJsonArray { }) })
|
||||
@@ -364,7 +364,7 @@ fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
)
|
||||
}
|
||||
|
||||
subsonicGet(users, "/search3") { call, user ->
|
||||
subsonicEndpoint(users, "/search3") { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val query = parameters["query"].orEmpty()
|
||||
val folders = user.effectiveFolderIds(library, parameters["musicFolderId"])
|
||||
|
||||
@@ -16,12 +16,12 @@ import win.taptap.musicmanager.subsonic.respondSubsonicError
|
||||
/** Per-user interaction: scrobbling, stars, ratings, bookmarks and lyrics. */
|
||||
fun Route.interactionRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
|
||||
subsonicGet(users, "/scrobble") { call, user ->
|
||||
subsonicEndpoint(users, "/scrobble") { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val ids = parameters.getAll("id").orEmpty().filter { it.isNotBlank() }
|
||||
if (ids.isEmpty()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val submission = parameters["submission"]?.toBooleanStrictOrNull() ?: true
|
||||
@@ -30,7 +30,7 @@ fun Route.interactionRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
for (id in ids) {
|
||||
if (LibraryRepository.song(id, folders, user.name) == null) {
|
||||
call.respondSubsonicError(70, "Song not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
UserStateRepository.scrobble(user.name, id, submission)
|
||||
}
|
||||
@@ -38,7 +38,7 @@ fun Route.interactionRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
call.respondSubsonic()
|
||||
}
|
||||
|
||||
subsonicGet(users, "/star") { call, user ->
|
||||
subsonicEndpoint(users, "/star") { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val folders = user.effectiveFolderIds(library, parameters["musicFolderId"])
|
||||
|
||||
@@ -48,12 +48,12 @@ fun Route.interactionRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
|
||||
if (songIds.isEmpty() && albumIds.isEmpty() && artistIds.isEmpty()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id, albumId or artistId")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
if (!validateVisible(songIds, albumIds, artistIds, folders, user.name)) {
|
||||
call.respondSubsonicError(70, "Item not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
songIds.forEach { UserStateRepository.starSong(user.name, it) }
|
||||
@@ -63,7 +63,7 @@ fun Route.interactionRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
call.respondSubsonic()
|
||||
}
|
||||
|
||||
subsonicGet(users, "/unstar") { call, user ->
|
||||
subsonicEndpoint(users, "/unstar") { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val folders = user.effectiveFolderIds(library, parameters["musicFolderId"])
|
||||
|
||||
@@ -73,12 +73,12 @@ fun Route.interactionRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
|
||||
if (songIds.isEmpty() && albumIds.isEmpty() && artistIds.isEmpty()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id, albumId or artistId")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
if (!validateVisible(songIds, albumIds, artistIds, folders, user.name)) {
|
||||
call.respondSubsonicError(70, "Item not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
songIds.forEach { UserStateRepository.unstarSong(user.name, it) }
|
||||
@@ -88,19 +88,19 @@ fun Route.interactionRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
call.respondSubsonic()
|
||||
}
|
||||
|
||||
subsonicGet(users, "/setRating") { call, user ->
|
||||
subsonicEndpoint(users, "/setRating") { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val id = parameters["id"]
|
||||
if (id.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val rawRating = parameters["rating"]
|
||||
val rating = rawRating?.toIntOrNull()
|
||||
if (rating == null || rating !in 0..5) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing or invalid: rating")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val folders = user.effectiveFolderIds(library, parameters["musicFolderId"])
|
||||
@@ -110,34 +110,34 @@ fun Route.interactionRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
id.startsWith("sg-") -> {
|
||||
if (LibraryRepository.song(id, folders, user.name) == null) {
|
||||
call.respondSubsonicError(70, "Song not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
UserStateRepository.setSongRating(user.name, id, storedRating)
|
||||
}
|
||||
id.startsWith("al-") -> {
|
||||
if (LibraryRepository.album(id, folders, user.name) == null) {
|
||||
call.respondSubsonicError(70, "Album not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
UserStateRepository.setItemRating(user.name, "album", id, storedRating)
|
||||
}
|
||||
id.startsWith("ar-") -> {
|
||||
if (LibraryRepository.artist(id, folders, user.name) == null) {
|
||||
call.respondSubsonicError(70, "Artist not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
UserStateRepository.setItemRating(user.name, "artist", id, storedRating)
|
||||
}
|
||||
else -> {
|
||||
call.respondSubsonicError(70, "Item not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
}
|
||||
|
||||
call.respondSubsonic()
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getBookmarks") { call, user ->
|
||||
subsonicEndpoint(users, "/getBookmarks") { call, user ->
|
||||
val folders = user.effectiveFolderIds(library, call.subsonicParams()["musicFolderId"])
|
||||
|
||||
val bookmarks = UserStateRepository.bookmarks(user.name).mapNotNull { bookmark ->
|
||||
@@ -160,53 +160,53 @@ fun Route.interactionRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
)
|
||||
}
|
||||
|
||||
subsonicGet(users, "/createBookmark") { call, user ->
|
||||
subsonicEndpoint(users, "/createBookmark") { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val id = parameters["id"]
|
||||
if (id.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val position = parameters["position"]?.toLongOrNull()
|
||||
if (position == null || position < 0) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing or invalid: position")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
if (LibraryRepository.song(id, user.effectiveFolderIds(library, parameters["musicFolderId"]), user.name) == null) {
|
||||
call.respondSubsonicError(70, "Song not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
UserStateRepository.createBookmark(user.name, id, position)
|
||||
call.respondSubsonic()
|
||||
}
|
||||
|
||||
subsonicGet(users, "/deleteBookmark") { call, user ->
|
||||
subsonicEndpoint(users, "/deleteBookmark") { call, user ->
|
||||
val id = call.subsonicParams()["id"]
|
||||
if (id.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
UserStateRepository.deleteBookmark(user.name, id)
|
||||
call.respondSubsonic()
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getLyricsBySongId") { call, user ->
|
||||
subsonicEndpoint(users, "/getLyricsBySongId") { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val id = parameters["id"]
|
||||
if (id.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val folders = user.effectiveFolderIds(library, parameters["musicFolderId"])
|
||||
val song = LibraryRepository.song(id, folders, user.name)
|
||||
if (song == null) {
|
||||
call.respondSubsonicError(70, "Song not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val lyrics = LibraryRepository.lyrics(id, folders)
|
||||
|
||||
@@ -18,11 +18,11 @@ fun Route.mediaRoutes(
|
||||
coverArtService: CoverArtService,
|
||||
) {
|
||||
|
||||
subsonicGet(users, "/stream") { call, user ->
|
||||
subsonicEndpoint(users, "/stream") { call, user ->
|
||||
val id = call.subsonicParams()["id"]
|
||||
if (id.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val resolved = LibraryRepository.resolveSong(
|
||||
@@ -33,17 +33,17 @@ fun Route.mediaRoutes(
|
||||
)
|
||||
if (resolved == null) {
|
||||
call.respondSubsonicError(70, "Song not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
call.respondFile(resolved.file.toFile())
|
||||
}
|
||||
|
||||
subsonicGet(users, "/download") { call, user ->
|
||||
subsonicEndpoint(users, "/download") { call, user ->
|
||||
val id = call.subsonicParams()["id"]
|
||||
if (id.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val resolved = LibraryRepository.resolveSong(
|
||||
@@ -54,7 +54,7 @@ fun Route.mediaRoutes(
|
||||
)
|
||||
if (resolved == null) {
|
||||
call.respondSubsonicError(70, "Song not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
call.response.headers.append(
|
||||
@@ -64,19 +64,19 @@ fun Route.mediaRoutes(
|
||||
call.respondFile(resolved.file.toFile())
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getCoverArt") { call, user ->
|
||||
subsonicEndpoint(users, "/getCoverArt") { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val id = parameters["id"]
|
||||
if (id.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val requestedSize = parameters["size"]
|
||||
val size = requestedSize?.toIntOrNull()
|
||||
if (requestedSize != null && (size == null || size !in 1..2048)) {
|
||||
call.respondSubsonicError(10, "Invalid cover size")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val folders = user.visibleFolderIds(library)
|
||||
@@ -90,7 +90,7 @@ fun Route.mediaRoutes(
|
||||
|
||||
if (albumId == null) {
|
||||
call.respond(HttpStatusCode.NotFound)
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val cover = if (size == null) {
|
||||
|
||||
@@ -16,7 +16,7 @@ import win.taptap.musicmanager.subsonic.respondSubsonic
|
||||
fun Route.playQueueRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
|
||||
for (endpoint in listOf("/savePlayQueue", "/savePlayQueueByIndex")) {
|
||||
subsonicGet(users, endpoint) { call, user ->
|
||||
subsonicEndpoint(users, endpoint) { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val folders = user.visibleFolderIds(library)
|
||||
|
||||
@@ -37,7 +37,7 @@ fun Route.playQueueRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
}
|
||||
|
||||
for (endpoint in listOf("/getPlayQueue", "/getPlayQueueByIndex")) {
|
||||
subsonicGet(users, endpoint) { call, user ->
|
||||
subsonicEndpoint(users, endpoint) { call, user ->
|
||||
val state = PlayQueueStore.get(user.name)
|
||||
val folders = user.visibleFolderIds(library)
|
||||
val songs = LibraryRepository.songsByIds(state?.songIds.orEmpty(), folders, user.name)
|
||||
|
||||
@@ -16,7 +16,7 @@ import win.taptap.musicmanager.subsonic.respondSubsonicError
|
||||
/** Playlist CRUD. Visibility: owned playlists plus all public playlists. */
|
||||
fun Route.playlistRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
|
||||
subsonicGet(users, "/getPlaylists") { call, user ->
|
||||
subsonicEndpoint(users, "/getPlaylists") { call, user ->
|
||||
val playlists = PlaylistRepository.visibleTo(user.name)
|
||||
|
||||
call.respondSubsonic(
|
||||
@@ -28,17 +28,17 @@ fun Route.playlistRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
)
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getPlaylist") { call, user ->
|
||||
subsonicEndpoint(users, "/getPlaylist") { call, user ->
|
||||
val id = call.subsonicParams()["id"]
|
||||
if (id.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val playlist = PlaylistRepository.get(id, user.name)
|
||||
if (playlist == null) {
|
||||
call.respondSubsonicError(70, "Playlist not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val folders = user.visibleFolderIds(library)
|
||||
@@ -47,7 +47,7 @@ fun Route.playlistRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
call.respondSubsonic(buildJsonObject { put("playlist", playlistJson(playlist, songs)) })
|
||||
}
|
||||
|
||||
subsonicGet(users, "/createPlaylist") { call, user ->
|
||||
subsonicEndpoint(users, "/createPlaylist") { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val folders = user.visibleFolderIds(library)
|
||||
val requestedSongs = parameters.getAll("songId").orEmpty()
|
||||
@@ -59,11 +59,11 @@ fun Route.playlistRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
val existing = PlaylistRepository.get(playlistId, user.name)
|
||||
if (existing == null) {
|
||||
call.respondSubsonicError(70, "Playlist not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
if (existing.owner != user.name) {
|
||||
call.respondSubsonicError(50, "Not authorized")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val newName = parameters["name"]?.takeIf { it.isNotBlank() }
|
||||
@@ -80,35 +80,35 @@ fun Route.playlistRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
|
||||
val updated = PlaylistRepository.get(playlistId, user.name)!!
|
||||
call.respondSubsonic(buildJsonObject { put("playlist", playlistJson(updated)) })
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val name = parameters["name"]
|
||||
if (name.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: name")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val playlist = PlaylistRepository.create(user.name, name.trim(), requestedSongs)
|
||||
call.respondSubsonic(buildJsonObject { put("playlist", playlistJson(playlist)) })
|
||||
}
|
||||
|
||||
subsonicGet(users, "/updatePlaylist") { call, user ->
|
||||
subsonicEndpoint(users, "/updatePlaylist") { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val playlistId = parameters["playlistId"]
|
||||
if (playlistId.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: playlistId")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val existing = PlaylistRepository.get(playlistId, user.name)
|
||||
if (existing == null) {
|
||||
call.respondSubsonicError(70, "Playlist not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
if (existing.owner != user.name) {
|
||||
call.respondSubsonicError(50, "Not authorized")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val folders = user.visibleFolderIds(library)
|
||||
@@ -136,21 +136,21 @@ fun Route.playlistRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
}
|
||||
}
|
||||
|
||||
subsonicGet(users, "/deletePlaylist") { call, user ->
|
||||
subsonicEndpoint(users, "/deletePlaylist") { call, user ->
|
||||
val playlistId = call.subsonicParams()["id"]
|
||||
if (playlistId.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val existing = PlaylistRepository.get(playlistId, user.name)
|
||||
if (existing == null) {
|
||||
call.respondSubsonicError(70, "Playlist not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
if (existing.owner != user.name) {
|
||||
call.respondSubsonicError(50, "Not authorized")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
PlaylistRepository.delete(playlistId, user.name)
|
||||
|
||||
+18
-13
@@ -3,16 +3,18 @@ package win.taptap.musicmanager.subsonic.routes
|
||||
import io.ktor.server.application.ApplicationCall
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.get
|
||||
import io.ktor.server.routing.post
|
||||
import win.taptap.musicmanager.config.UserAccount
|
||||
import win.taptap.musicmanager.config.UsersConfig
|
||||
import win.taptap.musicmanager.subsonic.SubsonicAuthenticator
|
||||
import win.taptap.musicmanager.subsonic.respondSubsonicError
|
||||
|
||||
/**
|
||||
* Registers a Subsonic GET endpoint for both `/{path}` and `/{path}.view`.
|
||||
* Registers a Subsonic endpoint for both `/{path}` and `/{path}.view`, and for both
|
||||
* GET and POST form posts (the `formPost` OpenSubsonic extension).
|
||||
* Authentication failures always return HTTP 200 with error code 40.
|
||||
*/
|
||||
internal fun Route.subsonicGet(
|
||||
internal fun Route.subsonicEndpoint(
|
||||
users: UsersConfig,
|
||||
path: String,
|
||||
handler: suspend (ApplicationCall, UserAccount) -> Unit,
|
||||
@@ -20,17 +22,20 @@ internal fun Route.subsonicGet(
|
||||
val authenticator = SubsonicAuthenticator(users)
|
||||
val paths = if (path.endsWith(".view")) listOf(path) else listOf(path, "$path.view")
|
||||
|
||||
paths.forEach { routePath ->
|
||||
get(routePath) {
|
||||
val parameters = subsonicParameters(call)
|
||||
val user = authenticator.authenticate(parameters)
|
||||
if (user == null) {
|
||||
call.respondSubsonicError(40, "Wrong username or password")
|
||||
} else {
|
||||
call.attributes.put(CurrentUserKey, user)
|
||||
call.attributes.put(SubsonicParametersKey, parameters)
|
||||
handler(call, user)
|
||||
}
|
||||
suspend fun handle(call: ApplicationCall) {
|
||||
val parameters = subsonicParameters(call)
|
||||
val user = authenticator.authenticate(parameters)
|
||||
if (user == null) {
|
||||
call.respondSubsonicError(40, "Wrong username or password")
|
||||
} else {
|
||||
call.attributes.put(CurrentUserKey, user)
|
||||
call.attributes.put(SubsonicParametersKey, parameters)
|
||||
handler(call, user)
|
||||
}
|
||||
}
|
||||
|
||||
paths.forEach { routePath ->
|
||||
get(routePath) { handle(call) }
|
||||
post(routePath) { handle(call) }
|
||||
}
|
||||
}
|
||||
@@ -21,11 +21,11 @@ fun Route.systemRoutes(
|
||||
startScan: () -> Boolean,
|
||||
scanStatus: () -> ScanStatus,
|
||||
) {
|
||||
subsonicGet(users, "/ping") { call, _ ->
|
||||
subsonicEndpoint(users, "/ping") { call, _ ->
|
||||
call.respondSubsonic()
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getLicense") { call, _ ->
|
||||
subsonicEndpoint(users, "/getLicense") { call, _ ->
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
put("license", buildJsonObject { put("valid", true) })
|
||||
@@ -33,7 +33,7 @@ fun Route.systemRoutes(
|
||||
)
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getOpenSubsonicExtensions") { call, _ ->
|
||||
subsonicEndpoint(users, "/getOpenSubsonicExtensions") { call, _ ->
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
put(
|
||||
@@ -48,7 +48,7 @@ fun Route.systemRoutes(
|
||||
)
|
||||
}
|
||||
|
||||
subsonicGet(users, "/startScan") { call, user ->
|
||||
subsonicEndpoint(users, "/startScan") { call, user ->
|
||||
if (!user.admin) {
|
||||
call.respondSubsonicError(50, "Not authorized")
|
||||
} else {
|
||||
@@ -57,7 +57,7 @@ fun Route.systemRoutes(
|
||||
}
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getScanStatus") { call, _ ->
|
||||
subsonicEndpoint(users, "/getScanStatus") { call, _ ->
|
||||
val status = scanStatus()
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
@@ -69,28 +69,28 @@ fun Route.systemRoutes(
|
||||
)
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getUser") { call, currentUser ->
|
||||
subsonicEndpoint(users, "/getUser") { call, currentUser ->
|
||||
val username = call.subsonicParams()["username"]
|
||||
if (username.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: username")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
if (username != currentUser.name && !currentUser.admin) {
|
||||
call.respondSubsonicError(50, "Not authorized")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
val requested = users.byName(username)
|
||||
if (requested == null) {
|
||||
call.respondSubsonicError(70, "User not found")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
call.respondSubsonic(buildJsonObject { put("user", userJson(requested, library)) })
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getInternetRadioStations") { call, _ ->
|
||||
subsonicEndpoint(users, "/getInternetRadioStations") { call, _ ->
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
put("internetRadioStations", buildJsonObject {
|
||||
@@ -105,15 +105,15 @@ fun Route.systemRoutes(
|
||||
"/updateInternetRadioStation",
|
||||
"/deleteInternetRadioStation",
|
||||
)) {
|
||||
subsonicGet(users, endpoint) { call, _ ->
|
||||
subsonicEndpoint(users, endpoint) { call, _ ->
|
||||
call.respondSubsonicError(0, "Internet radio is not supported")
|
||||
}
|
||||
}
|
||||
|
||||
subsonicGet(users, "/jukeboxControl") { call, _ ->
|
||||
subsonicEndpoint(users, "/jukeboxControl") { call, _ ->
|
||||
if (call.subsonicParams()["action"] != "status") {
|
||||
call.respondSubsonicError(0, "Jukebox is not supported")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
call.respondSubsonic(
|
||||
@@ -128,10 +128,10 @@ fun Route.systemRoutes(
|
||||
)
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getUsers") { call, currentUser ->
|
||||
subsonicEndpoint(users, "/getUsers") { call, currentUser ->
|
||||
if (!currentUser.admin) {
|
||||
call.respondSubsonicError(50, "Not authorized")
|
||||
return@subsonicGet
|
||||
return@subsonicEndpoint
|
||||
}
|
||||
|
||||
call.respondSubsonic(
|
||||
|
||||
Reference in new issue
Block a user