コード例 #1
0
        private NodeEnvelope CreateNodeFromDirectory(DirectoryInfo dirInfo, List <DirectoryInfo> toIndex, bool isRoot)
        {
            var gitUrl        = GetGitUrl(dirInfo);
            var isGoogleDrive = IsGoogleDrive(dirInfo);
            var isLink        = dirInfo.Attributes.HasFlag(FileAttributes.ReparsePoint);
            var isSystem      = dirInfo.Attributes.HasFlag(FileAttributes.System) ||
                                dirInfo.Attributes.HasFlag(FileAttributes.Hidden);
            var node = new NodeEnvelope()
            {
                Header = new NodeHeader()
                {
                    NodeType            = gitUrl != null || isGoogleDrive ? NodeType.Clone : isLink ? NodeType.Link : isSystem ? NodeType.System : NodeType.Directory,
                    FileName            = dirInfo.Name,
                    FullDirectoryPath   = Path.GetDirectoryName(dirInfo.FullName) ?? dirInfo.FullName.Substring(0, dirInfo.FullName.Length - dirInfo.Name.Length),
                    FileNumBytes        = null,
                    LastModifieDateTime = dirInfo.LastWriteTimeUtc,
                    CloneUrl            = gitUrl
                }
            };
            bool isExcluded = IsExcluded(dirInfo);

            if (string.IsNullOrWhiteSpace(gitUrl) && !isLink && !isSystem && !isGoogleDrive && !isExcluded)
            {
                toIndex.Add(dirInfo);
            }
            return(node);
        }
コード例 #2
0
        public void Index()
        {
            if (String.IsNullOrWhiteSpace(_rootPath) || !Directory.Exists(_rootPath))
            {
                throw new ApplicationException($"Root path does not exist {_rootPath}");
            }
            var rootInfo = new DirectoryInfo(Path.GetFullPath(_rootPath));

            List <DirectoryInfo> toIndex = new List <DirectoryInfo>();

            NodeEnvelopes.Add(CreateNodeFromDirectory(rootInfo, toIndex, true));
            while (toIndex.Any())
            {
                var currentDirectory = toIndex[0];
                toIndex.RemoveAt(0);
                IEnumerable <FileSystemInfo> directoryContents = null;
                try
                {
                    directoryContents = currentDirectory.EnumerateFileSystemInfos();
                }
                catch (UnauthorizedAccessException e)
                {
                    continue;
                }
                foreach (var fileOrDirectory in directoryContents)
                {
                    var dirInfo  = fileOrDirectory as DirectoryInfo;
                    var fileInfo = fileOrDirectory as FileInfo;
                    if (dirInfo != null)
                    {
                        var node = CreateNodeFromDirectory(dirInfo, toIndex, false);
                        NodeEnvelopes.Add(node);
                    }
                    else if (fileInfo != null)
                    {
                        if (fileInfo.Attributes.HasFlag(FileAttributes.System) || fileInfo.Attributes.HasFlag(FileAttributes.Hidden) || fileInfo.Extension.Equals(".lnk"))
                        {
                            continue;
                        }
                        var node = new NodeEnvelope()
                        {
                            Header = new NodeHeader()
                            {
                                NodeType            = NodeType.File,
                                FileName            = fileInfo.Name,
                                FullDirectoryPath   = fileInfo.DirectoryName,
                                FileNumBytes        = fileInfo.Length,
                                LastModifieDateTime = fileInfo.LastWriteTimeUtc
                            }
                        };
                        NodeEnvelopes.Add(node);
                    }
                }
            }
        }
コード例 #3
0
        public NodeHierarchy FindSubNode(NodeEnvelope node)
        {
            if (GetFullPath().Length > node.Header.FullDirectoryPath.Length)
            {
                return(this);
            }
            string subPath    = node.Header.FullDirectoryPath.Substring(GetFullPath().Length + 1);
            var    nodeParts  = subPath.Split('\\');
            var    searchNode = this;

            for (int i = 0; i < nodeParts.Length; i++)
            {
                var child = searchNode.Children.FirstOrDefault(a => a.Name == nodeParts[i]);
                if (child != null)
                {
                    searchNode = child;
                }
                else
                {
                    throw new ApplicationException($"Could not find subpath: {subPath}");
                }
            }
            return(searchNode);
        }