| | | 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.Database; |
| | | 10 | | using GreenDonut.Data; |
| | | 11 | | using HotChocolate.Types.Pagination; |
| | | 12 | | using Microsoft.EntityFrameworkCore; |
| | | 13 | | using Riok.Mapperly.Abstractions; |
| | | 14 | | using static Api.Files.Queries.FileRecordDataLoaders; |
| | | 15 | | |
| | | 16 | | namespace Api.Files.Queries; |
| | | 17 | | |
| | | 18 | | [QueryType] |
| | | 19 | | public static partial class FileRecordQuery |
| | | 20 | | { |
| | | 21 | | [PermissionCheckAuthorize(UserPermissionBits.FileRead)] |
| | | 22 | | [UseFiltering] |
| | | 23 | | [UseSorting] |
| | | 24 | | public static async Task<PageConnection<FileRecord>> GetFileRecords( |
| | | 25 | | [Service] PGContext db, |
| | | 26 | | [Service] ICurrentUserId userId, |
| | | 27 | | [Service] IFileRecordByIdDataLoader fileRecordById, |
| | | 28 | | QueryContext<FileRecord> qc, |
| | | 29 | | PagingArguments pagingArguments, |
| | | 30 | | CancellationToken ct |
| | | 31 | | ) |
| | | 32 | | { |
| | | 33 | | return await db |
| | | 34 | | .FileRecords.Where(f => f.OwnerId == userId.Id) |
| | | 35 | | .ProjectToDto() |
| | | 36 | | .WithQueryContext(qc) |
| | | 37 | | .ToPageWithDataLoaderAsync(pagingArguments, fileRecordById, ct); |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | [ObjectType<FileRecord>] |
| | | 42 | | public static partial class FileRecordNode |
| | | 43 | | { |
| | | 44 | | [PermissionCheckAuthorize(UserPermissionBits.FileRead)] |
| | | 45 | | [GraphQLName("sha256")] |
| | | 46 | | public static string GetSha256([Parent] FileRecord fileRecord) => |
| | | 47 | | Convert.ToHexStringLower(fileRecord.SHA256); |
| | | 48 | | |
| | | 49 | | [PermissionCheckAuthorize(UserPermissionBits.FileRead)] |
| | | 50 | | [GraphQLIgnore] |
| | | 51 | | public static async Task<FileRecord?> GetByIdAsync( |
| | | 52 | | [Service] IFileRecordByIdDataLoader fileRecordById, |
| | | 53 | | Guid id, |
| | | 54 | | CancellationToken ct |
| | | 55 | | ) => await fileRecordById.LoadAsync(id, cancellationToken: ct); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | public static class FileRecordDataLoaders |
| | | 59 | | { |
| | | 60 | | public interface IFileRecordByIdDataLoader : IBatchDataLoader<Guid, FileRecord>; |
| | | 61 | | |
| | | 62 | | [GraphQLIgnore] |
| | | 63 | | [DataLoader<IFileRecordByIdDataLoader>] |
| | | 64 | | public static async Task<IDictionary<Guid, FileRecord>> GetFileRecordByIdAsync( |
| | | 65 | | IReadOnlyList<Guid> ids, |
| | | 66 | | [Service] PGContext db, |
| | | 67 | | [Service] ICurrentUserId userId, |
| | | 68 | | CancellationToken ct |
| | | 69 | | ) |
| | | 70 | | { |
| | | 71 | | return await db |
| | | 72 | | .FileRecords.Where(f => f.OwnerId == userId.Id) |
| | | 73 | | .Where(f => ids.Contains(f.Id)) |
| | | 74 | | .ProjectToDto() |
| | | 75 | | .ToDictionaryAsync(f => f.Id, cancellationToken: ct); |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | [Node( |
| | | 80 | | NodeResolverType = typeof(FileRecordNode), |
| | | 81 | | NodeResolver = nameof(FileRecordNode.GetByIdAsync) |
| | | 82 | | )] |
| | | 83 | | public class FileRecord : IEntityMetadata, INode |
| | | 84 | | { |
| | 1 | 85 | | public static byte IdPostfix => Models.FileRecord.IdPostfix; |
| | | 86 | | |
| | | 87 | | public Guid Id { get; set; } |
| | | 88 | | public int RowVersion { get; set; } |
| | | 89 | | public NodaTime.Instant MetadataAddedAt { get; set; } |
| | | 90 | | public NodaTime.Instant MetadataUpdatedAt { get; set; } |
| | | 91 | | |
| | | 92 | | public FileKind FileKind { get; set; } |
| | | 93 | | public Guid FileProviderBackendConfigId { get; set; } |
| | | 94 | | |
| | | 95 | | public bool Uploaded { get; set; } |
| | | 96 | | |
| | | 97 | | public string? OriginalFileName { get; set; } |
| | | 98 | | public required string ContentType { get; set; } |
| | | 99 | | public long SizeBytes { get; set; } |
| | | 100 | | |
| | | 101 | | [GraphQLIgnore] |
| | | 102 | | public required byte[] SHA256 { get; set; } |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | [Mapper] |
| | | 106 | | public static partial class FileRecordMapper |
| | | 107 | | { |
| | | 108 | | public static partial FileRecord? ToDto(Models.FileRecord? f); |
| | | 109 | | |
| | | 110 | | public static partial IQueryable<FileRecord> ProjectToDto( |
| | | 111 | | this IQueryable<Models.FileRecord> q |
| | | 112 | | ); |
| | | 113 | | } |
| | | 114 | | // Mostly Ai Generated - End |