Esempio n. 1
0
        public Folder CreateFolder(string folderName, string parentId = null)
        {
            string             folderTypeName = this._entityFactory.GetEntityTypeStringName(typeof(Folder));
            var                folder         = this._entityFactory.CreateEntityForType(folderTypeName) as Folder;
            CloudBlobContainer container;

            if (string.IsNullOrWhiteSpace(parentId))
            {
                container = CurrentCloudBlobClient.GetContainerReference(folderName.ToLower());
                container.CreateIfNotExists();
                MapCloudBlobContainer2Folder(container, folder);
            }
            else
            {
                var    path          = Combine(parentId, folderName);
                string containerName = GetContainer(path);
                string prefix        = GetPrefix(path);

                container = CurrentCloudBlobClient.GetContainerReference(containerName);

                if (!container.ListBlobs(prefix).Any())
                {
                    container.GetBlockBlobReference(Combine(prefix, DirectoryPlaceHolder)).UploadText(string.Empty);
                }
                var directory = container.GetDirectoryReference(prefix);

                MapCloudBlobDirectory2Folder(directory, folder);
            }

            ChangeTracker.Attach(folder);
            return(folder);
        }
Esempio n. 2
0
        public byte[] GetImagePreview(string blobKey)
        {
            byte[] result;
            var    cloudBlob = CurrentCloudBlobClient.GetBlobReferenceFromServer(GetUri(blobKey));

            cloudBlob.FetchAttributes();

            if (cloudBlob.Metadata.ContainsKey(ThumbMetadataKey))
            {
                var imageAsString = cloudBlob.Metadata[ThumbMetadataKey];
                result = Convert.FromBase64String(imageAsString);
            }
            else
            {
                using (var dataStream = new MemoryStream())
                {
                    cloudBlob.DownloadToStream(dataStream);
                    result = GenerateThumb(dataStream, 100, 100, 8000);
                    SaveThumb(result, cloudBlob);
                }
            }

            // don't return null
            return(result ?? new byte[1]);
        }
Esempio n. 3
0
        public bool Exists(string blobKey)
        {
            var container = CurrentCloudBlobClient.GetContainerReference(CurrentCloudBlobClient.BaseUri.AbsoluteUri);
            var cloudBlob = container.GetBlockBlobReference(blobKey);

            return(cloudBlob.Exists());
        }
Esempio n. 4
0
        public bool Exists(string blobKey)
        {
            //	CloudBlobContainer container = CurrentCloudBlobClient.GetContainerReference(DefaultBlobContainerName);
            //	ICloudBlob cloudBlob = container.GetBlobReferenceFromServer(blobKey);
            var cloudBlob = CurrentCloudBlobClient.GetBlobReferenceFromServer(GetUri(blobKey));

            return(cloudBlob.Exists());
        }
Esempio n. 5
0
        public void Delete(string id)
        {
            var container   = CurrentCloudBlobClient.GetContainerReference(GetContainer(id));
            var listOfBlobs = container.ListBlobs(GetPrefix(id));

            Parallel.ForEach(listOfBlobs, item =>
            {
                var blob = CurrentCloudBlobClient.GetBlobReferenceFromServer(item.StorageUri);
                blob.Delete();
            });
        }
Esempio n. 6
0
        public virtual Stream OpenReadOnly(string blobKey)
        {
            if (Exists(blobKey))
            {
                //var container = this.CurrentCloudBlobClient.GetContainerReference(DefaultBlobContainerName);
                //var cloudBlob = container.GetBlobReferenceFromServer(blobKey);
                var cloudBlob = CurrentCloudBlobClient.GetBlobReferenceFromServer(GetUri(blobKey));

                var stream = new MemoryStream();
                cloudBlob.DownloadToStream(stream);
                if (stream.CanSeek)
                {
                    stream.Seek(0, SeekOrigin.Begin);
                }
                return(stream);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 7
0
        public void Rename(string id, string name)
        {
            var container = CurrentCloudBlobClient.GetContainerReference(GetContainer(id));
            var prefix    = GetPrefix(id);
            int index     = prefix.LastIndexOf(CurrentCloudBlobClient.DefaultDelimiter, 1, StringComparison.Ordinal);
            var names     = prefix.Split(new [] { CurrentCloudBlobClient.DefaultDelimiter }, StringSplitOptions.RemoveEmptyEntries);

            names[names.Length - 1] = name;
            var newPrefix = names.JoinStrings(CurrentCloudBlobClient.DefaultDelimiter);

            if (prefix.EndsWith(CurrentCloudBlobClient.DefaultDelimiter))
            {
                newPrefix += CurrentCloudBlobClient.DefaultDelimiter;
            }
            var listOfBlobs = container.ListBlobs(prefix);

            Parallel.ForEach(listOfBlobs, item =>
            {
                var oldBlob = CurrentCloudBlobClient.GetBlobReferenceFromServer(item.StorageUri);
                var newBlob = container.GetBlockBlobReference(oldBlob.Name.Replace(prefix, newPrefix));
                newBlob.StartCopyFromBlob(item.Uri);
                oldBlob.Delete();
            });
        }
Esempio n. 8
0
        public virtual FolderItem GetFolderItemById(string itemId)
        {
            FolderItem retVal =
                this.ChangeTracker.TrackingEntries.Select(x => x.Entity)
                .OfType <FolderItem>()
                .FirstOrDefault(x => x.FolderItemId == itemId);

            if (retVal == null)
            {
                var cloudBlob = CurrentCloudBlobClient.GetBlobReferenceFromServer(GetUri(itemId)) as CloudBlockBlob;
                if (cloudBlob != null && cloudBlob.Exists())
                {
                    string folderItemTypeName = this._entityFactory.GetEntityTypeStringName(typeof(FolderItem));
                    retVal = this._entityFactory.CreateEntityForType(folderItemTypeName) as FolderItem;
                    this.MapCloudBlob2FolderItem(cloudBlob, retVal);
                }

                if (retVal != null)
                {
                    this.ChangeTracker.Attach(retVal);
                }
            }
            return(retVal);
        }