[backend/api] Allow optionally storing a drive file in a specified folder
This commit is contained in:
parent
673e5c88bc
commit
98686e334e
2 changed files with 13 additions and 4 deletions
|
@ -118,7 +118,7 @@ public class DriveController(
|
|||
[Produces(MediaTypeNames.Application.Json)]
|
||||
[ProducesResults(HttpStatusCode.OK)]
|
||||
[MaxRequestSizeIsMaxUploadSize]
|
||||
public async Task<DriveFileResponse> UploadFile(IFormFile file)
|
||||
public async Task<DriveFileResponse> 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);
|
||||
}
|
||||
|
||||
|
|
|
@ -165,7 +165,7 @@ public class DriveService(
|
|||
}
|
||||
|
||||
public async Task<DriveFile> 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);
|
||||
|
|
Loading…
Add table
Reference in a new issue