From 98686e334e5272375c204882c46b2fc7256a1c07 Mon Sep 17 00:00:00 2001 From: pancakes Date: Mon, 16 Dec 2024 14:41:06 +1000 Subject: [PATCH] [backend/api] Allow optionally storing a drive file in a specified folder --- .../Controllers/Web/DriveController.cs | 4 ++-- Iceshrimp.Backend/Core/Services/DriveService.cs | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Iceshrimp.Backend/Controllers/Web/DriveController.cs b/Iceshrimp.Backend/Controllers/Web/DriveController.cs index 1bd6f718..5000057e 100644 --- a/Iceshrimp.Backend/Controllers/Web/DriveController.cs +++ b/Iceshrimp.Backend/Controllers/Web/DriveController.cs @@ -118,7 +118,7 @@ public class DriveController( [Produces(MediaTypeNames.Application.Json)] [ProducesResults(HttpStatusCode.OK)] [MaxRequestSizeIsMaxUploadSize] - public async Task UploadFile(IFormFile file) + public async Task UploadFile(IFormFile file, [FromQuery] string? folderId) { var user = HttpContext.GetUserOrFail(); var request = new DriveFileCreationRequest @@ -127,7 +127,7 @@ public class DriveController( MimeType = file.ContentType, IsSensitive = false }; - var res = await driveSvc.StoreFileAsync(file.OpenReadStream(), user, request); + var res = await driveSvc.StoreFileAsync(file.OpenReadStream(), user, request, folderId: folderId); return await GetFileById(res.Id); } diff --git a/Iceshrimp.Backend/Core/Services/DriveService.cs b/Iceshrimp.Backend/Core/Services/DriveService.cs index bf024dd9..365a2b36 100644 --- a/Iceshrimp.Backend/Core/Services/DriveService.cs +++ b/Iceshrimp.Backend/Core/Services/DriveService.cs @@ -165,7 +165,7 @@ public class DriveService( } public async Task StoreFileAsync( - Stream input, User user, DriveFileCreationRequest request, bool skipImageProcessing = false + Stream input, User user, DriveFileCreationRequest request, bool skipImageProcessing = false, string? folderId = null ) { if (user.IsLocalUser && input.Length > storageConfig.Value.MaxUploadSizeBytes) @@ -324,6 +324,14 @@ public class DriveService( request.Filename += $".{fmt.Extension}"; } + // If the requested folder doesn't exist then store it in the root folder + if (folderId != null) + { + var folder = await db.DriveFolders + .FirstOrDefaultAsync(p => p.Id == folderId && p.UserId == user.Id); + if (folder == null) folderId = null; + } + file = new DriveFile { Id = IdHelpers.GenerateSnowflakeId(), @@ -351,7 +359,8 @@ public class DriveService( ThumbnailMimeType = thumbnail?.format.Format.MimeType, PublicUrl = @public?.url, PublicAccessKey = @public?.accessKey, - PublicMimeType = @public?.format.Format.MimeType + PublicMimeType = @public?.format.Format.MimeType, + FolderId = folderId }; await db.AddAsync(file);