From 6d5303f99c97e7837260c46257494882feabb4e4 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 11 Jan 2026 23:58:37 +0800 Subject: [PATCH] :wastebasket: Clean up the cloud file table unused fields --- DysonNetwork.Drive/Billing/UsageService.cs | 4 ++++ DysonNetwork.Drive/Index/FileIndexController.cs | 1 + DysonNetwork.Drive/Index/FileIndexService.cs | 1 + DysonNetwork.Drive/Startup/BroadcastEventHandler.cs | 9 ++++++--- DysonNetwork.Drive/Storage/FileController.cs | 1 + DysonNetwork.Drive/Storage/FileUploadController.cs | 5 ++++- DysonNetwork.Shared/Models/CloudFile.cs | 12 ++++++------ DysonNetwork.Shared/Models/CloudFileObject.cs | 3 ++- DysonNetwork.sln.DotSettings.user | 1 + 9 files changed, 26 insertions(+), 11 deletions(-) diff --git a/DysonNetwork.Drive/Billing/UsageService.cs b/DysonNetwork.Drive/Billing/UsageService.cs index ca992738..1c9877e6 100644 --- a/DysonNetwork.Drive/Billing/UsageService.cs +++ b/DysonNetwork.Drive/Billing/UsageService.cs @@ -42,9 +42,11 @@ public class UsageService(AppDatabase db) PoolName = p.Name, UsageBytes = fileQuery .Where(f => f.PoolId == p.Id) + .Include(f => f.Object) .Sum(f => f.Size), Cost = fileQuery .Where(f => f.PoolId == p.Id) + .Include(f => f.Object) .Sum(f => f.Size) / 1024.0 / 1024.0 * (p.BillingConfig.CostMultiplier ?? 1.0), FileCount = fileQuery @@ -80,6 +82,7 @@ public class UsageService(AppDatabase db) .AsQueryable(); var usageBytes = await fileQuery + .Include(f => f.Object) .SumAsync(f => f.Size); var fileCount = await fileQuery @@ -106,6 +109,7 @@ public class UsageService(AppDatabase db) .Where(f => f.PoolId.HasValue) .Where(f => !f.IsMarkedRecycle) .Include(f => f.Pool) + .Include(f => f.Object) .Where(f => !f.ExpiredAt.HasValue || f.ExpiredAt > now) .Select(f => new { diff --git a/DysonNetwork.Drive/Index/FileIndexController.cs b/DysonNetwork.Drive/Index/FileIndexController.cs index 6a6040a9..d8758c09 100644 --- a/DysonNetwork.Drive/Index/FileIndexController.cs +++ b/DysonNetwork.Drive/Index/FileIndexController.cs @@ -216,6 +216,7 @@ public class FileIndexController( && f.IsMarkedRecycle == recycled && !db.FileIndexes.Any(fi => fi.FileId == f.Id && fi.AccountId == accountId) ) + .Include(f => f.Object) .AsQueryable(); // Apply sorting diff --git a/DysonNetwork.Drive/Index/FileIndexService.cs b/DysonNetwork.Drive/Index/FileIndexService.cs index 174d82d2..9d8d7d8a 100644 --- a/DysonNetwork.Drive/Index/FileIndexService.cs +++ b/DysonNetwork.Drive/Index/FileIndexService.cs @@ -167,6 +167,7 @@ public class FileIndexService(AppDatabase db) return await db.FileIndexes .Where(fi => fi.AccountId == accountId) .Include(fi => fi.File) + .ThenInclude(f => f.Object) .ToListAsync(); } diff --git a/DysonNetwork.Drive/Startup/BroadcastEventHandler.cs b/DysonNetwork.Drive/Startup/BroadcastEventHandler.cs index 43006694..05c9b898 100644 --- a/DysonNetwork.Drive/Startup/BroadcastEventHandler.cs +++ b/DysonNetwork.Drive/Startup/BroadcastEventHandler.cs @@ -314,9 +314,12 @@ public class BroadcastEventHandler( await scopedDb.Files.Where(f => f.Id == fileId).ExecuteUpdateAsync(setter => setter .SetProperty(f => f.UploadedAt, now) .SetProperty(f => f.PoolId, destPool) - .SetProperty(f => f.MimeType, newMimeType) - .SetProperty(f => f.HasCompression, hasCompression) - .SetProperty(f => f.HasThumbnail, hasThumbnail) + ); + + await scopedDb.FileObjects.Where(fo => fo.Id == fileId).ExecuteUpdateAsync(setter => setter + .SetProperty(fo => fo.MimeType, newMimeType) + .SetProperty(fo => fo.HasCompression, hasCompression) + .SetProperty(fo => fo.HasThumbnail, hasThumbnail) ); // Only delete temp file after successful upload and db update diff --git a/DysonNetwork.Drive/Storage/FileController.cs b/DysonNetwork.Drive/Storage/FileController.cs index 0741d827..fe63aea1 100644 --- a/DysonNetwork.Drive/Storage/FileController.cs +++ b/DysonNetwork.Drive/Storage/FileController.cs @@ -303,6 +303,7 @@ public class FileController( .Where(e => e.IsMarkedRecycle == recycled) .Where(e => e.AccountId == accountId) .Include(e => e.Pool) + .Include(e => e.Object) .AsQueryable(); if (pool.HasValue) filesQuery = filesQuery.Where(e => e.PoolId == pool); diff --git a/DysonNetwork.Drive/Storage/FileUploadController.cs b/DysonNetwork.Drive/Storage/FileUploadController.cs index 76509c9c..96b3c898 100644 --- a/DysonNetwork.Drive/Storage/FileUploadController.cs +++ b/DysonNetwork.Drive/Storage/FileUploadController.cs @@ -64,7 +64,10 @@ public class FileUploadController( var accountId = Guid.Parse(currentUser.Id); // Check if a file with the same hash already exists - var existingFile = await db.Files.FirstOrDefaultAsync(f => f.Hash == request.Hash); + var existingFile = await db.Files + .Include(f => f.Object) + .Where(f => f.Object != null && f.Object.Hash == request.Hash) + .FirstOrDefaultAsync(); if (existingFile != null) { // Create the file index if a path is provided, even for existing files diff --git a/DysonNetwork.Shared/Models/CloudFile.cs b/DysonNetwork.Shared/Models/CloudFile.cs index 625cba78..3f537062 100644 --- a/DysonNetwork.Shared/Models/CloudFile.cs +++ b/DysonNetwork.Shared/Models/CloudFile.cs @@ -20,14 +20,14 @@ public class SnCloudFile : ModelBase, ICloudFile, IIdentifiedResource [NotMapped] [Column(TypeName = "jsonb")] - public Dictionary FileMeta => Object!.Meta ?? []; - [NotMapped] [MaxLength(256)] public string? MimeType => Object!.MimeType; - [NotMapped] [MaxLength(256)] public string? Hash => Object!.Hash; + public Dictionary FileMeta => Object?.Meta ?? []; + [NotMapped] [MaxLength(256)] public string? MimeType => Object?.MimeType; + [NotMapped] [MaxLength(256)] public string? Hash => Object?.Hash; public Instant? ExpiredAt { get; set; } - [NotMapped] public long Size => Object!.Size; + [NotMapped] public long Size => Object?.Size ?? 0; public Instant? UploadedAt { get; set; } - [NotMapped] public bool HasCompression => Object!.HasCompression; - [NotMapped] public bool HasThumbnail => Object!.HasThumbnail; + [NotMapped] public bool HasCompression => Object?.HasCompression ?? false; + [NotMapped] public bool HasThumbnail => Object?.HasThumbnail ?? false; [MaxLength(32)] public string? ObjectId { get; set; } public SnFileObject? Object { get; set; } diff --git a/DysonNetwork.Shared/Models/CloudFileObject.cs b/DysonNetwork.Shared/Models/CloudFileObject.cs index 42d1ac9b..b45cd9d8 100644 --- a/DysonNetwork.Shared/Models/CloudFileObject.cs +++ b/DysonNetwork.Shared/Models/CloudFileObject.cs @@ -1,5 +1,6 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Text.Json.Serialization; namespace DysonNetwork.Shared.Models; @@ -33,7 +34,7 @@ public class SnFileReplica : ModelBase public Guid Id { get; set; } [MaxLength(32)] public string ObjectId { get; set; } - public SnFileObject Object { get; set; } = null!; + [JsonIgnore] public SnFileObject Object { get; set; } = null!; public Guid? PoolId { get; set; } public FilePool? Pool { get; set; } = null!; diff --git a/DysonNetwork.sln.DotSettings.user b/DysonNetwork.sln.DotSettings.user index ab0288e8..38ab4e31 100644 --- a/DysonNetwork.sln.DotSettings.user +++ b/DysonNetwork.sln.DotSettings.user @@ -145,6 +145,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded