🐛 Fix file reference JSON loop

This commit is contained in:
2025-11-18 21:52:21 +08:00
parent 587066d847
commit 4ab0dcf1c2
5 changed files with 26 additions and 26 deletions

View File

@@ -24,7 +24,7 @@ public class AppDatabase(
public DbSet<QuotaRecord> QuotaRecords { get; set; } = null!; public DbSet<QuotaRecord> QuotaRecords { get; set; } = null!;
public DbSet<SnCloudFile> Files { get; set; } = null!; public DbSet<SnCloudFile> Files { get; set; } = null!;
public DbSet<CloudFileReference> FileReferences { get; set; } = null!; public DbSet<SnCloudFileReference> FileReferences { get; set; } = null!;
public DbSet<SnCloudFileIndex> FileIndexes { get; set; } public DbSet<SnCloudFileIndex> FileIndexes { get; set; }
public DbSet<PersistentTask> Tasks { get; set; } = null!; public DbSet<PersistentTask> Tasks { get; set; } = null!;

View File

@@ -179,7 +179,7 @@ namespace DysonNetwork.Drive.Migrations
b.UseTphMappingStrategy(); b.UseTphMappingStrategy();
}); });
modelBuilder.Entity("DysonNetwork.Shared.Models.CloudFileReference", b => modelBuilder.Entity("DysonNetwork.Shared.Models.SnCloudFileReference", b =>
{ {
b.Property<Guid>("Id") b.Property<Guid>("Id")
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
@@ -571,7 +571,7 @@ namespace DysonNetwork.Drive.Migrations
b.HasDiscriminator().HasValue("PersistentUploadTask"); 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") b.HasOne("DysonNetwork.Shared.Models.SnCloudFile", "File")
.WithMany("References") .WithMany("References")

View File

@@ -231,7 +231,7 @@ public class FileController(
} }
[HttpGet("{id}/references")] [HttpGet("{id}/references")]
public async Task<ActionResult<List<Shared.Models.CloudFileReference>>> GetFileReferences(string id) public async Task<ActionResult<List<Shared.Models.SnCloudFileReference>>> GetFileReferences(string id)
{ {
var file = await fs.GetFileAsync(id); var file = await fs.GetFileAsync(id);
if (file is null) return NotFound("File not found."); if (file is null) return NotFound("File not found.");

View File

@@ -21,7 +21,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach
/// <param name="expiredAt">Optional expiration time for the file</param> /// <param name="expiredAt">Optional expiration time for the file</param>
/// <param name="duration">Optional duration after which the file expires (alternative to expiredAt)</param> /// <param name="duration">Optional duration after which the file expires (alternative to expiredAt)</param>
/// <returns>The created file reference</returns> /// <returns>The created file reference</returns>
public async Task<CloudFileReference> CreateReferenceAsync( public async Task<SnCloudFileReference> CreateReferenceAsync(
string fileId, string fileId,
string usage, string usage,
string resourceId, string resourceId,
@@ -34,7 +34,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach
if (duration.HasValue) if (duration.HasValue)
finalExpiration = SystemClock.Instance.GetCurrentInstant() + duration.Value; finalExpiration = SystemClock.Instance.GetCurrentInstant() + duration.Value;
var reference = new CloudFileReference var reference = new SnCloudFileReference
{ {
FileId = fileId, FileId = fileId,
Usage = usage, Usage = usage,
@@ -50,7 +50,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach
return reference; return reference;
} }
public async Task<List<CloudFileReference>> CreateReferencesAsync( public async Task<List<SnCloudFileReference>> CreateReferencesAsync(
List<string> fileId, List<string> fileId,
string usage, string usage,
string resourceId, string resourceId,
@@ -58,7 +58,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach
Duration? duration = null Duration? duration = null
) )
{ {
var data = fileId.Select(id => new CloudFileReference var data = fileId.Select(id => new SnCloudFileReference
{ {
FileId = id, FileId = id,
Usage = usage, Usage = usage,
@@ -74,11 +74,11 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach
/// </summary> /// </summary>
/// <param name="fileId">The ID of the file</param> /// <param name="fileId">The ID of the file</param>
/// <returns>A list of all references to the file</returns> /// <returns>A list of all references to the file</returns>
public async Task<List<CloudFileReference>> GetReferencesAsync(string fileId) public async Task<List<SnCloudFileReference>> GetReferencesAsync(string fileId)
{ {
var cacheKey = $"{CacheKeyPrefix}list:{fileId}"; var cacheKey = $"{CacheKeyPrefix}list:{fileId}";
var cachedReferences = await cache.GetAsync<List<CloudFileReference>>(cacheKey); var cachedReferences = await cache.GetAsync<List<SnCloudFileReference>>(cacheKey);
if (cachedReferences is not null) if (cachedReferences is not null)
return cachedReferences; return cachedReferences;
@@ -91,17 +91,17 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach
return references; return references;
} }
public async Task<Dictionary<string, List<CloudFileReference>>> GetReferencesAsync(IEnumerable<string> fileIds) public async Task<Dictionary<string, List<SnCloudFileReference>>> GetReferencesAsync(IEnumerable<string> fileIds)
{ {
var fileIdList = fileIds.ToList(); var fileIdList = fileIds.ToList();
var result = new Dictionary<string, List<CloudFileReference>>(); var result = new Dictionary<string, List<SnCloudFileReference>>();
// Check cache for each file ID // Check cache for each file ID
var uncachedFileIds = new List<string>(); var uncachedFileIds = new List<string>();
foreach (var fileId in fileIdList) foreach (var fileId in fileIdList)
{ {
var cacheKey = $"{CacheKeyPrefix}list:{fileId}"; var cacheKey = $"{CacheKeyPrefix}list:{fileId}";
var cachedReferences = await cache.GetAsync<List<CloudFileReference>>(cacheKey); var cachedReferences = await cache.GetAsync<List<SnCloudFileReference>>(cacheKey);
if (cachedReferences is not null) if (cachedReferences is not null)
{ {
result[fileId] = cachedReferences; result[fileId] = cachedReferences;
@@ -159,11 +159,11 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach
/// </summary> /// </summary>
/// <param name="resourceId">The ID of the resource</param> /// <param name="resourceId">The ID of the resource</param>
/// <returns>A list of file references associated with the resource</returns> /// <returns>A list of file references associated with the resource</returns>
public async Task<List<CloudFileReference>> GetResourceReferencesAsync(string resourceId) public async Task<List<SnCloudFileReference>> GetResourceReferencesAsync(string resourceId)
{ {
var cacheKey = $"{CacheKeyPrefix}resource:{resourceId}"; var cacheKey = $"{CacheKeyPrefix}resource:{resourceId}";
var cachedReferences = await cache.GetAsync<List<CloudFileReference>>(cacheKey); var cachedReferences = await cache.GetAsync<List<SnCloudFileReference>>(cacheKey);
if (cachedReferences is not null) if (cachedReferences is not null)
return cachedReferences; return cachedReferences;
@@ -181,11 +181,11 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach
/// </summary> /// </summary>
/// <param name="usage">The usage context</param> /// <param name="usage">The usage context</param>
/// <returns>A list of file references with the specified usage</returns> /// <returns>A list of file references with the specified usage</returns>
public async Task<List<CloudFileReference>> GetUsageReferencesAsync(string usage) public async Task<List<SnCloudFileReference>> GetUsageReferencesAsync(string usage)
{ {
var cacheKey = $"{CacheKeyPrefix}usage:{usage}"; var cacheKey = $"{CacheKeyPrefix}usage:{usage}";
var cachedReferences = await cache.GetAsync<List<CloudFileReference>>(cacheKey); var cachedReferences = await cache.GetAsync<List<SnCloudFileReference>>(cacheKey);
if (cachedReferences is not null) if (cachedReferences is not null)
return cachedReferences; return cachedReferences;
@@ -307,7 +307,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach
/// <param name="expiredAt">Optional expiration time for newly added files</param> /// <param name="expiredAt">Optional expiration time for newly added files</param>
/// <param name="duration">Optional duration after which newly added files expire</param> /// <param name="duration">Optional duration after which newly added files expire</param>
/// <returns>A list of the updated file references</returns> /// <returns>A list of the updated file references</returns>
public async Task<List<CloudFileReference>> UpdateResourceFilesAsync( public async Task<List<SnCloudFileReference>> UpdateResourceFilesAsync(
string resourceId, string resourceId,
IEnumerable<string>? newFileIds, IEnumerable<string>? newFileIds,
string usage, string usage,
@@ -315,7 +315,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach
Duration? duration = null) Duration? duration = null)
{ {
if (newFileIds == null) if (newFileIds == null)
return new List<CloudFileReference>(); return new List<SnCloudFileReference>();
var existingReferences = await db.FileReferences var existingReferences = await db.FileReferences
.Where(r => r.ResourceId == resourceId && r.Usage == usage) .Where(r => r.ResourceId == resourceId && r.Usage == usage)
@@ -333,7 +333,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach
// Files to add // Files to add
var toAdd = newFileIdsList var toAdd = newFileIdsList
.Where(id => !existingFileIds.Contains(id)) .Where(id => !existingFileIds.Contains(id))
.Select(id => new CloudFileReference .Select(id => new SnCloudFileReference
{ {
FileId = id, FileId = id,
Usage = usage, Usage = usage,
@@ -485,7 +485,7 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach
/// <param name="resourceId">The resource ID</param> /// <param name="resourceId">The resource ID</param>
/// <param name="usageType">The usage type</param> /// <param name="usageType">The usage type</param>
/// <returns>List of file references</returns> /// <returns>List of file references</returns>
public async Task<List<CloudFileReference>> GetResourceReferencesAsync(string resourceId, string usageType) public async Task<List<SnCloudFileReference>> GetResourceReferencesAsync(string resourceId, string usageType)
{ {
return await db.FileReferences return await db.FileReferences
.Where(r => r.ResourceId == resourceId && r.Usage == usageType) .Where(r => r.ResourceId == resourceId && r.Usage == usageType)

View File

@@ -57,7 +57,7 @@ public class SnCloudFile : ModelBase, ICloudFile, IIdentifiedResource
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? FastUploadLink { get; set; } public string? FastUploadLink { get; set; }
public ICollection<CloudFileReference> References { get; set; } = new List<CloudFileReference>(); public ICollection<SnCloudFileReference> References { get; set; } = new List<SnCloudFileReference>();
public Guid AccountId { get; set; } 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(); public Guid Id { get; set; } = Guid.NewGuid();
[MaxLength(32)] public string FileId { get; set; } = null!; [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 Usage { get; set; } = null!;
[MaxLength(1024)] public string ResourceId { get; set; } = null!; [MaxLength(1024)] public string ResourceId { get; set; } = null!;
@@ -124,10 +124,10 @@ public class CloudFileReference : ModelBase
public Instant? ExpiredAt { get; set; } public Instant? ExpiredAt { get; set; }
/// <summary> /// <summary>
/// Converts the CloudFileReference to a protobuf message /// Converts the SnCloudFileReference to a protobuf message
/// </summary> /// </summary>
/// <returns>The protobuf message representation of this object</returns> /// <returns>The protobuf message representation of this object</returns>
public Proto.CloudFileReference ToProtoValue() public CloudFileReference ToProtoValue()
{ {
return new Proto.CloudFileReference return new Proto.CloudFileReference
{ {