2026-05-22 14:29:22 +08:00
|
|
|
using FileShare_Services.Services.FileLibrary;
|
2026-05-21 16:45:56 +08:00
|
|
|
|
2026-05-22 14:29:22 +08:00
|
|
|
namespace FileShare_API.Extensions
|
2026-05-21 16:45:56 +08:00
|
|
|
{
|
2026-05-22 11:18:47 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// API-only raw file stream endpoints used by browser media elements.
|
|
|
|
|
/// </summary>
|
2026-05-21 16:45:56 +08:00
|
|
|
public static class FileStreamEndpointExtensions
|
|
|
|
|
{
|
2026-05-22 11:18:47 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// Map the media URL emitted by <see cref="FileRecordDto"/>.
|
|
|
|
|
/// </summary>
|
2026-05-21 16:45:56 +08:00
|
|
|
public static IEndpointRouteBuilder MapFileStreamEndpoints(this IEndpointRouteBuilder app)
|
|
|
|
|
{
|
2026-05-22 11:18:47 +08:00
|
|
|
app.MapMethods(
|
|
|
|
|
"/api/files/{id:int}/stream",
|
|
|
|
|
["GET", "HEAD"],
|
|
|
|
|
async (int id, IFileStreamService fileStreamService, HttpContext httpContext) =>
|
|
|
|
|
{
|
|
|
|
|
var fileResponse = await fileStreamService.GetFileStreamAsync(id);
|
|
|
|
|
if (fileResponse is null)
|
|
|
|
|
{
|
|
|
|
|
return Results.NotFound();
|
|
|
|
|
}
|
2026-05-21 16:45:56 +08:00
|
|
|
|
2026-05-22 11:18:47 +08:00
|
|
|
var stream = System.IO.File.Open(
|
|
|
|
|
fileResponse.FilePath,
|
|
|
|
|
FileMode.Open,
|
|
|
|
|
FileAccess.Read,
|
|
|
|
|
FileShare.ReadWrite);
|
2026-05-21 16:45:56 +08:00
|
|
|
|
2026-05-22 11:18:47 +08:00
|
|
|
httpContext.Response.Headers.ContentDisposition =
|
|
|
|
|
$"inline; filename=\"{Uri.EscapeDataString(fileResponse.FileName)}\"";
|
|
|
|
|
httpContext.Response.Headers.AcceptRanges = "bytes";
|
|
|
|
|
httpContext.Response.Headers.CacheControl = "public, max-age=3600";
|
2026-05-21 16:45:56 +08:00
|
|
|
|
2026-05-22 11:18:47 +08:00
|
|
|
return Results.File(
|
|
|
|
|
stream,
|
|
|
|
|
contentType: fileResponse.ContentType,
|
|
|
|
|
lastModified: fileResponse.LastModified,
|
|
|
|
|
enableRangeProcessing: true);
|
|
|
|
|
})
|
|
|
|
|
.WithName("StreamManagedFileById")
|
|
|
|
|
.WithTags("FileLibrary");
|
2026-05-21 16:45:56 +08:00
|
|
|
|
2026-05-22 17:01:49 +08:00
|
|
|
app.MapMethods(
|
|
|
|
|
"/api/thumbnails/{id:int}",
|
|
|
|
|
["GET", "HEAD"],
|
|
|
|
|
async (int id, IThumbnailStreamService thumbnailStreamService, HttpContext httpContext) =>
|
|
|
|
|
{
|
|
|
|
|
var thumbnail = await thumbnailStreamService.GetThumbnailAsync(id);
|
|
|
|
|
if (thumbnail is null)
|
|
|
|
|
{
|
|
|
|
|
return Results.NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var stream = System.IO.File.Open(
|
|
|
|
|
thumbnail.FilePath,
|
|
|
|
|
FileMode.Open,
|
|
|
|
|
FileAccess.Read,
|
|
|
|
|
FileShare.ReadWrite);
|
|
|
|
|
|
|
|
|
|
httpContext.Response.Headers.ContentDisposition =
|
|
|
|
|
$"inline; filename=\"{Uri.EscapeDataString(thumbnail.FileName)}\"";
|
|
|
|
|
httpContext.Response.Headers.CacheControl = "public, max-age=3600";
|
|
|
|
|
|
|
|
|
|
return Results.File(
|
|
|
|
|
stream,
|
|
|
|
|
contentType: thumbnail.ContentType,
|
|
|
|
|
lastModified: thumbnail.LastModified);
|
|
|
|
|
})
|
|
|
|
|
.WithName("StreamManagedThumbnailById")
|
|
|
|
|
.WithTags("FileLibrary");
|
|
|
|
|
|
2026-05-21 16:45:56 +08:00
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|