public void CanCreateFolder()
 {
     var folder = new StorageFolder("MyFolder", new List<StorageFolder>());
     Assert.AreEqual("MyFolder", folder.FullName);
     Assert.AreEqual("MyFolder", folder.Name);
     Assert.AreEqual(0,folder.Folders.Count());
 }
 public void CanCreateFolderWithMultipleFoldersInName()
 {
     var expectedFolderName = "MyFolder";
     var folder = new StorageFolder("//Some/Folder/MyFolder", new List<StorageFolder>());
     Assert.AreEqual("//Some/Folder/MyFolder", folder.FullName);
     Assert.AreEqual(expectedFolderName, folder.Name);
     Assert.AreEqual(0, folder.Folders.Count());
 }
        /// <inheritdoc/>
        public IEnumerable<StorageFolder> Convert(IEnumerable<StorageObject> objects)
        {
            objects.AssertIsNotNull("objects", "Cannot build folders with a null object collection.");
            
            var folders = new List<StorageFolder>();

            var sortedObjectList = objects.OrderByDescending(o => o.Name.Length).ToList();

            foreach (var obj in sortedObjectList)
            {
                //if the name has any consecutive slashes in the name, skip it.
                if (Regex.IsMatch(obj.FullName, consecutiveSlashRegex))
                {
                    continue;
                }

                //split the input using a forward slash as the folder delimiter, and separate the object name (if we have one) and the folder path.
                var folderParts = obj.FullName.TrimStart('/').Split('/');
                var objectName = folderParts.Last(); //this will be string.empty if the object name ends in a "/" indicating that it's a folder.
                folderParts = folderParts.Take(folderParts.Length - 1).ToArray();

                //if there are no folders in the object's name, skip it.
                if (folderParts.Count() <= 0)
                {
                    continue;
                }

                var currentRoot = folders.FirstOrDefault(f => string.Equals(f.Name, folderParts[0], StringComparison.Ordinal));
                if (currentRoot == null)
                {
                    //if the root folder does not exist, create it.
                    currentRoot = new StorageFolder(folderParts[0], new List<StorageFolder>());
                    folders.Add(currentRoot);
                }

                //go through the rest of the folder path (if any) and add nested folders (if needed).
                var currentPath = folderParts[0];
                foreach (var part in folderParts.Skip(1))
                {
                    currentPath += "/" + part;
                    var newRoot = currentRoot.Folders.FirstOrDefault(f => string.Equals(f.Name, part, StringComparison.Ordinal));
                    if (newRoot == null)
                    {
                        newRoot = new StorageFolder(currentPath, new List<StorageFolder>());
                        currentRoot.Folders.Add(newRoot);
                    }
                    currentRoot = newRoot;
                }

                //if this object is not a folder (e.g. the name does not end in a /), then add this object to the current folder.
                if (!string.IsNullOrEmpty(objectName))
                {
                    currentRoot.Objects.Add(obj);
                }
            }
            return folders;
        }
 public void CanCreateFolderWithTrailingSlashes()
 {
     var expectedFullFolderName = "MyFolder//";
     var expectedFolderName = "MyFolder";
     var folder = new StorageFolder("MyFolder//", new List<StorageFolder>());
     Assert.AreEqual(expectedFullFolderName, folder.FullName);
     Assert.AreEqual(expectedFolderName, folder.Name);
     Assert.AreEqual(0, folder.Folders.Count());
 }
        /// <summary>
        /// Gets the last segment of a large object that was found in the given folder.
        /// </summary>
        /// <param name="segmentFolder">The folder that contains the segments of the large object.</param>
        /// <returns>A key value pair that represents the segment Id and the name of the storage object that represents that segment.</returns>
        internal KeyValuePair<int, string> GetLastSegmentIdAndName(StorageFolder segmentFolder)
        {
            segmentFolder.AssertIsNotNull("segmentFolder","Cannot get segment id or name given a null segment folder.");
            if (segmentFolder.Objects.Count == 0)
            {
                return new KeyValuePair<int, string>(0, string.Empty);
            }

            try
            {
                var segments = segmentFolder.Objects.Select(o => new KeyValuePair<int, string>(GetSegmentIdFromKey(o), o.Name)).ToList();
                segments.Sort((kvp1, kvp2) => kvp1.Key.CompareTo(kvp2.Key));

                return segments.Last();
            }
            catch (System.FormatException)
            {
                //If the segment id in the file is malformed (IE does not conform to the format we expect)
                //Then we cannot get the last segment.
                return new KeyValuePair<int, string>(0, string.Empty);
            }
        }