| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik <alper@alper-celik.dev> |
| | | 2 | | // |
| | | 3 | | // SPDX-License-Identifier: AGPL-3.0-or-later |
| | | 4 | | |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text.Json; |
| | | 7 | | using Api.Database; |
| | | 8 | | using Api.Files.Endpoints; |
| | | 9 | | using Api.Files.FileProviders; |
| | | 10 | | using Api.Files.Models; |
| | | 11 | | using Microsoft.EntityFrameworkCore; |
| | | 12 | | |
| | | 13 | | namespace Api.Files; |
| | | 14 | | |
| | | 15 | | public class Setup |
| | | 16 | | { |
| | | 17 | | // Mostly Ai Generated - Start |
| | | 18 | | public static void RegisterServices(IServiceCollection services) |
| | 1 | 19 | | { |
| | 1 | 20 | | services.AddSingleton<FileProviderFactory>(); |
| | 1 | 21 | | services.AddScoped<UserFileRouter>(); |
| | 1 | 22 | | } |
| | | 23 | | |
| | | 24 | | public static void MapEndpoints(IEndpointRouteBuilder route) |
| | 1 | 25 | | { |
| | 1 | 26 | | route.MapGet("/{id:guid}", GetFileEndpoint.GetFileAsync); |
| | 1 | 27 | | route |
| | 1 | 28 | | .MapPost("/{id:guid}", UploadFileEndpoint.UploadFileAsync) |
| | 1 | 29 | | .DisableAntiforgery(); |
| | 1 | 30 | | } |
| | | 31 | | |
| | | 32 | | // Mostly Ai Generated - End |
| | | 33 | | |
| | | 34 | | public static async Task SeedDb(PGContext db, IConfiguration config) |
| | 1 | 35 | | { |
| | 1 | 36 | | var defaultConfigExists = await db |
| | 1 | 37 | | .FileProviderBackendConfigs.Where(cfg => |
| | 1 | 38 | | cfg.InstanceWide == true && cfg.FallbackDefault == true |
| | 1 | 39 | | ) |
| | 1 | 40 | | .AsNoTracking() |
| | 1 | 41 | | .AnyAsync(); |
| | 1 | 42 | | if (!defaultConfigExists) |
| | 1 | 43 | | { |
| | 1 | 44 | | var now = Now(); |
| | 1 | 45 | | var fsCfg = new FileProviderBackendConfig() |
| | 1 | 46 | | { |
| | 1 | 47 | | Id = Guid.CreateVersion7() |
| | 1 | 48 | | .WithPostfix(FileProviderBackendConfig.IdPostfix), |
| | 1 | 49 | | MetadataAddedAt = now, |
| | 1 | 50 | | MetadataUpdatedAt = now, |
| | 1 | 51 | | OwnerId = null, |
| | 1 | 52 | | |
| | 1 | 53 | | InstanceWide = true, |
| | 1 | 54 | | FallbackDefault = true, |
| | 1 | 55 | | DefaultFor = [], |
| | 1 | 56 | | ProviderId = FileProviderId.LocalFsProvider, |
| | 1 | 57 | | ProviderConfig = JsonSerializer.SerializeToDocument( |
| | 1 | 58 | | new LocalFSFileProviderConfig( |
| | 1 | 59 | | $"./BlobStorage{(config["PR_TestPrefix"] is { } prefix ? $"_{prefix}" : "")}" |
| | 1 | 60 | | ) |
| | 1 | 61 | | ), |
| | 1 | 62 | | }; |
| | | 63 | | |
| | 1 | 64 | | await db.FileProviderBackendConfigs.AddAsync(fsCfg); |
| | 1 | 65 | | await db.SaveChangesAsync(); |
| | 1 | 66 | | } |
| | 1 | 67 | | } |
| | | 68 | | } |