From d2da56ad11b71fb7c8eca61f4f2239fa337d0144 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Sun, 26 Apr 2026 01:19:31 -0500 Subject: [PATCH] Add streaming endpoint for media --- .../Endpoints/MediaEndpoints.cs | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/LANCommander.Server/Endpoints/MediaEndpoints.cs b/LANCommander.Server/Endpoints/MediaEndpoints.cs index c181d3a4..36881374 100644 --- a/LANCommander.Server/Endpoints/MediaEndpoints.cs +++ b/LANCommander.Server/Endpoints/MediaEndpoints.cs @@ -15,6 +15,7 @@ public static class MediaEndpoints group.MapGet("/{id:guid}", GetByIdAsync); group.MapGet("/{id:guid}/Thumbnail", ThumbnailAsync).AllowAnonymous(); group.MapGet("/{id:guid}/Download", DownloadAsync).AllowAnonymous(); + group.MapGet("/{id:guid}/Stream", StreamAsync).AllowAnonymous(); } internal static async Task>> GetAsync( @@ -88,15 +89,48 @@ public static class MediaEndpoints logger.LogWarning("Media file {Id} does not exist", id); return TypedResults.NotFound(); } - catch (DirectoryNotFoundException) + catch (DirectoryNotFoundException) { logger.LogWarning("Media file {Id} does not exist.", id); - return TypedResults.NotFound(); + return TypedResults.NotFound(); } - catch (Exception ex) - { - logger.LogError(ex, "Unhandled exception raised reading media item {Id}.", id); - return TypedResults.InternalServerError(); + catch (Exception ex) + { + logger.LogError(ex, "Unhandled exception raised reading media item {Id}.", id); + return TypedResults.InternalServerError(); + } + } + + internal static async Task StreamAsync( + Guid id, + [FromServices] MediaService mediaService, + [FromServices] ILoggerFactory loggerFactory) + { + var logger = loggerFactory.CreateLogger(nameof(MediaEndpoints)); + try + { + var media = await mediaService.GetAsync(id); + var path = MediaService.GetMediaPath(media); + + if (!File.Exists(path)) + return TypedResults.NotFound(); + + return TypedResults.PhysicalFile(path, media.MimeType, enableRangeProcessing: true); + } + catch (FileNotFoundException) + { + logger.LogWarning("Media file {Id} does not exist", id); + return TypedResults.NotFound(); + } + catch (DirectoryNotFoundException) + { + logger.LogWarning("Media file {Id} does not exist.", id); + return TypedResults.NotFound(); + } + catch (Exception ex) + { + logger.LogError(ex, "Unhandled exception raised streaming media item {Id}.", id); + return TypedResults.InternalServerError(); } } }