New Playback:AllowAnonymousListening / Playback:AnonymousFragmentSeconds
settings control whether a request without a session can stream at all,
and if so, whether it's limited to a fragment instead of the full track:
- GET /tracks/{id}/stream and GET /youtube/tracks/{videoId}/stream no
longer require authorization outright; the handlers gate on the config
instead, so both keep working exactly as before when it's off (the
default).
- The fragment cap is enforced by the Streaming Gateway, not the client:
the stream ticket now carries an optional maxBytes claim (computed from
AnonymousFragmentSeconds and the transcoder's known bitrate), and
TicketValidationMiddleware clamps the Range it forwards to SeaweedFS to
stay under it — synthesizing one if the request didn't send one, and
answering 416 outright if the requested range starts past the cap.
- A YouTube track that hasn't been provisioned server-side yet streams
straight from YouTube's CDN, bypassing the Gateway entirely — there's no
way to cap that, so an anonymous request for one is refused (409) instead
of silently handing out an uncapped stream.
- ListeningHistory has a non-nullable UserId, so anonymous plays simply
don't get recorded rather than trying to force a row through the FK.
Also fixes a real, pre-existing bug this surfaced: the AppSettings.Development.json
of the Api, Worker, and StreamingGateway all pointed StreamTicket/Workers
paths at "../../deploy/..." — one directory short of deploy/'s actual
location at the repo root relative to each project's own folder. Docker
never hit this (it mounts an absolute /keys path), so it had apparently
never been exercised via `dotnet run` before.
79 lines
2.7 KiB
C#
79 lines
2.7 KiB
C#
using Mediator;
|
|
using Musify.Api.Authentication;
|
|
using Musify.Api.Extensions;
|
|
using Musify.Application.Contracts;
|
|
using Musify.Application.YouTube;
|
|
using Musify.Application.YouTube.Responses;
|
|
|
|
namespace Musify.Api.Endpoints;
|
|
|
|
public static class YouTubeEndpoints
|
|
{
|
|
public static IEndpointRouteBuilder MapYouTubeEndpoints(this IEndpointRouteBuilder app)
|
|
{
|
|
var group = app.MapGroup("/youtube")
|
|
.WithTags("YouTube");
|
|
|
|
group.MapGet("/albums", SearchAlbums)
|
|
.WithName("SearchYouTubeAlbums")
|
|
.WithSummary("Search Albums On YouTube Music.")
|
|
.RequireAuthorization()
|
|
.Produces<YouTubeAlbumSearchResult>()
|
|
.Produces(StatusCodes.Status401Unauthorized)
|
|
.ProducesValidationProblem();
|
|
|
|
group.MapGet("/albums/{albumId}", GetAlbum)
|
|
.WithName("GetYouTubeAlbum")
|
|
.WithSummary("Get A YouTube Music Album With Its Tracklist.")
|
|
.RequireAuthorization()
|
|
.Produces<YouTubeAlbumDetail>()
|
|
.Produces(StatusCodes.Status401Unauthorized)
|
|
.Produces(StatusCodes.Status404NotFound);
|
|
|
|
group.MapGet("/tracks/{videoId}/stream", GetTrackStream)
|
|
.WithName("GetYouTubeTrackStream")
|
|
.WithSummary("Resolve Playback For A YouTube Track: Server Stream If Downloaded, Direct YouTube Stream Otherwise. Works Anonymously When The Playback Configuration Allows It.")
|
|
.Produces<YouTubeStreamResponse>()
|
|
.Produces(StatusCodes.Status401Unauthorized)
|
|
.Produces(StatusCodes.Status404NotFound)
|
|
.Produces(StatusCodes.Status409Conflict);
|
|
|
|
return app;
|
|
}
|
|
|
|
private static async Task<IResult> GetTrackStream(
|
|
IMediator mediator,
|
|
CurrentUser currentUser,
|
|
string videoId,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var result = await mediator.Send(
|
|
new ResolveYouTubeTrackStreamCommand(videoId, currentUser.Id),
|
|
cancellationToken);
|
|
|
|
return result.ToHttpResult();
|
|
}
|
|
|
|
private static async Task<IResult> SearchAlbums(
|
|
IMediator mediator,
|
|
CancellationToken cancellationToken,
|
|
string query,
|
|
string? continuation = null)
|
|
{
|
|
var result = await mediator.Send(
|
|
new SearchYouTubeAlbumsQuery(query, continuation ?? string.Empty),
|
|
cancellationToken);
|
|
|
|
return result.ToHttpResult();
|
|
}
|
|
|
|
private static async Task<IResult> GetAlbum(
|
|
IMediator mediator,
|
|
string albumId,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var result = await mediator.Send(new GetYouTubeAlbumQuery(albumId), cancellationToken);
|
|
return result.ToHttpResult();
|
|
}
|
|
}
|