/// <summary> /// Creates a hierarchical <see cref="BlobTree" /> object from the specified directory by traversing it recursively. /// </summary> /// <param name="path">A <see cref="string" /> specifying the path to a directory from which to create the <see cref="BlobTree" />.</param> /// <returns> /// The <see cref="BlobTree" /> this method creates, representing the entire content of the specified directory. /// </returns> public static BlobTree FromDirectory(string path) { Check.ArgumentNull(path, nameof(path)); Check.DirectoryNotFound(path); return(new BlobTree { Root = ReadDirectory(path.TrimEnd('/', '\\')) }); BlobTreeNode ReadDirectory(string directory) { BlobTreeNode node = new BlobTreeNode(Path.GetFileName(directory)); foreach (DirectoryInfo subDirectory in new DirectoryInfo(directory).GetDirectories()) { node.Nodes.Add(ReadDirectory(subDirectory.FullName)); } foreach (FileInfo file in new DirectoryInfo(directory).GetFiles()) { node.Blobs.Add(Blob.FromFile(file.FullName)); } return(node); } }
static void Save(BlobTreeNode node, string nodePath) { Directory.CreateDirectory(nodePath); foreach (BlobTreeNode childNode in node.Nodes) { Save(childNode, Path.Combine(nodePath, childNode.Name)); } node.Blobs.SaveToDirectory(nodePath); }
/// <summary> /// Tries to find a node by the specified path. The path contains each node name, separated by a backslash. Returns <see langword="null" />, if the <see cref="BlobTreeNode" /> could not be found. /// </summary> /// <param name="path">A <see cref="string" /> specifying a path. The path contains each node name, separated by a backslash.</param> /// <param name="ignoreCase"><see langword="true" /> to ignore character casing during name comparison.</param> /// <returns> /// The <see cref="BlobTreeNode" /> that was found by the specified path, or <see langword="null" />, if it could not be found. /// </returns> public BlobTreeNode FindNode(string path, bool ignoreCase) { Check.ArgumentNull(path, nameof(path)); Check.ArgumentEx.StringNotEmpty(path, nameof(path)); BlobTreeNode node = this; foreach (string pathPart in path.Split('\\')) { node = CSharp.Try(() => node.Nodes[pathPart, ignoreCase]); if (node == null) { break; } } return(node); }
static void CheckNames(BlobTreeNode node) { foreach (BlobTreeNode childNode in node.Nodes) { if (!Validate.FileName(childNode.Name)) { throw Throw.InvalidOperation("Blob tree node with the name '" + childNode.Name + "' has illegal filename characters."); } CheckNames(childNode); } Blob blob = node.Blobs.FirstOrDefault(b => !Validate.FileName(b.Name)); if (blob != null) { throw BlobCollection.CreateIllegalFilenameException(blob); } }
/// <summary> /// Initializes a new instance of the <see cref="BlobTree" /> class. /// </summary> public BlobTree() { Root = new BlobTreeNode(); }