| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik <alper@alper-celik.dev> |
| | | 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.Database; |
| | | 10 | | using Api.Database.Utils; |
| | | 11 | | using Api.Files.FileProviders; |
| | | 12 | | using Api.Files.Queries; |
| | | 13 | | using Api.Utils; |
| | | 14 | | using FluentValidation; |
| | | 15 | | using HotChocolate.Types; |
| | | 16 | | using Microsoft.EntityFrameworkCore; |
| | | 17 | | using static Api.Utils.ValidatorUtils; |
| | | 18 | | |
| | | 19 | | namespace Api.Files.Mutations; |
| | | 20 | | |
| | | 21 | | [MutationType] |
| | | 22 | | public static partial class UploadFileRecordContentMutations |
| | | 23 | | { |
| | | 24 | | [PermissionCheckAuthorize(UserPermissionBits.FileWrite)] |
| | | 25 | | [Error(typeof(DomainError))] |
| | | 26 | | public static async Task<UploadFileRecordContentPayload> UploadFileRecordContentAsync( |
| | | 27 | | [Service] ICurrentUserId userId, |
| | | 28 | | [Service] IEFTransactionDIAccessorService txGetter, |
| | | 29 | | [Service] UserFileRouter router, |
| | | 30 | | [Service] IValidator<UploadFileRecordContentInput> validator, |
| | | 31 | | UploadFileRecordContentInput input, |
| | | 32 | | IFile file, |
| | | 33 | | CancellationToken ct |
| | | 34 | | ) |
| | | 35 | | { |
| | | 36 | | await validator.ValidateOrThrowInputAsync(input, ct); |
| | | 37 | | |
| | | 38 | | var tx = await txGetter.BeginOrGetTransactionAsync(ct); |
| | | 39 | | |
| | | 40 | | var fileRecord = await router.SetFileAsync( |
| | | 41 | | userId.Id!.Value, |
| | | 42 | | input.Id, |
| | | 43 | | file.OpenReadStream(), |
| | | 44 | | ct |
| | | 45 | | ); |
| | | 46 | | |
| | | 47 | | if (fileRecord is null) |
| | | 48 | | { |
| | | 49 | | throw new DomainException( |
| | | 50 | | ErrorCodes.FILE_UPLOAD_FAILED, |
| | | 51 | | "File content did not match the declared size or SHA256" |
| | | 52 | | ); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | await tx.CommitAsync(ct); |
| | | 56 | | return new UploadFileRecordContentPayload(FileRecordMapper.ToDto(fileRecord)); |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public record UploadFileRecordContentPayload(Queries.FileRecord FileRecord); |
| | | 61 | | |
| | | 62 | | public record UploadFileRecordContentInput |
| | | 63 | | { |
| | | 64 | | public required Guid Id { get; init; } |
| | | 65 | | |
| | | 66 | | public class UploadFileRecordContentInputValidator |
| | | 67 | | : AbstractValidator<UploadFileRecordContentInput> |
| | | 68 | | { |
| | 1 | 69 | | public UploadFileRecordContentInputValidator( |
| | 1 | 70 | | PGContext db, |
| | 1 | 71 | | ICurrentUserId userId, |
| | 1 | 72 | | IEFTransactionDIAccessorService tx |
| | 1 | 73 | | ) |
| | 1 | 74 | | { |
| | 1 | 75 | | RuleFor(w => w).BeginTransaction(tx); |
| | | 76 | | |
| | 1 | 77 | | RuleFor(w => w.Id).IdMustExist(db.FileRecords, userId.Id); |
| | | 78 | | |
| | 1 | 79 | | RuleFor(w => w.Id) |
| | 1 | 80 | | .MustAsync( |
| | 1 | 81 | | async (id, ct) => |
| | 1 | 82 | | !await db.FileRecords.AnyAsync( |
| | 1 | 83 | | f => f.OwnerId == userId.Id && f.Id == id && f.Uploaded, |
| | 1 | 84 | | ct |
| | 1 | 85 | | ) |
| | 1 | 86 | | ) |
| | 1 | 87 | | .WithMessage("File is already uploaded") |
| | 1 | 88 | | .WithErrorCode(ErrorCodes.FILE_ALREADY_UPLOADED); |
| | 1 | 89 | | } |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | // Mostly Ai Generated - End |