From 31d4a361f8f0ad49bcd6ebbc81f20040ae94e7d0 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Sat, 25 Apr 2020 15:08:04 +0000 Subject: [PATCH] --- .../Controllers/AttachmentController.cs | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/server/AyaNova/Controllers/AttachmentController.cs b/server/AyaNova/Controllers/AttachmentController.cs index 813aed9e..81653f1b 100644 --- a/server/AyaNova/Controllers/AttachmentController.cs +++ b/server/AyaNova/Controllers/AttachmentController.cs @@ -13,6 +13,7 @@ using AyaNova.Api.ControllerHelpers; using AyaNova.Util; using AyaNova.Biz; using System.Linq; +using System.Collections.Generic; namespace AyaNova.Api.Controllers { @@ -133,7 +134,7 @@ namespace AyaNova.Api.Controllers } //Normallyh wouldn't return a whole list but in this case the UI demands it because of reactivity issues - var ret = await GetFileListForObjectAsync(dbObj.AttachToObjectType, dbObj.AttachToObjectId); + var ret = await GetFileListForObjectAsync(dbObj.AttachToObjectType, dbObj.AttachToObjectId); return Ok(ApiOkResponse.Response(ret, true)); } @@ -163,6 +164,13 @@ 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; } + } + /// /// Upload attachment file /// @@ -196,6 +204,7 @@ namespace AyaNova.Api.Controllers string AttachToObjectId = string.Empty; string errorMessage = string.Empty; string Notes = string.Empty; + List FileData = new List(); if (!uploadFormData.FormFieldData.ContainsKey("AttachToObjectType") || !uploadFormData.FormFieldData.ContainsKey("AttachToObjectId")) { @@ -207,6 +216,10 @@ namespace AyaNova.Api.Controllers AttachToObjectType = uploadFormData.FormFieldData["AttachToObjectType"].ToString(); AttachToObjectId = uploadFormData.FormFieldData["AttachToObjectId"].ToString(); 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>(uploadFormData.FormFieldData["FileData"].ToString()); + if (string.IsNullOrWhiteSpace(AttachToObjectType) || string.IsNullOrWhiteSpace(AttachToObjectId)) { badRequest = true; @@ -275,14 +288,26 @@ namespace AyaNova.Api.Controllers //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) { - var v = await FileUtil.StoreFileAttachmentAsync(a.InitialUploadedPathName, a.MimeType, a.OriginalFileName, a.LastModified, attachToObject, Notes, ct); - // returnList.Add(new NameIdItem() - // { - // Name = v.DisplayFileName, - // Id = v.Id - // }); + //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) + { + 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);