This commit is contained in:
2020-06-25 17:24:10 +00:00
parent 62a044e92e
commit 4412b3ca48
5 changed files with 124 additions and 50 deletions

View File

@@ -23,36 +23,16 @@ namespace AyaNova.Api.ControllerHelpers
internal static class ApiUploadProcessor
{
/// <summary>
/// Process uploaded attachment file
/// Will be treated as a temporary file for further processing into database
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
internal static async Task<ApiUploadedFilesResult> ProcessAttachmentUploadAsync(Microsoft.AspNetCore.Http.HttpContext httpContext)
{
return await ProcessUploadAsync(httpContext, true);
}
/// <summary>
/// Process uploaded utility file (backup)
/// Anything that will be stored in the backup folder as is
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
internal static async Task<ApiUploadedFilesResult> ProcessUtilityFileUploadAsync(Microsoft.AspNetCore.Http.HttpContext httpContext)
{
return await ProcessUploadAsync(httpContext, false);
}
/// <summary>
/// handle upload
/// </summary>
/// <param name="httpContext"></param>
/// <param name="processAsAttachment"></param>
/// <returns><see cref="ApiUploadedFilesResult"/> list of files and form field data (if present)</returns>
private static async Task<ApiUploadedFilesResult> ProcessUploadAsync(Microsoft.AspNetCore.Http.HttpContext httpContext, bool processAsAttachment)
internal static async Task<ApiUploadedFilesResult> ProcessUploadAsync(Microsoft.AspNetCore.Http.HttpContext httpContext)
{
ApiUploadedFilesResult result = new ApiUploadedFilesResult();
@@ -69,7 +49,6 @@ namespace AyaNova.Api.ControllerHelpers
var section = await reader.ReadNextSectionAsync();
while (section != null)
{
ContentDispositionHeaderValue contentDisposition;
@@ -83,19 +62,8 @@ namespace AyaNova.Api.ControllerHelpers
string filePathAndName = string.Empty;
var CleanedUploadFileName = contentDisposition.FileName.Value.Replace("\"", "");
if (processAsAttachment)
{
//get temp file path and temp file name
filePathAndName = FileUtil.NewRandomAttachmentFileName;
}
else
{
//store directly into the backup file folder
//NOTE: all utility files are always stored as lowercase to avoid recognition issues down the road
CleanedUploadFileName = CleanedUploadFileName.ToLowerInvariant();
filePathAndName = FileUtil.GetFullPathForUtilityFile(CleanedUploadFileName);
}
//get temp file path and temp file name
filePathAndName = FileUtil.NewRandomUserFilesFolderFileName;
//save to disk
using (var stream = new FileStream(filePathAndName, FileMode.Create))

View File

@@ -165,11 +165,11 @@ namespace AyaNova.Api.Controllers
//used to hold extra file data sent by client
public class fileData
{
public string name { get; set; }
public long lastModified { get; set; }
}
// public class UploadFileData
// {
// public string name { get; set; }
// public long lastModified { get; set; }
// }
/// <summary>
/// Upload attachment file
@@ -197,14 +197,14 @@ namespace AyaNova.Api.Controllers
if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, "FileUploadAttempt", $"Expected a multipart request, but got {Request.ContentType}"));
var uploadFormData = await ApiUploadProcessor.ProcessAttachmentUploadAsync(HttpContext);
var uploadFormData = await ApiUploadProcessor.ProcessUploadAsync(HttpContext);
bool badRequest = false;
string AttachToObjectType = string.Empty;
string AttachToObjectId = string.Empty;
string errorMessage = string.Empty;
string Notes = string.Empty;
List<fileData> FileData = new List<fileData>();
List<UploadFileData> FileData = new List<UploadFileData>();
if (
!uploadFormData.FormFieldData.ContainsKey("FileData") ||
@@ -222,7 +222,7 @@ namespace AyaNova.Api.Controllers
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<List<fileData>>(uploadFormData.FormFieldData["FileData"].ToString());
FileData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<UploadFileData>>(uploadFormData.FormFieldData["FileData"].ToString());
if (string.IsNullOrWhiteSpace(AttachToObjectType) || string.IsNullOrWhiteSpace(AttachToObjectId))
{
@@ -298,7 +298,7 @@ namespace AyaNova.Api.Controllers
//Get the actual date from the separate filedata
//this is because the lastModified date is always empty in the form data files
DateTime theDate = DateTime.MinValue;
foreach (fileData f in FileData)
foreach (UploadFileData f in FileData)
{
if (f.name == a.OriginalFileName)
{

View File

@@ -15,6 +15,7 @@ using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Linq;
using AyaNova.Util;
@@ -304,12 +305,11 @@ namespace AyaNova.Api.Controllers
var asText = Newtonsoft.Json.JsonConvert.SerializeObject(
o,
Newtonsoft.Json.Formatting.None,
new JsonSerializerSettings { ContractResolver = new ShouldSerializeContractResolver(new string[] { "Concurrency", "Id","TranslationId" }) });
new JsonSerializerSettings { ContractResolver = new ShouldSerializeContractResolver(new string[] { "Concurrency", "Id", "TranslationId" }) });
var bytes = System.Text.Encoding.UTF8.GetBytes(asText);
var file = new FileContentResult(bytes, "application/octet-stream");
file.FileDownloadName = Util.FileUtil.StringToSafeFileName(o.Name) + ".json";
return file;
}
@@ -336,6 +336,104 @@ namespace AyaNova.Api.Controllers
/// <summary>
/// Upload Translation export file
/// Max 15mb total
/// </summary>
/// <returns>Accepted</returns>
[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<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));
// var returnList = new List<NameIdItem>();
object ret = null;
AyaTypeId attachToObject = null;
try
{
if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, "FileUploadAttempt", $"Expected a multipart request, but got {Request.ContentType}"));
var 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<UploadFileData> FileData = new List<UploadFileData>();
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<List<UploadFileData>>(uploadFormData.FormFieldData["FileData"].ToString());
}
long UserId = UserIdFromContext.Id(HttpContext.Items);
//We have our files and a confirmed AyObject, ready to attach and save permanently
if (uploadFormData.UploadedFiles.Count > 0)
{
foreach (UploadedFileInfo a in uploadFormData.UploadedFiles)
{
//Get the actual date from the separate filedata
//this is because the lastModified date is always empty in the form data files
DateTime theDate = DateTime.MinValue;
foreach (UploadFileData f in FileData)
{
if (f.name == a.OriginalFileName)
{
if (f.lastModified > 0)
{
theDate = DateTimeOffset.FromUnixTimeMilliseconds(f.lastModified).DateTime;
}
}
}
if (theDate == DateTime.MinValue)
theDate = DateTime.UtcNow;
var v = await FileUtil.StoreFileAttachmentAsync(a.InitialUploadedPathName, a.MimeType, a.OriginalFileName, theDate, attachToObject, Notes, ct);
//EVENT LOG
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, attachToObject.ObjectId, attachToObject.ObjectType, AyaEvent.AttachmentCreate, v.DisplayFileName), ct);
//SEARCH INDEXING
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationIdFromContext.Id(HttpContext.Items), v.Id, AyaType.FileAttachment);
SearchParams.AddText(v.Notes).AddText(v.DisplayFileName);
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
}
}
}
catch (System.IO.InvalidDataException ex)
{
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, "FileUploadAttempt", ex.Message));
}
//Return the list of attachment ids and filenames
return Accepted();
}

View File

@@ -0,0 +1,8 @@
namespace AyaNova.Models
{
public class UploadFileData
{
public string name { get; set; }
public long lastModified { get; set; }
}
}

View File

@@ -331,7 +331,7 @@ namespace AyaNova.Util
/// Get a random file name with path to attachments folder
/// </summary>
/// <returns></returns>
internal static string NewRandomAttachmentFileName
internal static string NewRandomUserFilesFolderFileName
{
get
{