diff --git a/AobaCore/Services/AobaService.cs b/AobaCore/Services/AobaService.cs index 0527426..a1c9efb 100644 --- a/AobaCore/Services/AobaService.cs +++ b/AobaCore/Services/AobaService.cs @@ -25,6 +25,11 @@ public class AobaService(IMongoDatabase db) return await _media.Find(m => m.MediaId == id).FirstOrDefaultAsync(cancellationToken); } + public async Task> GetMediaAsync(IEnumerable ids, CancellationToken cancellationToken = default) + { + return await _media.Find(m => ids.Contains(m.MediaId)).ToListAsync(cancellationToken); + } + public async Task> FindMediaAsync(string? query, ObjectId userId, int page = 1, int pageSize = 100, CancellationToken cancellationToken = default) { var filters = new List>() diff --git a/AobaCore/Services/ThumbnailService.cs b/AobaCore/Services/ThumbnailService.cs index d95762c..14620b2 100644 --- a/AobaCore/Services/ThumbnailService.cs +++ b/AobaCore/Services/ThumbnailService.cs @@ -20,6 +20,7 @@ using SixLabors.ImageSharp.Processing; using System; using System.Collections.Generic; using System.Diagnostics; +using System.Drawing; using System.Linq; using System.Net.Http.Headers; using System.Text; @@ -53,6 +54,23 @@ public class ThumbnailService(IMongoDatabase db, AobaService aobaService) return null; } + public async Task DeleteThumbnailDirectAsync(ObjectId thumbnailId) + { + try + { + await _gridfs.DeleteAsync(thumbnailId); + } + catch (GridFSFileNotFoundException) + { + //Ignore if the file was not found (somehow already deleted) + } + catch (Exception e) + { + return new ExceptionError(e); + } + return null; + } + /// /// /// diff --git a/AobaServer/Services/AobaRpcService.cs b/AobaServer/Services/AobaRpcService.cs index cd8e516..40f95a8 100644 --- a/AobaServer/Services/AobaRpcService.cs +++ b/AobaServer/Services/AobaRpcService.cs @@ -13,7 +13,7 @@ using System.Text.Json; namespace AobaServer.Services; -public class AobaRpcService(AobaService aobaService, AccountsService accountsService, AuthConfigService authConfig) : AobaRpc.AobaRpcBase +public class AobaRpcService(AobaService aobaService, ThumbnailService thumbnailService, AccountsService accountsService, AuthConfigService authConfig) : AobaRpc.AobaRpcBase { public override async Task GetMedia(Id request, ServerCallContext context) { @@ -59,13 +59,30 @@ public class AobaRpcService(AobaService aobaService, AccountsService accountsSer public override async Task DeleteMedia(Id request, ServerCallContext context) { - await aobaService.DeleteFileAsync(request.ToObjectId(), context.CancellationToken); + var media = await aobaService.GetMediaAsync(request.ToObjectId()); + if (media == null) + return new Empty(); + await aobaService.DeleteFileAsync(media.MediaId, context.CancellationToken); + foreach (var (_, id) in media.Thumbnails) + { + await thumbnailService.DeleteThumbnailDirectAsync(id); + } return new Empty(); } public override async Task DeleteMediaBulk(IdList request, ServerCallContext context) { + var media = await aobaService.GetMediaAsync(request.ToObjectId(), context.CancellationToken); + if(media.Count == 0) + return new Empty(); await aobaService.DeleteFilesAsync(request.ToObjectId(), context.CancellationToken); + foreach (var item in media) + { + foreach (var (_, id) in item.Thumbnails) + { + await thumbnailService.DeleteThumbnailDirectAsync(id); + } + } return new Empty(); } } \ No newline at end of file diff --git a/AobaServer/Services/DebugService.cs b/AobaServer/Services/DebugService.cs index 4e2ce80..5b8e7e3 100644 --- a/AobaServer/Services/DebugService.cs +++ b/AobaServer/Services/DebugService.cs @@ -8,5 +8,6 @@ public class DebugService(AobaService aobaService, ThumbnailService thumbnailSer { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { + //todo: clean up orphaned thumbnails } }