From 4ab0dcf1c2c864e909a7ddaad9ec32d6b1d379b4 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 18 Nov 2025 21:52:21 +0800 Subject: [PATCH] :bug: Fix file reference JSON loop --- DysonNetwork.Drive/AppDatabase.cs | 2 +- .../Migrations/AppDatabaseModelSnapshot.cs | 4 +-- DysonNetwork.Drive/Storage/FileController.cs | 2 +- .../Storage/FileReferenceService.cs | 34 +++++++++---------- DysonNetwork.Shared/Models/CloudFile.cs | 10 +++--- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/DysonNetwork.Drive/AppDatabase.cs b/DysonNetwork.Drive/AppDatabase.cs index 85eded4..2eb7b08 100644 --- a/DysonNetwork.Drive/AppDatabase.cs +++ b/DysonNetwork.Drive/AppDatabase.cs @@ -24,7 +24,7 @@ public class AppDatabase( public DbSet QuotaRecords { get; set; } = null!; public DbSet Files { get; set; } = null!; - public DbSet FileReferences { get; set; } = null!; + public DbSet FileReferences { get; set; } = null!; public DbSet FileIndexes { get; set; } public DbSet Tasks { get; set; } = null!; diff --git a/DysonNetwork.Drive/Migrations/AppDatabaseModelSnapshot.cs b/DysonNetwork.Drive/Migrations/AppDatabaseModelSnapshot.cs index 824af79..0cf2fb3 100644 --- a/DysonNetwork.Drive/Migrations/AppDatabaseModelSnapshot.cs +++ b/DysonNetwork.Drive/Migrations/AppDatabaseModelSnapshot.cs @@ -179,7 +179,7 @@ namespace DysonNetwork.Drive.Migrations b.UseTphMappingStrategy(); }); - modelBuilder.Entity("DysonNetwork.Shared.Models.CloudFileReference", b => + modelBuilder.Entity("DysonNetwork.Shared.Models.SnCloudFileReference", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -571,7 +571,7 @@ namespace DysonNetwork.Drive.Migrations b.HasDiscriminator().HasValue("PersistentUploadTask"); }); - modelBuilder.Entity("DysonNetwork.Shared.Models.CloudFileReference", b => + modelBuilder.Entity("DysonNetwork.Shared.Models.SnCloudFileReference", b => { b.HasOne("DysonNetwork.Shared.Models.SnCloudFile", "File") .WithMany("References") diff --git a/DysonNetwork.Drive/Storage/FileController.cs b/DysonNetwork.Drive/Storage/FileController.cs index 5456e4d..086f51c 100644 --- a/DysonNetwork.Drive/Storage/FileController.cs +++ b/DysonNetwork.Drive/Storage/FileController.cs @@ -231,7 +231,7 @@ public class FileController( } [HttpGet("{id}/references")] - public async Task>> GetFileReferences(string id) + public async Task>> GetFileReferences(string id) { var file = await fs.GetFileAsync(id); if (file is null) return NotFound("File not found."); diff --git a/DysonNetwork.Drive/Storage/FileReferenceService.cs b/DysonNetwork.Drive/Storage/FileReferenceService.cs index ae6a53c..4127c76 100644 --- a/DysonNetwork.Drive/Storage/FileReferenceService.cs +++ b/DysonNetwork.Drive/Storage/FileReferenceService.cs @@ -21,7 +21,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach /// Optional expiration time for the file /// Optional duration after which the file expires (alternative to expiredAt) /// The created file reference - public async Task CreateReferenceAsync( + public async Task CreateReferenceAsync( string fileId, string usage, string resourceId, @@ -34,7 +34,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach if (duration.HasValue) finalExpiration = SystemClock.Instance.GetCurrentInstant() + duration.Value; - var reference = new CloudFileReference + var reference = new SnCloudFileReference { FileId = fileId, Usage = usage, @@ -50,7 +50,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach return reference; } - public async Task> CreateReferencesAsync( + public async Task> CreateReferencesAsync( List fileId, string usage, string resourceId, @@ -58,7 +58,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach Duration? duration = null ) { - var data = fileId.Select(id => new CloudFileReference + var data = fileId.Select(id => new SnCloudFileReference { FileId = id, Usage = usage, @@ -74,11 +74,11 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach /// /// The ID of the file /// A list of all references to the file - public async Task> GetReferencesAsync(string fileId) + public async Task> GetReferencesAsync(string fileId) { var cacheKey = $"{CacheKeyPrefix}list:{fileId}"; - var cachedReferences = await cache.GetAsync>(cacheKey); + var cachedReferences = await cache.GetAsync>(cacheKey); if (cachedReferences is not null) return cachedReferences; @@ -91,17 +91,17 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach return references; } - public async Task>> GetReferencesAsync(IEnumerable fileIds) + public async Task>> GetReferencesAsync(IEnumerable fileIds) { var fileIdList = fileIds.ToList(); - var result = new Dictionary>(); + var result = new Dictionary>(); // Check cache for each file ID var uncachedFileIds = new List(); foreach (var fileId in fileIdList) { var cacheKey = $"{CacheKeyPrefix}list:{fileId}"; - var cachedReferences = await cache.GetAsync>(cacheKey); + var cachedReferences = await cache.GetAsync>(cacheKey); if (cachedReferences is not null) { result[fileId] = cachedReferences; @@ -159,11 +159,11 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach /// /// The ID of the resource /// A list of file references associated with the resource - public async Task> GetResourceReferencesAsync(string resourceId) + public async Task> GetResourceReferencesAsync(string resourceId) { var cacheKey = $"{CacheKeyPrefix}resource:{resourceId}"; - var cachedReferences = await cache.GetAsync>(cacheKey); + var cachedReferences = await cache.GetAsync>(cacheKey); if (cachedReferences is not null) return cachedReferences; @@ -181,11 +181,11 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach /// /// The usage context /// A list of file references with the specified usage - public async Task> GetUsageReferencesAsync(string usage) + public async Task> GetUsageReferencesAsync(string usage) { var cacheKey = $"{CacheKeyPrefix}usage:{usage}"; - var cachedReferences = await cache.GetAsync>(cacheKey); + var cachedReferences = await cache.GetAsync>(cacheKey); if (cachedReferences is not null) return cachedReferences; @@ -307,7 +307,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach /// Optional expiration time for newly added files /// Optional duration after which newly added files expire /// A list of the updated file references - public async Task> UpdateResourceFilesAsync( + public async Task> UpdateResourceFilesAsync( string resourceId, IEnumerable? newFileIds, string usage, @@ -315,7 +315,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach Duration? duration = null) { if (newFileIds == null) - return new List(); + return new List(); var existingReferences = await db.FileReferences .Where(r => r.ResourceId == resourceId && r.Usage == usage) @@ -333,7 +333,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach // Files to add var toAdd = newFileIdsList .Where(id => !existingFileIds.Contains(id)) - .Select(id => new CloudFileReference + .Select(id => new SnCloudFileReference { FileId = id, Usage = usage, @@ -485,7 +485,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach /// The resource ID /// The usage type /// List of file references - public async Task> GetResourceReferencesAsync(string resourceId, string usageType) + public async Task> GetResourceReferencesAsync(string resourceId, string usageType) { return await db.FileReferences .Where(r => r.ResourceId == resourceId && r.Usage == usageType) diff --git a/DysonNetwork.Shared/Models/CloudFile.cs b/DysonNetwork.Shared/Models/CloudFile.cs index 46256dc..d937f8a 100644 --- a/DysonNetwork.Shared/Models/CloudFile.cs +++ b/DysonNetwork.Shared/Models/CloudFile.cs @@ -57,7 +57,7 @@ public class SnCloudFile : ModelBase, ICloudFile, IIdentifiedResource [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? FastUploadLink { get; set; } - public ICollection References { get; set; } = new List(); + public ICollection References { get; set; } = new List(); public Guid AccountId { get; set; } @@ -110,11 +110,11 @@ public class SnCloudFile : ModelBase, ICloudFile, IIdentifiedResource } } -public class CloudFileReference : ModelBase +public class SnCloudFileReference : ModelBase { public Guid Id { get; set; } = Guid.NewGuid(); [MaxLength(32)] public string FileId { get; set; } = null!; - public SnCloudFile File { get; set; } = null!; + [JsonIgnore] public SnCloudFile File { get; set; } = null!; [MaxLength(1024)] public string Usage { get; set; } = null!; [MaxLength(1024)] public string ResourceId { get; set; } = null!; @@ -124,10 +124,10 @@ public class CloudFileReference : ModelBase public Instant? ExpiredAt { get; set; } /// - /// Converts the CloudFileReference to a protobuf message + /// Converts the SnCloudFileReference to a protobuf message /// /// The protobuf message representation of this object - public Proto.CloudFileReference ToProtoValue() + public CloudFileReference ToProtoValue() { return new Proto.CloudFileReference {