From 740f5ad3fcfbeb394def1c7908b761e5cd831c2f Mon Sep 17 00:00:00 2001
From: LittleSheep <littlesheep.code@hotmail.com>
Date: Mon, 2 Jun 2025 21:22:38 +0800
Subject: [PATCH] :bug: Another bug fix

---
 .../Storage/FileService.ReferenceMigration.cs | 48 +++++++++++--------
 1 file changed, 28 insertions(+), 20 deletions(-)

diff --git a/DysonNetwork.Sphere/Storage/FileService.ReferenceMigration.cs b/DysonNetwork.Sphere/Storage/FileService.ReferenceMigration.cs
index f6b3933..53dfee7 100644
--- a/DysonNetwork.Sphere/Storage/FileService.ReferenceMigration.cs
+++ b/DysonNetwork.Sphere/Storage/FileService.ReferenceMigration.cs
@@ -34,33 +34,41 @@ public class FileReferenceMigrationService(AppDatabase db)
     {
         var posts = await db.Posts
             .Include(p => p.OutdatedAttachments)
+            .Where(p => p.OutdatedAttachments.Any())
             .ToListAsync();
 
         var attachmentsId = posts.SelectMany(p => p.OutdatedAttachments.Select(a => a.Id)).ToList();
-        var attachments = await db.Files.Where(f => attachmentsId.Contains(f.Id)).ToListAsync();
-        var attachmentsDict = attachments
-            .GroupBy(a => posts.First(p => p.OutdatedAttachments.Any(oa => oa.Id == a.Id)).Id)
-            .ToDictionary(g => g.Key, g => g.ToList());
+        var attachments =
+            await db.Files.Where(f => attachmentsId.Contains(f.Id)).ToDictionaryAsync(x => x.Id);
 
-        var fileReferences = posts.SelectMany(post =>
-            attachmentsDict.TryGetValue(post.Id, out var value) 
-                ? value.Select(attachment =>
-                    new CloudFileReference
-                    {
-                        FileId = attachment.Id,
-                        File = attachment,
-                        Usage = "post",
-                        ResourceId = post.Id.ToString(),
-                        CreatedAt = SystemClock.Instance.GetCurrentInstant(),
-                        UpdatedAt = SystemClock.Instance.GetCurrentInstant() 
-                    })
-                : []
-            )
-            .ToList();
+        var fileReferences = posts.SelectMany(post => post.Attachments.Select(attachment =>
+            {
+                var value = attachments.TryGetValue(attachment.Id, out var file) ? file : null;
+                if (value is null) return null;
+                return new CloudFileReference
+                {
+                    FileId = value.Id,
+                    File = value,
+                    Usage = "post",
+                    ResourceId = post.Id.ToString(),
+                    CreatedAt = SystemClock.Instance.GetCurrentInstant(),
+                    UpdatedAt = SystemClock.Instance.GetCurrentInstant()
+                };
+            }).Where(x => x != null).Select(x => x!)
+        ).ToList();
 
         foreach (var post in posts)
         {
-            post.Attachments = post.OutdatedAttachments.Select(a => a.ToReferenceObject()).ToList();
+            var updatedAttachments = new List<CloudFileReferenceObject>();
+            foreach (var attachment in post.OutdatedAttachments)
+            {
+                if (await db.Files.AnyAsync(f => f.Id == attachment.Id))
+                    updatedAttachments.Add(attachment.ToReferenceObject());
+                else
+                    updatedAttachments.Add(attachment.ToReferenceObject());
+            }
+
+            post.Attachments = updatedAttachments;
             db.Posts.Update(post);
         }