From 583876edfca55b72e04f5330b0e3a813cc4c1c6e Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Wed, 29 Apr 2020 15:56:41 +0000 Subject: [PATCH] --- source/Plugins/AyaNova.Plugin.V8/V8.cs | 178 ++++++++++++++--------- source/Plugins/AyaNova.Plugin.V8/util.cs | 61 ++++++++ 2 files changed, 167 insertions(+), 72 deletions(-) diff --git a/source/Plugins/AyaNova.Plugin.V8/V8.cs b/source/Plugins/AyaNova.Plugin.V8/V8.cs index c4e4532..575c509 100644 --- a/source/Plugins/AyaNova.Plugin.V8/V8.cs +++ b/source/Plugins/AyaNova.Plugin.V8/V8.cs @@ -12,6 +12,8 @@ using System.IO.Compression; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Linq; +using System.Net.Http; +using System.Net.Http.Headers; namespace AyaNova.PlugIn.V8 @@ -364,7 +366,8 @@ namespace AyaNova.PlugIn.V8 List tags = new List(); tags.Add(ImportTag); - //skip administrator + //skip administrator user fields + //but do export administrator if (i.ID == User.AdministratorID) continue; User c = User.GetItem(i.ID); @@ -409,6 +412,13 @@ namespace AyaNova.PlugIn.V8 } SetTags(d, tags); + //wiki page + var w = GetWikiContent(new TypeAndID(RootObjectTypes.User, c.ID)); + if (w != null) + { + d.wiki = w; + } + //Custom fields? if (ShouldExportCustom) { @@ -429,6 +439,10 @@ namespace AyaNova.PlugIn.V8 await util.PutAsync("UserOptions/" + RavenId.ToString(), d.ToString()); } + + //Attachments / FILES + await ExportAttachments(new TypeAndID(RootObjectTypes.User, c.ID)); + } @@ -673,7 +687,7 @@ namespace AyaNova.PlugIn.V8 #endregion custom fields #region WIKI page exporter - + //WIKI #region Wikiable objects reference @@ -701,79 +715,13 @@ namespace AyaNova.PlugIn.V8 */ #endregion - private async System.Threading.Tasks.Task ExportWikiPageAndAttachments(TypeAndID tid) + private string GetWikiContent(TypeAndID tid) { - //todo: upload attached files - //todo: return wiki as string - //may not exist if (!WikiPage.HasWiki(tid.ID)) return null; WikiPage w = WikiPage.GetItem(tid); var content = w.GetContentAsString; - AyaFileList fl = AyaFileList.GetList(w.ID); - - if (string.IsNullOrWhiteSpace(content) && fl.Count < 1) return null; - - - - - - // //files - // foreach (AyaFileList.AyaFileListInfo i in fl) - // { - // //WikiFileInfo fi = new WikiFileInfo(); - // // fi.Id = i.LT_O_AyaFile.Value.ToString(); - // // fi.Name = i.LT_O_AyaFile.Display; - // // fi.Size = i.LT_AyaFile_Label_FileSize; - // // fi.Creator = i.LT_Common_Label_Creator.Display; - // // fi.Created = i.LT_Common_Label_Created.ToString(); - // // ret.Add(fi); - - - - // //save each file plus file info into their own folder by ayafile id this is because ayafiles can be dupe names and we need the wiki info to fixup the last created date etc - // var filePath = wikiOutputPath + Path.DirectorySeparatorChar + i.LT_O_AyaFile.Value.ToString(); - // makeFolderIfNotExist(filePath);//output/objectype/client.f861ec01-8bde-46e1-9849-fcee9b42f05e/files/d5461ec01-8bde-46e1-9849-fcee9b42f0ff34/ - - // var af = AyaFile.GetItem(i.LT_O_AyaFile.Value); - // if (af == null) continue; - // af.WriteToDisk(filePath, af.Name); - - // var fileInfo = new - // { - // name = i.LT_O_AyaFile.Display, - // created = i.LT_Common_Label_Created, - // creator = i.LT_Common_Label_Creator.Value, - // mimetype = af.mimeType, - // id = af.ID, - // size = af.FileSize, - // ayafiletype = af.FileType, - // rootobjectid = af.RootObjectID, - // rootobjecttype = af.RootObjectType - // }; - - // JsonSerializer serializer = new JsonSerializer(); - // serializer.NullValueHandling = NullValueHandling.Include; - - //#if(DEBUG) - // serializer.Formatting = Formatting.Indented; - //#endif - // JObject jo = JObject.FromObject(fileInfo, serializer); - - - - // using (StreamWriter sw = new StreamWriter(filePath + Path.DirectorySeparatorChar + "meta.json")) - // using (JsonWriter writer = new JsonTextWriter(sw)) - // { - - // // serializer.Serialize(writer, o); - // serializer.Serialize(writer, jo); - // } - - // } - - if (!string.IsNullOrWhiteSpace(content)) return content; @@ -781,9 +729,95 @@ namespace AyaNova.PlugIn.V8 return null; } + + private async System.Threading.Tasks.Task ExportAttachments(TypeAndID tid) + { + if (!WikiPage.HasWiki(tid.ID)) return; + + WikiPage w = WikiPage.GetItem(tid); + AyaFileList fl = AyaFileList.GetList(w.ID); + if (fl.Count == 0) return; + + + //files + foreach (AyaFileList.AyaFileListInfo i in fl) + { + //WikiFileInfo fi = new WikiFileInfo(); + // fi.Id = i.LT_O_AyaFile.Value.ToString(); + // fi.Name = i.LT_O_AyaFile.Display; + // fi.Size = i.LT_AyaFile_Label_FileSize; + // fi.Creator = i.LT_Common_Label_Creator.Display; + // fi.Created = i.LT_Common_Label_Created.ToString(); + // ret.Add(fi); + + + var af = AyaFile.GetItem(i.LT_O_AyaFile.Value); + if (af == null) continue; + + //Upload + + + MultipartFormDataContent formDataContent = new MultipartFormDataContent(); + + //Form data like the bizobject type and id + formDataContent.Add(new StringContent(util.RootObjectToAyaType(tid.RootObjectType).ToString()), name: "AttachToObjectType"); + formDataContent.Add(new StringContent(Map[tid.ID].ToString()), name: "AttachToObjectId"); + //or if testing non-existant this is probably safe: long.MaxValue + + + StreamContent AttachmentFile = new StreamContent(af.GetContent()); + AttachmentFile.Headers.ContentType = new MediaTypeHeaderValue(af.mimeType); + AttachmentFile.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data"); + AttachmentFile.Headers.ContentDisposition.FileName = af.Name; + + var sDate = i.LT_Common_Label_Created.ToString(); + DateTime dtLastModified = DateTime.UtcNow; + if (sDate != null) + { + //parse out to UTC date + DateTime dt = new DateTime(); + if (DateTime.TryParse(sDate, out dt)) + { + dtLastModified = dt.ToUniversalTime(); + } + } + + AttachmentFile.Headers.ContentDisposition.ModificationDate = dtLastModified; + formDataContent.Add(AttachmentFile); + + //create via inventory full test user as attachments use the role of the object attaching to + await util.PostFormDataAsync("Attachment", formDataContent); + + + + + + //af.WriteToDisk(filePath, af.Name); + + //var fileInfo = new + //{ + // name = i.LT_O_AyaFile.Display, + // created = i.LT_Common_Label_Created, + // creator = i.LT_Common_Label_Creator.Value, + // mimetype = af.mimeType, + // id = af.ID, + // size = af.FileSize, + // ayafiletype = af.FileType, + // rootobjectid = af.RootObjectID, + // rootobjecttype = af.RootObjectType + //}; + + + + } + + + + } + #endregion wiki - + #region TAGS private void Tagit(Guid g, List tags) { @@ -808,7 +842,7 @@ namespace AyaNova.PlugIn.V8 - + #region OLD JSON EXPORT STUFF @@ -837,7 +871,7 @@ namespace AyaNova.PlugIn.V8 //} #endregion contract resolver - + //private static string EnsureValidFileName(string fileName) //{ // //make lower and replace spaces with dashes diff --git a/source/Plugins/AyaNova.Plugin.V8/util.cs b/source/Plugins/AyaNova.Plugin.V8/util.cs index be34e2b..d03ede1 100644 --- a/source/Plugins/AyaNova.Plugin.V8/util.cs +++ b/source/Plugins/AyaNova.Plugin.V8/util.cs @@ -8,6 +8,8 @@ using System.Net.Http.Headers; using System.Threading.Tasks; using Newtonsoft.Json.Linq; using System.Security.Cryptography; +using biz = GZTW.AyaNova.BLL; + namespace AyaNova.PlugIn.V8 @@ -159,6 +161,8 @@ namespace AyaNova.PlugIn.V8 return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) }; } + + public async static Task PutAsync(string route, string putJson = null) { @@ -181,6 +185,24 @@ namespace AyaNova.PlugIn.V8 } + public async static Task PostFormDataAsync(string route, MultipartFormDataContent formContent) + { + var requestMessage = new HttpRequestMessage(HttpMethod.Post, route); + if (!string.IsNullOrWhiteSpace(JWT)) + requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JWT); + + requestMessage.Content = formContent; + + HttpResponseMessage response = await client.SendAsync(requestMessage); + var responseAsString = await response.Content.ReadAsStringAsync(); + if (!response.IsSuccessStatusCode) + { + throw new Exception("POST FORMDATA error, route: " + route + "\n" + responseAsString + "\n" + response.ReasonPhrase); + } + else + return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) }; + } + //eoc public class ApiResponse { @@ -304,6 +326,45 @@ namespace AyaNova.PlugIn.V8 } + + public static int RootObjectToAyaType(biz.RootObjectTypes rot) + { + + switch (rot) + { + case biz.RootObjectTypes.User: + return (int)AyaType.User; + default: + throw new NotImplementedException("V8:util:RootObjectToAyaType -> type " + rot.ToString() + " is not coded yet"); + } + } + + public enum AyaType : int + { + + NoType = 0, + Global = 1, + Widget = 2,//attachable, wikiable, reviewable + User = 3,//attachable, wikiable, reviewable + ServerState = 4, + License = 5, + LogFile = 6, + PickListTemplate = 7, + DEPRECATED_REUSELATER_08 = 8, + ServerJob = 9, + AyaNova7Import = 10, + TrialSeeder = 11, + Metrics = 12, + Translation = 13, + UserOptions = 14, + DEPRECATED_REUSELATER_15 = 15, + DEPRECATED_REUSELATER_16 = 16, + FileAttachment = 17, + DataListView = 18, + FormCustom = 19 + } + + #endregion }//eoc