This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -12,6 +13,7 @@ using AyaNova.Api.ControllerHelpers;
|
||||
using AyaNova.Biz;
|
||||
using AyaNova.Util;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace AyaNova.Api.Controllers
|
||||
{
|
||||
@@ -260,7 +262,7 @@ namespace AyaNova.Api.Controllers
|
||||
{
|
||||
await Task.Delay(ServerBootConfig.FAILED_AUTH_DELAY);//DOS protection
|
||||
return StatusCode(401, new ApiErrorResponse(ApiErrorCode.AUTHENTICATION_FAILED));
|
||||
}
|
||||
}
|
||||
|
||||
if (!FileUtil.TemporaryFileExists(fileName))
|
||||
{
|
||||
@@ -305,24 +307,110 @@ namespace AyaNova.Api.Controllers
|
||||
var asText = Newtonsoft.Json.JsonConvert.SerializeObject(
|
||||
o,
|
||||
Newtonsoft.Json.Formatting.None,
|
||||
new JsonSerializerSettings { ContractResolver = new AyaNova.Util.JsonUtil.ShouldSerializeContractResolver(new string[] { "Concurrency", "Id", "ReportId" }) });
|
||||
new JsonSerializerSettings { ContractResolver = new AyaNova.Util.JsonUtil.ShouldSerializeContractResolver(new string[] { "Concurrency", "Id" }) });
|
||||
var bytes = System.Text.Encoding.UTF8.GetBytes(asText);
|
||||
var file = new FileContentResult(bytes, "application/octet-stream");
|
||||
file.FileDownloadName = Util.FileUtil.StringToSafeFileName(o.Name) + ".json";
|
||||
file.FileDownloadName = Util.FileUtil.StringToSafeFileName(o.Name) + ".ayrt";
|
||||
return file;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Upload Reprot template export file
|
||||
/// Max 15mb total
|
||||
/// </summary>
|
||||
/// <returns>Accepted</returns>
|
||||
[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
|
||||
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
|
||||
|
||||
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);
|
||||
|
||||
|
||||
|
||||
List<UploadFileData> FileData = new List<UploadFileData>();
|
||||
|
||||
if (!uploadFormData.FormFieldData.ContainsKey("FileData"))//only filedata is required
|
||||
{
|
||||
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, "Missing required FormFieldData value: FileData"));
|
||||
}
|
||||
|
||||
//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<List<UploadFileData>>(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
|
||||
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
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
//-----------------------------------------
|
||||
|
||||
|
||||
|
||||
//------------
|
||||
/*
|
||||
NOTES/TODO during testing
|
||||
|
||||
Need job to automatically erase any temp files older than 5 minutes (or whatever)
|
||||
|
||||
*/
|
||||
|
||||
}//eoc
|
||||
}//eons
|
||||
Reference in New Issue
Block a user