diff --git a/server/AyaNova/Controllers/ImportController.cs b/server/AyaNova/Controllers/ImportController.cs index d0f73d65..0985ab5a 100644 --- a/server/AyaNova/Controllers/ImportController.cs +++ b/server/AyaNova/Controllers/ImportController.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; @@ -66,7 +67,7 @@ namespace AyaNova.Api.Controllers return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, $"Expected a multipart request, but got {Request.ContentType}")); //Save uploads to disk under temporary file names until we decide how to handle them - uploadFormData = await ApiUploadProcessor.ProcessUploadAsync(HttpContext); + // uploadFormData = await ApiUploadProcessor.ProcessUploadAsync(HttpContext);xx string UploadObjectType = string.Empty; @@ -75,6 +76,26 @@ namespace AyaNova.Api.Controllers string Notes = string.Empty; List FileData = new List(); + //Save uploads to disk under temporary file names until we decide how to handle them + uploadFormData = await ApiUploadProcessor.ProcessUploadAsync(HttpContext); + if (!string.IsNullOrWhiteSpace(uploadFormData.Error)) + { + errorMessage = uploadFormData.Error; + //delete temp files + ApiUploadProcessor.DeleteTempUploadFile(uploadFormData); + //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), AyaNova.Util.FileUtil.GetBytesReadable(AyaNova.Util.ServerBootConfig.MAX_IMPORT_FILE_UPLOAD_BYTES)))); + } + else//not too big, something else + return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, null, errorMessage)); + } + if (!uploadFormData.FormFieldData.ContainsKey("FileData"))//only filedata is required return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, "Missing required FormFieldData value: FileData")); diff --git a/server/AyaNova/Controllers/ReportController.cs b/server/AyaNova/Controllers/ReportController.cs index 1e54c136..f2a53f4d 100644 --- a/server/AyaNova/Controllers/ReportController.cs +++ b/server/AyaNova/Controllers/ReportController.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Threading.Tasks; +using System; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Routing; @@ -194,7 +195,7 @@ namespace AyaNova.Api.Controllers ReportBiz biz = ReportBiz.GetBiz(ct, HttpContext); if (!ModelState.IsValid) return BadRequest(new ApiErrorResponse(ModelState)); - + var reportData = await biz.GetReportData(selectedRequest); if (reportData == null) @@ -321,7 +322,7 @@ namespace AyaNova.Api.Controllers [Authorize] [HttpPost("upload")] [DisableFormValueModelBinding] - [RequestSizeLimit(15000000)]//currently the largest v7 export for a report template is 828kb, I'm guessing 15mb is more than enough + [RequestSizeLimit(AyaNova.Util.ServerBootConfig.MAX_REPORT_TEMPLATE_UPLOAD_BYTES)] public async Task UploadAsync() { //Adapted from the example found here: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads#uploading-large-files-with-streaming @@ -337,10 +338,28 @@ 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}")); + //Save uploads to disk under temporary file names until we decide how to handle them + // uploadFormData = await ApiUploadProcessor.ProcessUploadAsync(HttpContext); + //Save uploads to disk under temporary file names until we decide how to handle them uploadFormData = await ApiUploadProcessor.ProcessUploadAsync(HttpContext); + if (!string.IsNullOrWhiteSpace(uploadFormData.Error)) + { - + //delete temp files + ApiUploadProcessor.DeleteTempUploadFile(uploadFormData); + //file too large is most likely issue so in that case return this localized properly + if (uploadFormData.Error.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), AyaNova.Util.FileUtil.GetBytesReadable(AyaNova.Util.ServerBootConfig.MAX_REPORT_TEMPLATE_UPLOAD_BYTES)))); + } + else//not too big, something else + return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, null, uploadFormData.Error)); + } List FileData = new List(); diff --git a/server/AyaNova/Controllers/TranslationController.cs b/server/AyaNova/Controllers/TranslationController.cs index 385ca31e..3f60c6bf 100644 --- a/server/AyaNova/Controllers/TranslationController.cs +++ b/server/AyaNova/Controllers/TranslationController.cs @@ -318,7 +318,7 @@ namespace AyaNova.Api.Controllers [Authorize] [HttpPost("upload")] [DisableFormValueModelBinding] - [RequestSizeLimit(15000000)]//currently export file is 200kb * 50 maximum at a time = 15mb https://github.com/aspnet/Announcements/issues/267 + [RequestSizeLimit(AyaNova.Util.ServerBootConfig.MAX_TRANSLATION_UPLOAD_BYTES)] public async Task UploadAsync() { //Adapted from the example found here: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads#uploading-large-files-with-streaming @@ -334,8 +334,6 @@ 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}")); - //Save uploads to disk under temporary file names until we decide how to handle them - uploadFormData = await ApiUploadProcessor.ProcessUploadAsync(HttpContext); bool badRequest = false; string UploadObjectType = string.Empty; @@ -344,6 +342,27 @@ namespace AyaNova.Api.Controllers string Notes = string.Empty; List FileData = new List(); + //Save uploads to disk under temporary file names until we decide how to handle them + uploadFormData = await ApiUploadProcessor.ProcessUploadAsync(HttpContext); + if (!string.IsNullOrWhiteSpace(uploadFormData.Error)) + { + errorMessage = uploadFormData.Error; + //delete temp files + ApiUploadProcessor.DeleteTempUploadFile(uploadFormData); + //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), AyaNova.Util.FileUtil.GetBytesReadable(AyaNova.Util.ServerBootConfig.MAX_TRANSLATION_UPLOAD_BYTES)))); + } + else//not too big, something else + return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, null, errorMessage)); + } + + if ( !uploadFormData.FormFieldData.ContainsKey("FileData"))//only filedata is required { @@ -469,7 +488,7 @@ namespace AyaNova.Api.Controllers -/// + /// /// Put (UpdateTranslationItemDisplayText) /// Update a list of items with new display text /// diff --git a/server/AyaNova/util/ServerBootConfig.cs b/server/AyaNova/util/ServerBootConfig.cs index aa54c700..ece7ec4d 100644 --- a/server/AyaNova/util/ServerBootConfig.cs +++ b/server/AyaNova/util/ServerBootConfig.cs @@ -16,9 +16,11 @@ namespace AyaNova.Util internal const int FAILED_AUTH_DELAY = 3000;//ms internal const int REPORT_RENDERING_OPERATION_TIMEOUT = 20000;//ms //UPLOAD LIMITS - internal const long MAX_ATTACHMENT_UPLOAD_BYTES = 1048576;//10737418241=10gb in bytes, 1048576 = 1mb for testing - internal const long MAX_LOGO_UPLOAD_BYTES = 512000;//Max 500 KiB total (512000 bytes) - internal const long MAX_IMPORT_FILE_UPLOAD_BYTES = 104857600;//100mib limit + internal const long MAX_ATTACHMENT_UPLOAD_BYTES = 1048576;//10737418241=10GiB in bytes, 1048576 = 1MiB for testing + internal const long MAX_LOGO_UPLOAD_BYTES = 512000;//500KiB limit + internal const long MAX_IMPORT_FILE_UPLOAD_BYTES = 104857600;//100MiB limit + internal const long MAX_REPORT_TEMPLATE_UPLOAD_BYTES = 15728640;//15MiB limit; currently the largest v7 export for a report template is 828kb, I'm guessing 15mb is more than enough + internal const long MAX_TRANSLATION_UPLOAD_BYTES = 15728640;//15MiB limit; currently export file is 200kb * 50 maximum at a time = 15mb //############################################################################################################ //############################