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) if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); 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 try
{ {
if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType)) if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, "FileUploadAttempt", $"Expected a multipart request, but got {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; bool badRequest = false;
string UploadObjectType = string.Empty; string UploadObjectType = string.Empty;
@@ -390,39 +391,32 @@ namespace AyaNova.Api.Controllers
} }
long UserId = UserIdFromContext.Id(HttpContext.Items); // long UserId = UserIdFromContext.Id(HttpContext.Items);
//Instantiate the business object handler
TranslationBiz biz = TranslationBiz.GetBiz(ct, HttpContext);
//We have our files and a confirmed AyObject, ready to attach and save permanently
//We have our files now can parse and insert into db
if (uploadFormData.UploadedFiles.Count > 0) if (uploadFormData.UploadedFiles.Count > 0)
{ {
//deserialize the file
foreach (UploadedFileInfo a in uploadFormData.UploadedFiles) foreach (UploadedFileInfo a in uploadFormData.UploadedFiles)
{ {
//Get the actual date from the separate filedata if (!await biz.ImportAsync(ImportJson))
//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) //delete all the files temporarily uploaded and return bad request
{
if (f.lastModified > 0) DeleteTempUploadFile(uploadFormData);
{ return BadRequest(new ApiErrorResponse(biz.Errors));
theDate = DateTimeOffset.FromUnixTimeMilliseconds(f.lastModified).DateTime;
}
}
} }
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); 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);
} }
} }
} }
@@ -430,12 +424,28 @@ namespace AyaNova.Api.Controllers
{ {
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, "FileUploadAttempt", ex.Message)); 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 the list of attachment ids and filenames
return Accepted(); 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) #if (DEBUG)