| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik <[email protected]> |
| | | 2 | | // |
| | | 3 | | // SPDX-License-Identifier: AGPL-3.0-or-later |
| | | 4 | | |
| | | 5 | | // Mostly Ai Generated - Start |
| | | 6 | | using Api.Auth.Handlers; |
| | | 7 | | using Api.Auth.Models; |
| | | 8 | | using Api.Auth.Utils; |
| | | 9 | | using Api.Files.FileProviders; |
| | | 10 | | using Microsoft.AspNetCore.Http.HttpResults; |
| | | 11 | | using Microsoft.AspNetCore.Mvc; |
| | | 12 | | using Microsoft.Net.Http.Headers; |
| | | 13 | | |
| | | 14 | | namespace Api.Files.Endpoints; |
| | | 15 | | |
| | | 16 | | public static class GetFileEndpoint |
| | | 17 | | { |
| | 1 | 18 | | static readonly string[] s_inlineContentTypes = |
| | 1 | 19 | | [ |
| | 1 | 20 | | "image/avif", |
| | 1 | 21 | | "image/jpeg", |
| | 1 | 22 | | "application/epub+zip", |
| | 1 | 23 | | ]; |
| | | 24 | | |
| | | 25 | | [PermissionCheckAuthorize(UserPermissionBits.FileRead)] |
| | | 26 | | public static async Task<Results<FileStreamHttpResult, NotFound>> GetFileAsync( |
| | | 27 | | Guid id, |
| | | 28 | | [FromServices] ICurrentUserId userId, |
| | | 29 | | [FromServices] UserFileRouter router, |
| | | 30 | | HttpContext httpContext, |
| | | 31 | | CancellationToken ct |
| | | 32 | | ) |
| | 1 | 33 | | { |
| | 1 | 34 | | if (await router.GetFileAsync(userId.Id!.Value, id, ct) is not { } content) |
| | 1 | 35 | | { |
| | 1 | 36 | | return TypedResults.NotFound(); |
| | | 37 | | } |
| | | 38 | | |
| | 1 | 39 | | httpContext.Response.Headers["Cache-Control"] = |
| | 1 | 40 | | "private, max-age=31536000, immutable"; |
| | 1 | 41 | | httpContext.Response.Headers["X-Content-Type-Options"] = "nosniff"; |
| | | 42 | | |
| | 1 | 43 | | var inline = s_inlineContentTypes.Contains(content.ContentType); |
| | | 44 | | |
| | 1 | 45 | | return TypedResults.File( |
| | 1 | 46 | | content.Stream, |
| | 1 | 47 | | inline ? content.ContentType : "application/octet-stream", |
| | 1 | 48 | | inline ? null : content.OriginalFileName ?? id.ToString(), |
| | 1 | 49 | | content.MetadataAddedAt.ToDateTimeOffset(), |
| | 1 | 50 | | new EntityTagHeaderValue($"\"{Convert.ToHexStringLower(content.SHA256)}\""), |
| | 1 | 51 | | enableRangeProcessing: true |
| | 1 | 52 | | ); |
| | 1 | 53 | | } |
| | | 54 | | } |
| | | 55 | | // Mostly Ai Generated - End |