This commit is contained in:
2020-06-25 17:34:46 +00:00
parent 4412b3ca48
commit 2322135bb4

View File

@@ -352,15 +352,16 @@ namespace AyaNova.Api.Controllers
if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
// var returnList = new List<NameIdItem>();
object ret = null;
AyaTypeId attachToObject = null;
// AyaTypeId attachToObject = null;
ApiUploadProcessor.ApiUploadedFilesResult uploadFormData = 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);
//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;
@@ -386,56 +387,65 @@ namespace AyaNova.Api.Controllers
//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
// 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 the file
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 (!await biz.ImportAsync(ImportJson))
{
if (f.name == a.OriginalFileName)
{
if (f.lastModified > 0)
{
theDate = DateTimeOffset.FromUnixTimeMilliseconds(f.lastModified).DateTime;
}
}
//delete all the files temporarily uploaded and return bad request
DeleteTempUploadFile(uploadFormData);
return BadRequest(new ApiErrorResponse(biz.Errors));
}
if (theDate == DateTime.MinValue)
theDate = DateTime.UtcNow;
//import into the db
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));
}
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);
}
}
}
#if (DEBUG)