Add streaming endpoint for media
This commit is contained in:
parent
f05a32c907
commit
d2da56ad11
1 changed files with 40 additions and 6 deletions
|
|
@ -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<Ok<IEnumerable<SDK.Models.Media>>> 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<IResult> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue