From b18a87bdf50c2e287c47798f2053909a354b8047 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 26 Oct 2020 20:42:16 +0000 Subject: [PATCH] --- .../ControllerHelpers/ApiUploadProcessor.cs | 15 +- .../Controllers/AttachmentController.cs | 32 ++-- .../AyaNova/Controllers/ImportController.cs | 160 ++++++++++++++++++ .../AyaNova/Controllers/ReportController.cs | 24 +-- .../Controllers/TranslationController.cs | 24 +-- server/AyaNova/biz/WidgetBiz.cs | 8 +- 6 files changed, 216 insertions(+), 47 deletions(-) create mode 100644 server/AyaNova/Controllers/ImportController.cs diff --git a/server/AyaNova/ControllerHelpers/ApiUploadProcessor.cs b/server/AyaNova/ControllerHelpers/ApiUploadProcessor.cs index 91965fc4..71dfeeac 100644 --- a/server/AyaNova/ControllerHelpers/ApiUploadProcessor.cs +++ b/server/AyaNova/ControllerHelpers/ApiUploadProcessor.cs @@ -24,9 +24,6 @@ namespace AyaNova.Api.ControllerHelpers { - - - /// /// handle upload /// @@ -122,6 +119,16 @@ namespace AyaNova.Api.ControllerHelpers } + public static void DeleteTempUploadFile(ApiUploadedFilesResult uploadFormData) + { + if (uploadFormData.UploadedFiles.Count > 0) + { + foreach (UploadedFileInfo a in uploadFormData.UploadedFiles) + { + System.IO.File.Delete(a.InitialUploadedPathName); + } + } + } private static Encoding GetEncoding(MultipartSection section) { @@ -137,8 +144,6 @@ namespace AyaNova.Api.ControllerHelpers } - - /// /// Contains result of upload form processor /// diff --git a/server/AyaNova/Controllers/AttachmentController.cs b/server/AyaNova/Controllers/AttachmentController.cs index 46411f1c..969a7e26 100644 --- a/server/AyaNova/Controllers/AttachmentController.cs +++ b/server/AyaNova/Controllers/AttachmentController.cs @@ -300,7 +300,7 @@ namespace AyaNova.Api.Controllers if (!Authorized.HasModifyRole(HttpContext.Items, attachToObject.ObjectType)) { //delete temp files - DeleteTempFileUploadDueToBadRequest(uploadFormData); + ApiUploadProcessor.DeleteTempUploadFile(uploadFormData); return StatusCode(403, new ApiNotAuthorizedResponse()); } @@ -312,7 +312,7 @@ namespace AyaNova.Api.Controllers if (badRequest) { //delete temp files - DeleteTempFileUploadDueToBadRequest(uploadFormData); + ApiUploadProcessor.DeleteTempUploadFile(uploadFormData); //return bad request return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, null, errorMessage)); } @@ -364,20 +364,20 @@ namespace AyaNova.Api.Controllers return Ok(ApiOkResponse.Response(ret)); } - /// - /// Utility to delete files that were uploaded but couldn't be stored for some reason, called by Attach route - /// - /// - private static void DeleteTempFileUploadDueToBadRequest(ApiUploadProcessor.ApiUploadedFilesResult uploadFormData) - { - if (uploadFormData.UploadedFiles.Count > 0) - { - foreach (UploadedFileInfo a in uploadFormData.UploadedFiles) - { - System.IO.File.Delete(a.InitialUploadedPathName); - } - } - } + // /// + // /// Utility to delete files that were uploaded but couldn't be stored for some reason, called by Attach route + // /// + // /// + // private static void DeleteTempFileUploadDueToBadRequest(ApiUploadProcessor.ApiUploadedFilesResult uploadFormData) + // { + // if (uploadFormData.UploadedFiles.Count > 0) + // { + // foreach (UploadedFileInfo a in uploadFormData.UploadedFiles) + // { + // System.IO.File.Delete(a.InitialUploadedPathName); + // } + // } + // } /// diff --git a/server/AyaNova/Controllers/ImportController.cs b/server/AyaNova/Controllers/ImportController.cs new file mode 100644 index 00000000..632d0865 --- /dev/null +++ b/server/AyaNova/Controllers/ImportController.cs @@ -0,0 +1,160 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Routing; +using Microsoft.AspNetCore.Authorization; +using Microsoft.Extensions.Logging; +using AyaNova.Models; +using AyaNova.Api.ControllerHelpers; +using AyaNova.Biz; +using AyaNova.Util; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System.IO; +using System.IO.Compression; +using ChoETL; + +namespace AyaNova.Api.Controllers +{ + [ApiController] + [ApiVersion("8.0")] + [Route("api/v{version:apiVersion}/import")] + [Produces("application/json")] + [Authorize] + public class ImportController : ControllerBase + { + private readonly AyContext ct; + private readonly ILogger log; + private readonly ApiServerState serverState; + + /// + /// ctor + /// + /// + /// + /// + public ImportController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) + { + ct = dbcontext; + log = logger; + serverState = apiServerState; + } + + + + + + /// + /// Upload Translation export file + /// Max 15mb total + /// + /// Accepted + [Authorize] + [HttpPost("upload")] + [DisableFormValueModelBinding] + [RequestSizeLimit(15000000)]//currently export file is 200kb * 50 maximum at a time = 15mb https://github.com/aspnet/Announcements/issues/267 + 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 + + if (!serverState.IsOpen) + return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); + + + // AyaTypeId attachToObject = null; + ApiUploadProcessor.ApiUploadedFilesResult uploadFormData = null; + try + { + 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; + string UploadObjectId = string.Empty; + string errorMessage = string.Empty; + string Notes = string.Empty; + List FileData = new List(); + + if ( + !uploadFormData.FormFieldData.ContainsKey("FileData"))//only filedata is required + { + badRequest = true; + errorMessage = "Missing required FormFieldData value: FileData"; + } + if (!badRequest) + { + if (uploadFormData.FormFieldData.ContainsKey("ObjectType")) + UploadObjectType = uploadFormData.FormFieldData["ObjectType"].ToString(); + if (uploadFormData.FormFieldData.ContainsKey("ObjectId")) + UploadObjectId = uploadFormData.FormFieldData["ObjectId"].ToString(); + if (uploadFormData.FormFieldData.ContainsKey("Notes")) + Notes = uploadFormData.FormFieldData["Notes"].ToString(); + //fileData in JSON stringify format which contains the actual last modified dates etc + //"[{\"name\":\"Client.csv\",\"lastModified\":1582822079618},{\"name\":\"wmi4fu06nrs41.jpg\",\"lastModified\":1586900220990}]" + FileData = Newtonsoft.Json.JsonConvert.DeserializeObject>(uploadFormData.FormFieldData["FileData"].ToString()); + + } + + + // long UserId = UserIdFromContext.Id(HttpContext.Items); + //Instantiate the business object handler + TranslationBiz biz = TranslationBiz.GetBiz(ct, HttpContext); + + + //We have our files now can parse and insert into db + if (uploadFormData.UploadedFiles.Count > 0) + { + //deserialize each file and import + foreach (UploadedFileInfo a in uploadFormData.UploadedFiles) + { + JObject o = JObject.Parse(System.IO.File.ReadAllText(a.InitialUploadedPathName)); + if (!await biz.ImportAsync(o)) + { + //delete all the files temporarily uploaded and return bad request + ApiUploadProcessor.DeleteTempUploadFile(uploadFormData); + return BadRequest(new ApiErrorResponse(biz.Errors)); + } + } + } + } + catch (System.IO.InvalidDataException ex) + { + return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, ex.Message)); + } + finally + { + //delete all the files temporarily uploaded and return bad request + + ApiUploadProcessor.DeleteTempUploadFile(uploadFormData); + } + + //Return the list of attachment ids and filenames + return Accepted(); + } + + + // private static void DeleteTempUploadFile(ApiUploadProcessor.ApiUploadedFilesResult uploadFormData) + // { + // if (uploadFormData.UploadedFiles.Count > 0) + // { + // foreach (UploadedFileInfo a in uploadFormData.UploadedFiles) + // { + // System.IO.File.Delete(a.InitialUploadedPathName); + // } + // } + // } + + + + //----------------------------------------- + + + + + + }//eoc +}//eons \ No newline at end of file diff --git a/server/AyaNova/Controllers/ReportController.cs b/server/AyaNova/Controllers/ReportController.cs index d1972cca..898bbdc6 100644 --- a/server/AyaNova/Controllers/ReportController.cs +++ b/server/AyaNova/Controllers/ReportController.cs @@ -365,7 +365,7 @@ namespace AyaNova.Api.Controllers if (!await biz.ImportAsync(o)) { //delete all the files temporarily uploaded and return bad request - DeleteTempUploadFile(uploadFormData); + ApiUploadProcessor.DeleteTempUploadFile(uploadFormData); return BadRequest(new ApiErrorResponse(biz.Errors)); } } @@ -379,23 +379,23 @@ namespace AyaNova.Api.Controllers { //delete all the files temporarily uploaded and return bad request - DeleteTempUploadFile(uploadFormData); + ApiUploadProcessor.DeleteTempUploadFile(uploadFormData); } //Return the list of attachment ids and filenames return Accepted(); } - private static void DeleteTempUploadFile(ApiUploadProcessor.ApiUploadedFilesResult uploadFormData) - { - if (uploadFormData.UploadedFiles.Count > 0) - { - foreach (UploadedFileInfo a in uploadFormData.UploadedFiles) - { - System.IO.File.Delete(a.InitialUploadedPathName); - } - } - } + // private static void DeleteTempUploadFile(ApiUploadProcessor.ApiUploadedFilesResult uploadFormData) + // { + // if (uploadFormData.UploadedFiles.Count > 0) + // { + // foreach (UploadedFileInfo a in uploadFormData.UploadedFiles) + // { + // System.IO.File.Delete(a.InitialUploadedPathName); + // } + // } + // } //----------------------------------------- diff --git a/server/AyaNova/Controllers/TranslationController.cs b/server/AyaNova/Controllers/TranslationController.cs index b08a1c8d..353411df 100644 --- a/server/AyaNova/Controllers/TranslationController.cs +++ b/server/AyaNova/Controllers/TranslationController.cs @@ -363,7 +363,7 @@ namespace AyaNova.Api.Controllers if (!await biz.ImportAsync(o)) { //delete all the files temporarily uploaded and return bad request - DeleteTempUploadFile(uploadFormData); + ApiUploadProcessor.DeleteTempUploadFile(uploadFormData); return BadRequest(new ApiErrorResponse(biz.Errors)); } } @@ -377,7 +377,7 @@ namespace AyaNova.Api.Controllers { //delete all the files temporarily uploaded and return bad request - DeleteTempUploadFile(uploadFormData); + ApiUploadProcessor.DeleteTempUploadFile(uploadFormData); } //Return the list of attachment ids and filenames @@ -385,16 +385,16 @@ namespace AyaNova.Api.Controllers } - private static void DeleteTempUploadFile(ApiUploadProcessor.ApiUploadedFilesResult uploadFormData) - { - if (uploadFormData.UploadedFiles.Count > 0) - { - foreach (UploadedFileInfo a in uploadFormData.UploadedFiles) - { - System.IO.File.Delete(a.InitialUploadedPathName); - } - } - } + // private static void DeleteTempUploadFile(ApiUploadProcessor.ApiUploadedFilesResult uploadFormData) + // { + // if (uploadFormData.UploadedFiles.Count > 0) + // { + // foreach (UploadedFileInfo a in uploadFormData.UploadedFiles) + // { + // System.IO.File.Delete(a.InitialUploadedPathName); + // } + // } + // } #if (DEBUG) diff --git a/server/AyaNova/biz/WidgetBiz.cs b/server/AyaNova/biz/WidgetBiz.cs index 28264310..d8074c2f 100644 --- a/server/AyaNova/biz/WidgetBiz.cs +++ b/server/AyaNova/biz/WidgetBiz.cs @@ -325,9 +325,13 @@ namespace AyaNova.Biz return await GetReportData(idList); } - public Task ImportData(JArray ja) + public async Task ImportData(JArray ja) { - throw new NotImplementedException(); + foreach (JObject j in ja) + { + var w = j.ToObject(); + await CreateAsync(w); + } }