From f16a472dbef38c42f60636a67ddcdd2f5186f98a Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Mon, 23 Sep 2024 01:45:43 +0200 Subject: [PATCH] [backend/api] Add drive file deletion endpoint --- .../Controllers/Web/DriveController.cs | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Iceshrimp.Backend/Controllers/Web/DriveController.cs b/Iceshrimp.Backend/Controllers/Web/DriveController.cs index 99c6d661..8e23d29a 100644 --- a/Iceshrimp.Backend/Controllers/Web/DriveController.cs +++ b/Iceshrimp.Backend/Controllers/Web/DriveController.cs @@ -4,6 +4,7 @@ using Iceshrimp.Backend.Controllers.Shared.Attributes; using Iceshrimp.Backend.Core.Configuration; using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Middleware; +using Iceshrimp.Backend.Core.Queues; using Iceshrimp.Backend.Core.Services; using Iceshrimp.Shared.Schemas.Web; using Microsoft.AspNetCore.Cors; @@ -20,7 +21,8 @@ public class DriveController( ObjectStorageService objectStorage, IOptionsSnapshot options, ILogger logger, - DriveService driveSvc + DriveService driveSvc, + QueueService queueSvc ) : ControllerBase { [EnableCors("drive")] @@ -164,4 +166,28 @@ public class DriveController( return await GetFileById(id); } + + [HttpDelete("{id}")] + [Authenticate] + [Authorize] + [ProducesResults(HttpStatusCode.Accepted)] + [ProducesErrors(HttpStatusCode.NotFound, HttpStatusCode.UnprocessableEntity)] + public async Task DeleteFile(string id) + { + var user = HttpContext.GetUserOrFail(); + var file = await db.DriveFiles.FirstOrDefaultAsync(p => p.User == user && p.Id == id) ?? + throw GracefulException.NotFound("File not found"); + + if (await db.Users.AnyAsync(p => p.Avatar == file || p.Banner == file)) + throw GracefulException.UnprocessableEntity("Refusing to delete file: used in banner or avatar"); + if (await db.Notes.AnyAsync(p => p.FileIds.Contains(file.Id))) + throw GracefulException.UnprocessableEntity("Refusing to delete file: used in note"); + + await queueSvc.BackgroundTaskQueue.EnqueueAsync(new DriveFileDeleteJobData + { + DriveFileId = file.Id, Expire = false + }); + + return Accepted(); + } } \ No newline at end of file