💥 ♻️ Refactor cloud files' references, and loading system

This commit is contained in:
2025-06-01 19:18:23 +08:00
parent 02ae634690
commit 00229fd406
32 changed files with 5204 additions and 582 deletions

View File

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
using DysonNetwork.Sphere.Post;
using DysonNetwork.Sphere.Storage;
@ -22,10 +23,8 @@ public class Publisher : ModelBase
[MaxLength(256)] public string Nick { get; set; } = string.Empty;
[MaxLength(4096)] public string? Bio { get; set; }
[MaxLength(32)] public string? PictureId { get; set; }
public CloudFile? Picture { get; set; }
[MaxLength(32)] public string? BackgroundId { get; set; }
public CloudFile? Background { get; set; }
[Column(TypeName = "jsonb")] public CloudFileReferenceObject? Picture { get; set; }
[Column(TypeName = "jsonb")] public CloudFileReferenceObject? Background { get; set; }
[JsonIgnore] public ICollection<Post.Post> Posts { get; set; } = new List<Post.Post>();
[JsonIgnore] public ICollection<PostCollection> Collections { get; set; } = new List<PostCollection>();

View File

@ -13,7 +13,7 @@ namespace DysonNetwork.Sphere.Publisher;
[ApiController]
[Route("/publishers")]
public class PublisherController(AppDatabase db, PublisherService ps, FileService fs, ActionLogService als)
public class PublisherController(AppDatabase db, PublisherService ps, FileService fs, FileReferenceService fileRefService, ActionLogService als)
: ControllerBase
{
[HttpGet("{name}")]
@ -356,20 +356,52 @@ public class PublisherController(AppDatabase db, PublisherService ps, FileServic
{
var picture = await db.Files.Where(f => f.Id == request.PictureId).FirstOrDefaultAsync();
if (picture is null) return BadRequest("Invalid picture id.");
if (publisher.Picture is not null) await fs.MarkUsageAsync(publisher.Picture, -1);
publisher.Picture = picture;
await fs.MarkUsageAsync(picture, 1);
var publisherResourceId = $"publisher:{publisher.Id}";
// Remove old references for the publisher picture
if (publisher.Picture is not null) {
var oldPictureRefs = await fileRefService.GetResourceReferencesAsync(publisherResourceId, "publisher.picture");
foreach (var oldRef in oldPictureRefs)
{
await fileRefService.DeleteReferenceAsync(oldRef.Id);
}
}
publisher.Picture = picture.ToReferenceObject();
// Create a new reference
await fileRefService.CreateReferenceAsync(
picture.Id,
"publisher.picture",
publisherResourceId
);
}
if (request.BackgroundId is not null)
{
var background = await db.Files.Where(f => f.Id == request.BackgroundId).FirstOrDefaultAsync();
if (background is null) return BadRequest("Invalid background id.");
if (publisher.Background is not null) await fs.MarkUsageAsync(publisher.Background, -1);
publisher.Background = background;
await fs.MarkUsageAsync(background, 1);
var publisherResourceId = $"publisher:{publisher.Id}";
// Remove old references for the publisher background
if (publisher.Background is not null) {
var oldBackgroundRefs = await fileRefService.GetResourceReferencesAsync(publisherResourceId, "publisher.background");
foreach (var oldRef in oldBackgroundRefs)
{
await fileRefService.DeleteReferenceAsync(oldRef.Id);
}
}
publisher.Background = background.ToReferenceObject();
// Create a new reference
await fileRefService.CreateReferenceAsync(
background.Id,
"publisher.background",
publisherResourceId
);
}
db.Update(publisher);
@ -405,10 +437,10 @@ public class PublisherController(AppDatabase db, PublisherService ps, FileServic
if (member.Role < PublisherMemberRole.Owner)
return StatusCode(403, "You need to be the owner to delete the publisher.");
if (publisher.Picture is not null)
await fs.MarkUsageAsync(publisher.Picture, -1);
if (publisher.Background is not null)
await fs.MarkUsageAsync(publisher.Background, -1);
var publisherResourceId = $"publisher:{publisher.Id}";
// Delete all file references for this publisher
await fileRefService.DeleteResourceReferencesAsync(publisherResourceId);
db.Publishers.Remove(publisher);
await db.SaveChangesAsync();

View File

@ -6,7 +6,7 @@ using NodaTime;
namespace DysonNetwork.Sphere.Publisher;
public class PublisherService(AppDatabase db, FileService fs, ICacheService cache)
public class PublisherService(AppDatabase db, FileService fs, FileReferenceService fileRefService, ICacheService cache)
{
public async Task<Publisher> CreateIndividualPublisher(
Account.Account account,
@ -23,8 +23,8 @@ public class PublisherService(AppDatabase db, FileService fs, ICacheService cach
Name = name ?? account.Name,
Nick = nick ?? account.Nick,
Bio = bio ?? account.Profile.Bio,
Picture = picture ?? account.Profile.Picture,
Background = background ?? account.Profile.Background,
Picture = picture?.ToReferenceObject() ?? account.Profile.Picture,
Background = background?.ToReferenceObject() ?? account.Profile.Background,
AccountId = account.Id,
Members = new List<PublisherMember>
{
@ -40,8 +40,23 @@ public class PublisherService(AppDatabase db, FileService fs, ICacheService cach
db.Publishers.Add(publisher);
await db.SaveChangesAsync();
if (publisher.Picture is not null) await fs.MarkUsageAsync(publisher.Picture, 1);
if (publisher.Background is not null) await fs.MarkUsageAsync(publisher.Background, 1);
var publisherResourceId = $"publisher:{publisher.Id}";
if (publisher.Picture is not null) {
await fileRefService.CreateReferenceAsync(
publisher.Picture.Id,
"publisher.picture",
publisherResourceId
);
}
if (publisher.Background is not null) {
await fileRefService.CreateReferenceAsync(
publisher.Background.Id,
"publisher.background",
publisherResourceId
);
}
return publisher;
}
@ -62,8 +77,8 @@ public class PublisherService(AppDatabase db, FileService fs, ICacheService cach
Name = name ?? realm.Slug,
Nick = nick ?? realm.Name,
Bio = bio ?? realm.Description,
Picture = picture ?? realm.Picture,
Background = background ?? realm.Background,
Picture = picture?.ToReferenceObject() ?? realm.Picture,
Background = background?.ToReferenceObject() ?? realm.Background,
RealmId = realm.Id,
Members = new List<PublisherMember>
{
@ -79,8 +94,23 @@ public class PublisherService(AppDatabase db, FileService fs, ICacheService cach
db.Publishers.Add(publisher);
await db.SaveChangesAsync();
if (publisher.Picture is not null) await fs.MarkUsageAsync(publisher.Picture, 1);
if (publisher.Background is not null) await fs.MarkUsageAsync(publisher.Background, 1);
var publisherResourceId = $"publisher:{publisher.Id}";
if (publisher.Picture is not null) {
await fileRefService.CreateReferenceAsync(
publisher.Picture.Id,
"publisher.picture",
publisherResourceId
);
}
if (publisher.Background is not null) {
await fileRefService.CreateReferenceAsync(
publisher.Background.Id,
"publisher.background",
publisherResourceId
);
}
return publisher;
}