This commit is contained in:
2021-03-16 17:14:35 +00:00
parent 9236c91ff6
commit 89c411bc28
5 changed files with 115 additions and 91 deletions

View File

@@ -194,25 +194,18 @@ namespace AyaNova.Api.Controllers
//used to hold extra file data sent by client
// public class UploadFileData
// {
// public string name { get; set; }
// public long lastModified { get; set; }
// }
/// <summary>
/// Upload attachment file
///
/// Max 10GiB total
/// Requires same Authorization roles as object that file is being attached to
///
/// </summary>
/// <returns>NameValue list of filenames and attachment id's</returns>
[Authorize]
[HttpPost]
[DisableFormValueModelBinding]
// [RequestSizeLimit(10737418241)]//10737418240 = 10gb https://github.com/aspnet/Announcements/issues/267
[RequestSizeLimit(1048576)]//1048576 = 1mb for testing
[DisableFormValueModelBinding]
[RequestSizeLimit(ServerBootConfig.MAX_ATTACHMENT_UPLOAD_BYTES)]
public async Task<IActionResult> UploadAsync()
{
//Adapted from the example found here: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads#uploading-large-files-with-streaming
@@ -228,7 +221,7 @@ namespace AyaNova.Api.Controllers
if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, $"Expected a multipart request, but got {Request.ContentType}"));
var uploadFormData = await ApiUploadProcessor.ProcessUploadAsync(HttpContext);
bool badRequest = false;
string AttachToObjectType = string.Empty;
@@ -237,10 +230,17 @@ namespace AyaNova.Api.Controllers
string Notes = string.Empty;
List<UploadFileData> FileData = new List<UploadFileData>();
if (
!uploadFormData.FormFieldData.ContainsKey("FileData") ||
!uploadFormData.FormFieldData.ContainsKey("AttachToObjectType") ||
!uploadFormData.FormFieldData.ContainsKey("AttachToObjectId"))
var uploadFormData = await ApiUploadProcessor.ProcessUploadAsync(HttpContext);
if (!string.IsNullOrWhiteSpace(uploadFormData.Error))
{
badRequest = true;
errorMessage = uploadFormData.Error;
}
if (!badRequest
&& (!uploadFormData.FormFieldData.ContainsKey("FileData")
|| !uploadFormData.FormFieldData.ContainsKey("AttachToObjectType")
|| !uploadFormData.FormFieldData.ContainsKey("AttachToObjectId")))
{
badRequest = true;
errorMessage = "Missing one or more required FormFieldData values: AttachToObjectType, AttachToObjectId, FileData";
@@ -314,8 +314,17 @@ namespace AyaNova.Api.Controllers
{
//delete temp files
ApiUploadProcessor.DeleteTempUploadFile(uploadFormData);
//return bad request
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, null, errorMessage));
//file too large is most likely issue so in that case return this localized properly
if (errorMessage.Contains("413"))
{
var TransId = UserTranslationIdFromContext.Id(HttpContext.Items);
return BadRequest(new ApiErrorResponse(
ApiErrorCode.VALIDATION_LENGTH_EXCEEDED,
null,
String.Format(await TranslationBiz.GetTranslationStaticAsync("AyaFileFileTooLarge", TransId, ct), FileUtil.GetBytesReadable(ServerBootConfig.MAX_ATTACHMENT_UPLOAD_BYTES))));
}
else//not too big, something else
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, null, errorMessage));
}
long UserId = UserIdFromContext.Id(HttpContext.Items);