This commit is contained in:
@@ -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;
|
||||||
@@ -386,56 +387,65 @@ namespace AyaNova.Api.Controllers
|
|||||||
//fileData in JSON stringify format which contains the actual last modified dates etc
|
//fileData in JSON stringify format which contains the actual last modified dates etc
|
||||||
//"[{\"name\":\"Client.csv\",\"lastModified\":1582822079618},{\"name\":\"wmi4fu06nrs41.jpg\",\"lastModified\":1586900220990}]"
|
//"[{\"name\":\"Client.csv\",\"lastModified\":1582822079618},{\"name\":\"wmi4fu06nrs41.jpg\",\"lastModified\":1586900220990}]"
|
||||||
FileData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<UploadFileData>>(uploadFormData.FormFieldData["FileData"].ToString());
|
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)
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (System.IO.InvalidDataException ex)
|
catch (System.IO.InvalidDataException ex)
|
||||||
{
|
{
|
||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user