+ return rootNodes;
+ }
+
+ private static void AddParentNodes(Dictionary<string, DirectoryRecord> lookups, List<DirectoryRecord> rootNodes, string[] parts, ObjectInfo item)
+ {
+ for (int i = 0; i < parts.Length - 1; i++)
+ {
+ var nodeName = String.Join("/", parts, 0, i + 1);
+ var storageUri = GetStorageUri(item);
+ var nodeKey = String.Format("{0}/{1}/{2}/{3}", storageUri, item.Account,item.Container, nodeName);
+ //If the node was already addeds, skip
+ if (lookups.ContainsKey(nodeKey))
+ continue;
+ var newNode = new DirectoryRecord
+ {
+ DisplayName = parts[i],
+ ObjectInfo = new ObjectInfo
+ {
+ Account = item.Account,
+ Container = item.Container,
+ Name = nodeName,
+ StorageUri=item.StorageUri,
+ Content_Type = "application/directory"
+ }
+ };
+ AddNode(rootNodes, lookups, newNode);
+ }
+ }
+
+ private static string GetStorageUri(ObjectInfo item)
+ {
+ var storageParts = item.StorageUri.ToString().Split('/');
+ var accountUri = String.Join("/", storageParts, 0, storageParts.Length - 1);
+ return accountUri;
+ }
+
+ private static void AddNode(List<DirectoryRecord> rootNodes, Dictionary<string, DirectoryRecord> lookups, DirectoryRecord newNode)
+ {
+ DirectoryRecord parent;
+ var path = newNode.ObjectInfo.Uri.ToString();
+ var parentPath = GetParentPath(path);
+ lookups[path] = newNode;
+
+ //If the record is a directory, we need to add it no matter what
+
+ //If it is a file, we need to check that its parent node exists
+ //If the parent node doesn't exist, we need to add a node for it.
+ //We need to add nodes for all parents recursively,in case they don't exist
+
+ //Does a parent item exist?
+ if (lookups.TryGetValue(parentPath, out parent))
+ {
+ //If so, add the current item under its parent
+ parent.Directories.Add(newNode);
+ parent.Directories.Sort((x, y) => String.CompareOrdinal(x.Uri.ToString(), y.Uri.ToString()));
+ }
+ else
+ //Otherwise add it to the list of root nodes
+ rootNodes.Add(newNode);
+ }
+
+ private static string GetParentPath(string path)
+ {
+ var lastIndex = path.LastIndexOf("/", StringComparison.Ordinal);
+ if (lastIndex < 0)
+ return null;
+ var parentPath = path.Substring(0, lastIndex);
+ return parentPath;
+ }
+
+ static readonly Regex PascalCaseRegex = new Regex("[a-z][A-Z]", RegexOptions.Compiled);
+ public static string Name(this Enum value)
+ {
+ var name = Enum.GetName(value.GetType(), value);
+ return PascalCaseRegex.Replace(name, m => m.Value[0] + " " + char.ToLower(m.Value[1]));