Exemplo n.º 1
0
 /// <summary>
 /// Get all the cloud blob.
 /// </summary>
 /// <param name="prefix">A string containing the blob name prefix.</param>
 /// <returns>The list of cloud blob.</returns>
 public IEnumerable <IListBlobItem> GetBlobs(string prefix)
 {
     return(_cloudBlobClient.ListBlobs(prefix));
 }
Exemplo n.º 2
0
        /// <summary>
        /// Storage2s the json.
        /// </summary>
        /// <returns></returns>
        public async Task <string> Storage2JSON()
        {
            long dirSize = 0;

            //This is how it works with a container
            foreach (var blobItem in _client.ListBlobs(String.Format("{0}/", _userName), true))
            {
                var reference = blobItem.Container.GetBlockBlobReference(blobItem.Uri.AbsoluteUri);

                await reference.FetchAttributesAsync();

                dirSize += reference.Properties.Length;

                var realPath = reference.Uri.LocalPath;
                var temp     = realPath.Substring(1);
                var temp2    = realPath.Substring(temp.IndexOf('/') + 1);

                directory.Add(new DirectoryEntry()
                {
                    CFile = new CloudFile()
                    {
                        ContentType = reference.Properties.ContentType,
                        FileSize    = reference.Properties.Length
                    }
                    ,
                    Path = temp2
                });
            }

            CloudDirectory root = new CloudDirectory();

            root.isRoot = true;
            CloudDirectory lastDir = null;

            root.Name = _userStorage.Name;
            string lastPath = "";

            foreach (var fileitem in directory)
            {
                string fileName = "";
                var    items    = fileitem.Path.Split(new String[] { "/" }, StringSplitOptions.RemoveEmptyEntries);

                if (items.Length == 2)
                {
                    //File is on root level
                    var itemsRoot = items.ToList().GetRange(1, items.Length - 1);
                    fileitem.CFile.FileName = itemsRoot[0];
                    root.CloudFiles.Add(fileitem.CFile);
                    continue;
                }

                var itemsWithoutRootAndFile = items.ToList().GetRange(1, items.Length - 2);

                var pathstring = String.Join("/", itemsWithoutRootAndFile);

                fileName = items.Last();

                if (!lastPath.Equals(pathstring))
                {
                    lastPath = pathstring;

                    bool addedToRoot = false;

                    foreach (var dir in itemsWithoutRootAndFile)
                    {
                        if (!addedToRoot)
                        {
                            lastDir = new CloudDirectory()
                            {
                                Name = dir
                            };
                            var inRootDir = root.SubDirectories.Where(rs => rs.Name.Equals(dir)).FirstOrDefault();
                            if (inRootDir == null)
                            {
                                lastDir.Parent = root.Name;
                                root.SubDirectories.Add(lastDir);
                            }
                            else
                            {
                                lastDir = root.SubDirectories.Where(s => s.Name.Equals(dir)).FirstOrDefault();
                            }
                            addedToRoot = true;
                        }
                        else
                        {
                            var nextDir = new CloudDirectory()
                            {
                                Name = dir
                            };
                            var inSubDir = lastDir.SubDirectories.Where(ls => ls.Name.Equals(dir)).FirstOrDefault();
                            if (inSubDir == null)
                            {
                                nextDir.Parent = lastDir.Name;
                                lastDir.SubDirectories.Add(nextDir);
                            }
                            else
                            {
                                nextDir = lastDir.SubDirectories.Where(sub => sub.Name.Equals(dir)).FirstOrDefault();
                            }


                            lastDir = nextDir;
                        }
                    }
                }

                fileitem.CFile.FileName = fileName;
                lastDir.CloudFiles.Add(fileitem.CFile);
                lastPath = pathstring;
            }

            UserDirectory userdir = new UserDirectory();

            userdir.UserName       = _userName;
            userdir.StorageContent = root;


            root.DirectorySizeInBytes       = dirSize;
            root.DirectorySizeHumanReadable = FileHelper.StrFormatByteSize(dirSize);



            Wrapper wp = new Wrapper();

            wp.UserDirectory = root;
            wp.Username      = _userName;

            var ser = JsonConvert.SerializeObject(wp);

            this.directory.Clear();

            return(ser);
        }