feat: add browsing and media endpoints
- user-scoped getMusicFolders, getIndexes, getArtists, getArtist, getAlbum and getSong - search3 with pagination, literal matching and empty/wildcard queries - stream and download with Range support and RFC 5987 filenames - getCoverArt with cached size variants and visibility checks - V2 migration for per-user album and artist ratings
This commit is contained in:
1 parent
1f73d901d5
commit
643ee42f09
14 files changed
+1364
-4
No files matched your search
@@ -0,0 +1,117 @@
|
||||
package win.taptap.musicmanager.subsonic.routes
|
||||
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.server.response.respond
|
||||
import io.ktor.server.response.respondFile
|
||||
import io.ktor.server.routing.Route
|
||||
import java.net.URLEncoder
|
||||
import win.taptap.musicmanager.config.LibraryConfig
|
||||
import win.taptap.musicmanager.config.UsersConfig
|
||||
import win.taptap.musicmanager.db.LibraryRepository
|
||||
import win.taptap.musicmanager.scanner.CoverArtService
|
||||
import win.taptap.musicmanager.subsonic.respondSubsonicError
|
||||
|
||||
fun Route.mediaRoutes(
|
||||
users: UsersConfig,
|
||||
library: LibraryConfig,
|
||||
coverArtService: CoverArtService,
|
||||
) {
|
||||
|
||||
subsonicGet(users, "/stream") { call, user ->
|
||||
val id = call.subsonicParams()["id"]
|
||||
if (id.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
}
|
||||
|
||||
val resolved = LibraryRepository.resolveSong(
|
||||
id,
|
||||
user.visibleFolderIds(library),
|
||||
user.name,
|
||||
library,
|
||||
)
|
||||
if (resolved == null) {
|
||||
call.respondSubsonicError(70, "Song not found")
|
||||
return@subsonicGet
|
||||
}
|
||||
|
||||
call.respondFile(resolved.file.toFile())
|
||||
}
|
||||
|
||||
subsonicGet(users, "/download") { call, user ->
|
||||
val id = call.subsonicParams()["id"]
|
||||
if (id.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
}
|
||||
|
||||
val resolved = LibraryRepository.resolveSong(
|
||||
id,
|
||||
user.visibleFolderIds(library),
|
||||
user.name,
|
||||
library,
|
||||
)
|
||||
if (resolved == null) {
|
||||
call.respondSubsonicError(70, "Song not found")
|
||||
return@subsonicGet
|
||||
}
|
||||
|
||||
call.response.headers.append(
|
||||
HttpHeaders.ContentDisposition,
|
||||
contentDisposition(resolved.file.fileName.toString()),
|
||||
)
|
||||
call.respondFile(resolved.file.toFile())
|
||||
}
|
||||
|
||||
subsonicGet(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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
val folders = user.visibleFolderIds(library)
|
||||
val albumId = if (id.startsWith("sg-")) {
|
||||
LibraryRepository.albumIdForSong(id, folders)
|
||||
} else if (LibraryRepository.album(id, folders, user.name) != null) {
|
||||
id
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
if (albumId == null) {
|
||||
call.respond(HttpStatusCode.NotFound)
|
||||
return@subsonicGet
|
||||
}
|
||||
|
||||
val cover = if (size == null) {
|
||||
coverArtService.coverFile(albumId)
|
||||
} else {
|
||||
coverArtService.resizedCover(albumId, size)
|
||||
}
|
||||
|
||||
if (cover == null) {
|
||||
call.respond(HttpStatusCode.NotFound)
|
||||
} else {
|
||||
call.respondFile(cover.toFile())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun contentDisposition(fileName: String): String {
|
||||
val encoded = URLEncoder.encode(fileName, Charsets.UTF_8).replace("+", "%20")
|
||||
val ascii = fileName.map { character ->
|
||||
if (character.code in 32..126 && character != '"' && character != '\\') character else '_'
|
||||
}.joinToString("")
|
||||
|
||||
return "attachment; filename=\"$ascii\"; filename*=UTF-8''$encoded"
|
||||
}
|
||||
Reference in new issue
Block a user