示例#1
0
        /***************************************************/

        public static oM.Adapters.File.FSDirectory ReadDirectory(string fullPath, bool inclHidDirs = false, bool inclSysDirs = false, bool includeFolderContent = false)
        {
            // Perform the "Read" = get the System.DirectoryInfo, which will be the basis for our oM.Adapters.Filing.Directory
            DirectoryInfo di = new DirectoryInfo(fullPath);

            // Checks on config
            if (!inclHidDirs && (di.Attributes & FileAttributes.Hidden) > 0)
            {
                return(null);
            }

            if (!inclSysDirs && (di.Attributes & FileAttributes.System) > 0)
            {
                return(null);
            }

            // Checks on FileInfo
            if ((di.Attributes & FileAttributes.Directory) <= 0 && !di.Exists)
            {
                return(null);
            }

            // Convert the FileInfo to our oM.Adapters.Filing.File
            oM.Adapters.File.FSDirectory dir = di.ToFiling();

            // Add author data if possible
            AddAuthor(dir);

            if (includeFolderContent)
            {
                AddContent(dir);
            }

            return(dir);
        }
示例#2
0
        /***************************************************/

        private static void AddContent(oM.Adapters.File.FSDirectory retrievedDir)
        {
            string fullPath = retrievedDir.IFullPath();

            var content = new DirectoryInfo(fullPath).GetFiles("*.*");

            retrievedDir.Content.AddRange(content.Cast <IFSInfo>());
        }
示例#3
0
        public static oM.Adapters.File.FSDirectory FSDirectory(oM.Adapters.File.FSDirectory parentDirectory, string directoryName)
        {
            if (Path.HasExtension(directoryName))
            {
                BH.Engine.Base.Compute.RecordError($"{nameof(directoryName)} must identify a Directory. Do not include an extension.");
                return(null);
            }

            return(new oM.Adapters.File.FSDirectory()
            {
                ParentDirectory = parentDirectory,
                Name = directoryName,
            });
        }
示例#4
0
        public static oM.Adapters.File.FSDirectory ToFiling(this DirectoryInfo di)
        {
            if (di == null)
            {
                return(null);
            }

            oM.Adapters.File.FSDirectory bd = new oM.Adapters.File.FSDirectory();

            bd.ParentDirectory = di.Parent.ToFiling();
            bd.Name            = di.Name;
            bd.Exists          = di.Exists;
            bd.IsReadOnly      = di.Attributes.HasFlag(FileAttributes.ReadOnly);
            bd.Attributes      = di.Attributes;
            bd.CreationTimeUtc = di.CreationTimeUtc;
            bd.ModifiedTimeUtc = di.LastWriteTimeUtc;

            return(bd);
        }
示例#5
0
        /***************************************************/
        /**** Public Methods                            ****/
        /***************************************************/

        public IFSContainer CreateDirectory(FSDirectory dir, PushType pushType, PushConfig pushConfig)
        {
            List <BH.oM.Adapters.File.IFSContainer> createdDirs = new List <oM.Adapters.File.IFSContainer>();

            bool clearfile = pushType == PushType.DeleteThenCreate ? true : false;

            string dirFullPath = dir.IFullPath();
            bool   existed     = System.IO.Directory.Exists(dirFullPath);

            bool directoryCreated = true;

            try
            {
                if (pushType == PushType.DeleteThenCreate) // Deletes and recreates the directory.
                {
                    if (existed)
                    {
                        System.IO.Directory.Delete(dirFullPath, true); // Deletes the directory and all contents. To make things safer, a Warning is exposed in the Push before proceeding.
                    }
                    System.IO.Directory.CreateDirectory(dirFullPath);
                }
                else if (pushType == PushType.CreateOnly || pushType == PushType.CreateNonExisting || pushType == PushType.UpdateOrCreateOnly || pushType == PushType.UpdateOnly)
                {
                    // Create only directories that didn't exist.
                    if (pushType != PushType.UpdateOnly)
                    {
                        if (!existed)
                        {
                            System.IO.Directory.CreateDirectory(dirFullPath);
                        }
                        else
                        {
                            BH.Engine.Base.Compute.RecordNote($"Directory {dirFullPath} was not created as it existed already (Pushtype {pushType.ToString()} was specified).");
                            directoryCreated = false;
                        }
                    }

                    if (dir.Content != null && dir.Content.Any())
                    {
                        for (int i = 0; i < dir.Content.Count; i++)
                        {
                            ILocatableResource item = (dir.Content[i] as ILocatableResource).DeepClone();
                            if (item == null)
                            {
                                BH.Engine.Base.Compute.RecordWarning($"Cannot push Directory content {dir.Content[i].GetType().Name}.");
                            }

                            string itemFullPath = item.IFullPath();
                            if (string.IsNullOrWhiteSpace(itemFullPath) && !string.IsNullOrWhiteSpace(item.Name))
                            {
                                itemFullPath  = Path.Combine(dirFullPath, item.Name); // Default to Container Directory path.
                                item.Location = Path.GetDirectoryName(itemFullPath);
                            }

                            if (item.Location == dirFullPath)
                            {
                                Create(item, pushType, pushConfig);
                            }
                            else
                            {
                                BH.Engine.Base.Compute.RecordWarning($"The content of the Directory {dirFullPath} can't be Pushed because the content Path {itemFullPath} does not match the container Directory path.");
                            }
                        }
                    }
                }
                else
                {
                    BH.Engine.Base.Compute.RecordWarning($"The specified Pushtype of {pushType.ToString()} is not supported for {nameof(BH.oM.Adapters.File.FSDirectory)} objects.");
                    directoryCreated = false;
                }
            }
            catch (Exception e)
            {
                BH.Engine.Base.Compute.RecordError(e.Message);
            }

            if (directoryCreated || existed)
            {
                System.IO.DirectoryInfo      dirInfo    = new System.IO.DirectoryInfo(dirFullPath);
                oM.Adapters.File.FSDirectory createdDir = dirInfo.ToFiling();

                return(createdDir);
            }

            BH.Engine.Base.Compute.RecordError($"Could not create the Directory {dir.ToString()}.");
            return(null);
        }
示例#6
0
        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;
                    }
                }
            }
        }
示例#7
0
 public static IFSContainer ChangeDirectory(this oM.Adapters.File.IFSContainer fileOrDir, oM.Adapters.File.FSDirectory to)
 {
     fileOrDir = BH.Engine.Base.Query.ShallowClone(fileOrDir);
     fileOrDir.ParentDirectory = to;
     return(fileOrDir);
 }
示例#8
0
 public static DirectoryInfo FromFiling(this oM.Adapters.File.FSDirectory directory)
 {
     return(new DirectoryInfo(directory.IFullPath()));
 }