From 1e9ecde554253559eb5a9d209d9d3aad1cc03cf6 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Thu, 24 Mar 2022 16:43:35 +0000 Subject: [PATCH] --- .vscode/launch.json | 2 +- server/AyaNova/AyaNova.csproj | 1 - .../AyaNova/Controllers/ExportController.cs | 56 +++++-------------- 3 files changed, 15 insertions(+), 44 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 7c905164..2c3525c1 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -48,7 +48,7 @@ "AYANOVA_DATA_PATH": "c:\\temp\\ravendata", "AYANOVA_USE_URLS": "http://*:7575;", //"AYANOVA_PERMANENTLY_ERASE_DATABASE":"true", - "AYANOVA_SERVER_TEST_MODE": "true", + "AYANOVA_SERVER_TEST_MODE": "false", "AYANOVA_SERVER_TEST_MODE_TZ_OFFSET": "-8", //"AYANOVA_REPORT_RENDERING_TIMEOUT":"1", "AYANOVA_SERVER_TEST_MODE_SEEDLEVEL": "small", diff --git a/server/AyaNova/AyaNova.csproj b/server/AyaNova/AyaNova.csproj index b7645c96..13149b5c 100644 --- a/server/AyaNova/AyaNova.csproj +++ b/server/AyaNova/AyaNova.csproj @@ -18,7 +18,6 @@ - diff --git a/server/AyaNova/Controllers/ExportController.cs b/server/AyaNova/Controllers/ExportController.cs index 7d1a427b..23c367f7 100644 --- a/server/AyaNova/Controllers/ExportController.cs +++ b/server/AyaNova/Controllers/ExportController.cs @@ -12,8 +12,6 @@ using AyaNova.Util; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.IO; -using System.IO.Compression; -using ChoETL; using System; namespace AyaNova.Api.Controllers @@ -46,11 +44,10 @@ namespace AyaNova.Api.Controllers /// /// Export to file /// - /// Valid values are: "csv","json" /// /// downloadable export file name - [HttpPost("render/{format}")] - public async Task RenderExport([FromRoute] string format, [FromBody] DataListSelectedRequest selectedRequest) + [HttpPost("render")] + public async Task RenderExport([FromBody] DataListSelectedRequest selectedRequest) { if (!serverState.IsOpen) @@ -64,12 +61,6 @@ namespace AyaNova.Api.Controllers if (!Authorized.HasReadFullRole(HttpContext.Items, selectedRequest.AType)) return StatusCode(403, new ApiNotAuthorizedResponse()); - if (string.IsNullOrWhiteSpace(format)) - return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_REQUIRED, null, "format required")); - - if (format != "csv" && format != "json") - return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, null, "format not valid, must be 'csv' or 'json'")); - var UserId = UserIdFromContext.Id(HttpContext.Items); var UserRoles = UserRolesFromContext.Roles(HttpContext.Items); @@ -89,48 +80,27 @@ namespace AyaNova.Api.Controllers log.LogDebug($"Instantiating biz object handler for {selectedRequest.AType}"); var biz = BizObjectFactory.GetBizObject(selectedRequest.AType, ct, UserId, UserRoles, UserTranslationId); log.LogDebug($"Fetching data for {selectedRequest.SelectedRowIds.Length} {selectedRequest.AType} items"); - string baseFileName = FileUtil.StringToSafeFileName($"{selectedRequest.AType.ToString().ToLowerInvariant()}-{format}-{FileUtil.GetSafeDateFileName()}"); - string outputSourceFileName = baseFileName + "." + format; + string baseFileName = FileUtil.StringToSafeFileName($"{selectedRequest.AType.ToString().ToLowerInvariant()}-{FileUtil.GetSafeDateFileName()}"); + string outputSourceFileName = baseFileName + ".json"; string outputSourceFullPath = System.IO.Path.Combine(FileUtil.TemporaryFilesFolder, outputSourceFileName); - string outputZipFullpath = System.IO.Path.Combine(FileUtil.TemporaryFilesFolder, baseFileName + ".zip"); - log.LogDebug($"Calling render export data to file {outputZipFullpath}"); + log.LogDebug($"Calling render export data to file {outputSourceFullPath}"); try { - switch (format) + using (StreamWriter file = System.IO.File.CreateText(outputSourceFullPath)) + using (JsonTextWriter writer = new JsonTextWriter(file)) { - case "csv": - using (var w = new ChoCSVWriter(outputSourceFullPath).WithFirstLineHeader().ThrowAndStopOnMissingField(false).WithMaxScanRows(100)) - { - //max scan rows means how many rows it will scan to determine field types so this affects tags because it will scan the first 100 to see the maximum tag count then only ever output that many - var dat = await ((IExportAbleObject)biz).GetExportData(selectedRequest, Guid.Empty);//todo: jobify - w.Write(ToDynamicList(dat)); - } - break; - case "json": - using (StreamWriter file = System.IO.File.CreateText(outputSourceFullPath)) - using (JsonTextWriter writer = new JsonTextWriter(file)) - { - var dat = await ((IExportAbleObject)biz).GetExportData(selectedRequest, Guid.Empty);//todo: jobify - dat.WriteTo(writer); - } - break; - } - - //zip it - using (FileStream fs = new FileStream(outputZipFullpath, FileMode.Create)) - using (ZipArchive arch = new ZipArchive(fs, ZipArchiveMode.Create)) - { - arch.CreateEntryFromFile(outputSourceFullPath, outputSourceFileName); + var dat = await ((IExportAbleObject)biz).GetExportData(selectedRequest, Guid.Empty);//todo: jobify + dat.WriteTo(writer); } log.LogDebug($"Completed, returning results"); - return Ok(ApiOkResponse.Response(baseFileName + ".zip")); + return Ok(ApiOkResponse.Response(outputSourceFileName)); } catch (ReportRenderTimeOutException) { log.LogInformation($"RenderExport timeout data list key: {selectedRequest.DataListKey}, record count:{selectedRequest.SelectedRowIds.LongLength}, user:{UserNameFromContext.Name(HttpContext.Items)} "); - return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, "timeout - select fewer records")); + return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, "timeout - select fewer records")); } } @@ -173,7 +143,9 @@ namespace AyaNova.Api.Controllers } var FilePath = FileUtil.GetFullPathForTemporaryFile(fileName); - return PhysicalFile(FilePath, "application/zip"); + + //including the file name triggers save automatically "attachment" rather than viewing it "inline" + return PhysicalFile(FilePath, "application/json", fileName); }