public static byte[] ContentsAsByteArray(this FSFile bhomFile) { byte[] contents = null; try { using (var stream = System.IO.File.OpenRead(bhomFile.IFullPath())) { byte[] buff = new byte[stream.Length]; int read = stream.Read(buff, 0, (int)stream.Length); contents = buff; } } catch { Engine.Base.Compute.RecordError("Unable to read file: " + bhomFile.IFullPath()); } return(contents); }
/***************************************************/ /**** Public Methods ****/ /***************************************************/ public FSFile CreateJson(FSFile file, PushType pushType, PushConfig pushConfig) { string fullPath = file.IFullPath(); bool fileExisted = System.IO.File.Exists(fullPath); // Put together all of the file content. List <string> allLines = new List <string>(); string json = ""; // Process file content, only if there is any. if (file.Content != null && file.Content.Count != 0) { if (file.Content.All(o => o is Dataset)) { pushConfig.UseDatasetSerialization = true; } if (!pushConfig.UseDatasetSerialization) { allLines.AddRange(file.Content.Where(c => c != null).Select(obj => obj.ToJson() + ",")); // Remove the trailing comma if there is only one element. allLines[allLines.Count - 1] = allLines[allLines.Count - 1].Remove(allLines[allLines.Count - 1].Length - 1); // Join all between square brackets to make a valid JSON array. json = String.Join(Environment.NewLine, allLines); json = "[" + json + "]"; } else { // Use the non-JSON compliant "Dataset" serialization style. // This is a newline-separated concatenation of individual JSON-serialized objects. allLines.AddRange(file.Content.Where(c => c != null).Select(obj => obj.ToJson())); json = String.Join(Environment.NewLine, allLines); } if (pushConfig.BeautifyJson) { try { json = BeautifyJson(json); } catch (Exception e) { BH.Engine.Base.Compute.RecordWarning($"Beautify json failed. File will be created with non-beautified json. Error:\n{e.Message}"); } } } bool filecreated = true; try { // Clarify if we are considering the Push in terms of content or of Files. if (string.IsNullOrWhiteSpace(m_defaultFilePath)) // We are talking about Files/Directories. { if (pushType == PushType.DeleteThenCreate) { if (fileExisted) { System.IO.File.Delete(fullPath); } WriteJsonFile(fullPath, json, true); } else if (pushType == PushType.UpdateOnly) { // Overwrite existing file. if (fileExisted) { WriteJsonFile(fullPath, json, true); } else { BH.Engine.Base.Compute.RecordNote($"File {fullPath} was not updated as no file existed at that location."); } } else if (pushType == PushType.UpdateOrCreateOnly) { // Overwrite existing file. If it doesn't exist, create it. WriteJsonFile(fullPath, json, true); } else if (pushType == PushType.CreateOnly || pushType == PushType.CreateNonExisting) { // Create only if file didn't exist. Do not touch existing ones. if (!fileExisted) { WriteJsonFile(fullPath, json, true); } else { BH.Engine.Base.Compute.RecordNote($"File {fullPath} was not created as it existed already (Pushtype {pushType.ToString()} was specified)."); } } else { BH.Engine.Base.Compute.RecordWarning($"The specified Pushtype of {pushType.ToString()} is not supported for .json files."); filecreated = false; } } else // We are talking about File content. { if (pushType == PushType.DeleteThenCreate) { BH.Engine.Base.Compute.RecordNote($"Replacing entire content of file `{fullPath}`."); // Replace all content. WriteJsonFile(fullPath, json, true); } else if (pushType == PushType.CreateOnly || pushType == PushType.CreateNonExisting || pushType == PushType.UpdateOnly || pushType == PushType.UpdateOrCreateOnly) { // Should be refactored to cover distinct use cases for CreateNonExisting, UpdateOnly, UpdateOrCreateOnly if (fileExisted) { BH.Engine.Base.Compute.RecordNote($"Appending content to file `{fullPath}`."); } WriteJsonFile(fullPath, json, false); } else if (pushType == PushType.CreateNonExisting) { // Currently captured by CreateOnly. // The following ideally should be the default behaviour of the IDiffing method. //IEnumerable<object> allReadContent = ReadContent(fullPath); //IEnumerable<IBHoMObject> bHoMObjects_read = allReadContent.OfType<IBHoMObject>(); //IEnumerable<object> genericObjs_read = allReadContent.Except(bHoMObjects_read); //IEnumerable<IBHoMObject> readBhomObjects_hashAssigned = BH.Engine.Diffing.Modify.SetHashFragment(bHoMObjects_read); //IEnumerable<IBHoMObject> bHoMObjects_create = file.Content.OfType<IBHoMObject>(); //IEnumerable<object> genericObjs_create = file.Content.Except(bHoMObjects_create); //Diff diffGeneric = BH.Engine.Diffing.Compute.DiffGenericObjects(genericObjs_read, genericObjs_create, null, true); // Then combine the two diffs in one. // For old objects (= already in file) do not create. // Create only those that are "new". } else if (pushType == PushType.UpdateOnly || pushType == PushType.UpdateOrCreateOnly) { // Currently captured by CreateOnly. See above. // For old objects (= already in file) Update Them. // For those who are "new": create them only if `UpdateOrCreateOnly` is used. } else { BH.Engine.Base.Compute.RecordWarning($"The specified Pushtype of {pushType.ToString()} is not supported for .json files."); filecreated = false; } } } catch (Exception e) { BH.Engine.Base.Compute.RecordError(e.Message); } if (filecreated) { System.IO.FileInfo fileinfo = new System.IO.FileInfo(fullPath); oM.Adapters.File.FSFile createdFile = fileinfo.ToFiling(); createdFile.Content = file.Content; return(createdFile); } BH.Engine.Base.Compute.RecordError($"Could not create {file.ToString()}"); return(null); }