public IEnumerable <object> Read(FileContentRequest fcr, PullConfig pullConfig) { List <object> output = new List <object>(); string fileFullPath = fcr.File.IFullPath(); oM.Adapters.File.FSFile readFile = ReadFile(fileFullPath, true, pullConfig.IncludeHiddenFiles, pullConfig.IncludeSystemFiles); if (readFile == null) { return(output); } output = readFile.Content ?? new List <object>(); if (!output.Any()) { BH.Engine.Base.Compute.RecordWarning($"No content could be pulled for {fileFullPath}. Make sure it's not protected or empty."); } return(output .Where(o => fcr.Types.Count > 0 ? fcr.Types.Any(t => t == o.GetType()) : true) .Where(o => fcr.FragmentTypes.Count > 0 ? (o as BHoMObject)?.Fragments.Select(f => f.GetType()).Intersect(fcr.FragmentTypes).Any() ?? false : true) .Where(o => fcr.CustomDataKeys.Count > 0 ? (o as BHoMObject)?.CustomData.Keys.Intersect(fcr.CustomDataKeys).Any() ?? false : true)); }
/***************************************************/ private static void ReadAndAddContent(oM.Adapters.File.FSFile retrievedFile) { string fullPath = retrievedFile.IFullPath(); var content = ReadContent(fullPath); retrievedFile.Content.AddRange(content); }
public static oM.Adapters.File.FSFile ToFiling(this FileInfo fi) { if (fi == null) { return(null); } oM.Adapters.File.FSFile bf = new oM.Adapters.File.FSFile(); bf.ParentDirectory = fi.Directory.ToFiling(); bf.Name = fi.Name; bf.Exists = fi.Exists; bf.IsReadOnly = fi.IsReadOnly; bf.Size = (int)(fi.Length & 0xFFFFFFFF); bf.Attributes = fi.Attributes; bf.CreationTimeUtc = fi.CreationTimeUtc; bf.ModifiedTimeUtc = fi.LastWriteTimeUtc; return(bf); }
/***************************************************/ public static oM.Adapters.File.FSFile ReadFile(string fullPath, bool inclFileContent = false, bool inclHidFiles = false, bool inclSysFiles = false) { // Perform the "Read" = get the System.FileInfo, which will be the basis for our oM.Adapters.Filing.File FileInfo fi = new FileInfo(fullPath); // Checks on config if (!inclHidFiles && (fi.Attributes & FileAttributes.Hidden) > 0) { return(null); } if (!inclSysFiles && (fi.Attributes & FileAttributes.System) > 0) { return(null); } // Checks on FileInfo if ((fi.Attributes & FileAttributes.Directory) <= 0 && !fi.Exists) { return(null); } // Convert the FileInfo to our oM.Adapters.Filing.File oM.Adapters.File.FSFile file = fi.ToFiling(); // Add author data if possible AddAuthor(file); // Add content data if requested and possible if (inclFileContent) { ReadAndAddContent(file); } return(file); }
public static FileInfo FromFiling(this oM.Adapters.File.FSFile file) { return(new FileInfo(file.IFullPath())); }
private void WalkDirectories(List <FSFile> files, List <FSDirectory> dirs, FileDirRequest fdr, ref int filesCount, ref int dirsCount, bool inclHidFiles = false, bool inclSysFiles = false, WildcardPattern wildcardPattern = null) { // Recursion stop condition. if (fdr.MaxNesting == 0) { return; } // Look in directory and, if requested, recursively in subdirectories. if (string.IsNullOrWhiteSpace(fdr.Location)) { BH.Engine.Base.Compute.RecordError($"Missing parameter {nameof(fdr.Location)} from the request."); return; } System.IO.DirectoryInfo selectedDir = new System.IO.DirectoryInfo(fdr.Location.IFullPath()); System.IO.DirectoryInfo[] dirArray = new System.IO.DirectoryInfo[] { }; // Check if the location points to a single file. // To point to a single file, the location must not be a wildcard (therefore wildcardPattern must be null) // and it must have an extension. bool isSingleFile = Path.HasExtension(selectedDir.FullName) && wildcardPattern == null; if (!isSingleFile) // If the location points to a directory, populate the list of folders there. { dirArray = selectedDir.GetDirectories(); } else // if the location points to a single file, the selected directory is its parent. { selectedDir = new System.IO.DirectoryInfo(Path.GetDirectoryName(selectedDir.FullName)); } foreach (System.IO.DirectoryInfo di in dirArray) { oM.Adapters.File.FSDirectory bhomDir = ReadDirectory(di.FullName, inclHidFiles, inclSysFiles); if (bhomDir == null) { continue; } bhomDir.ParentDirectory = di.Parent.ToFiling(); if (fdr.Exclusions != null && fdr.Exclusions.Contains(bhomDir)) { continue; } if (fdr.IncludeDirectories && wildcardPattern == null) { if (fdr.SortOrder != SortOrder.Default || !MaxItemsReached(fdr.MaxDirectories, dirsCount)) { // The limit in number of item retrieved in WalkDirectories applies only if there is no sortOrder applied. // If a sortOrder is applied, the maxItems must be applied after the sorting is done (outside of WalkDirectories) // Check exclusions if (fdr.Exclusions != null && fdr.Exclusions.Contains(bhomDir)) { continue; } // Check Wildcard matches - DISABLED: only allow wildcards in filename. Too complicated otherwise. //if (!wildcardPattern?.IsMatch(bhomDir.Name) ?? false) // continue; dirs.Add(bhomDir); dirsCount += 1; } } // Recurse if requested, and if the limits are not exceeded. if (fdr.SearchSubdirectories == true && !MaxItemsReached(fdr.MaxFiles, filesCount, fdr.MaxDirectories, dirsCount)) { FileDirRequest fdrRecurse = BH.Engine.Base.Query.ShallowClone(fdr); fdrRecurse.Location = bhomDir.IFullPath(); fdrRecurse.MaxNesting -= 1; WalkDirectories(files, dirs, fdrRecurse, ref filesCount, ref dirsCount, inclHidFiles, inclSysFiles, wildcardPattern); } } if (fdr.IncludeFiles) { System.IO.FileInfo[] fileInfos = new System.IO.FileInfo[1]; try { if (isSingleFile) { fileInfos[0] = new FileInfo(fdr.Location.IFullPath()); } else { fileInfos = selectedDir.GetFiles("*.*"); } } // This is thrown if one of the files requires permissions greater than the application provides. catch (UnauthorizedAccessException e) { // Write out the message and continue. BH.Engine.Base.Compute.RecordNote(e.Message); } foreach (var fi in fileInfos) { if (fdr.SortOrder != SortOrder.Default || !MaxItemsReached(fdr.MaxFiles, filesCount)) { // The limit in number of item retrieved in WalkDirectories applies only if there is no sortOrder applied. // If a sortOrder is applied, the maxItems must be applied after the sorting is done (outside of WalkDirectories) // Check exclusions if (fdr.Exclusions != null && fdr.Exclusions.Contains(fi.ToFiling())) { continue; } // Check Wildcard matches if (!wildcardPattern?.IsMatch(fi.Name) ?? false) { continue; } // When reading the file, do not retrieve content. // Content must be retrieved after WalkDirectories has run. // This is because additional filtering might be done later. oM.Adapters.File.FSFile omFile = ReadFile(fi.FullName, false, inclHidFiles, inclSysFiles); if (omFile != null) { files.Add(omFile); filesCount += 1; } } else { break; } } } }
/***************************************************/ /**** 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); }