Beispiel #1
0
        public void Export(Repository repository, string baseFolder, string[] folders, string[] docs, Stream outputStream)
        {
            ZipFile zipFile    = new ZipFile();
            var     basePrefix = StorageNamesEncoder.EncodeContainerName(repository.Name) + "/" + MediaBlobHelper.MediaDirectoryName + "/";

            if (!string.IsNullOrEmpty(baseFolder))
            {
                var baseMediaFolder = ServiceFactory.MediaFolderManager.Get(repository, baseFolder);
                basePrefix = baseMediaFolder.GetMediaFolderItemPath(null) + "/";
            }

            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

            //add file
            if (docs != null)
            {
                foreach (var doc in docs)
                {
                    var blob = blobClient.GetBlockBlobReference(basePrefix + StorageNamesEncoder.EncodeBlobName(doc));

                    var bytes = blob.DownloadByteArray();
                    zipFile.AddEntry(doc, bytes);
                }
            }
            //add folders
            if (folders != null)
            {
                foreach (var folder in folders)
                {
                    var folderName = folder.Split('~').LastOrDefault();
                    zipFolder(blobClient, basePrefix, folderName, "", ref zipFile);
                }
            }
            zipFile.Save(outputStream);
        }
Beispiel #2
0
        public static MediaContent BlobToMediaContent(this CloudBlob blob, MediaContent mediaContent)
        {
            if (!string.IsNullOrEmpty(blob.Metadata["Published"]))
            {
                mediaContent.Published = bool.Parse(blob.Metadata["Published"]);
            }
            if (!string.IsNullOrEmpty(blob.Metadata["Size"]))
            {
                mediaContent.Size = int.Parse(blob.Metadata["Size"]);
            }
            mediaContent.FileName    = StorageNamesEncoder.DecodeBlobName(blob.Metadata["FileName"]);
            mediaContent.UserKey     = mediaContent.FileName;
            mediaContent.UUID        = mediaContent.FileName;
            mediaContent.UserId      = blob.Metadata["UserId"];
            mediaContent.VirtualPath = blob.Uri.ToString();
            if (mediaContent.Metadata == null)
            {
                mediaContent.Metadata = new MediaContentMetadata();
            }

            mediaContent.Metadata.AlternateText = StorageNamesEncoder.DecodeBlobName(blob.Metadata["AlternateText"]);
            mediaContent.Metadata.Description   = StorageNamesEncoder.DecodeBlobName(blob.Metadata["Description"]);
            mediaContent.Metadata.Title         = StorageNamesEncoder.DecodeBlobName(blob.Metadata["Title"]);
            return(mediaContent);
        }
Beispiel #3
0
        public static void DeleteRepositoryContainer(Repository repository)
        {
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

            var container = blobClient.GetContainerReference(StorageNamesEncoder.EncodeContainerName(repository.Name));

            container.Delete();
        }
        public static string GetTextContentDirectoryPath(this TextContent textContent)
        {
            var textFolder = textContent.GetFolder();

            return(UrlUtility.Combine(new string[] { StorageNamesEncoder.EncodeContainerName(textFolder.Repository.Name), TextContentFileDirectoryName }
                                      .Concat(textFolder.NamePaths.Select(it => StorageNamesEncoder.EncodeBlobName(it)))
                                      .Concat(new[] { StorageNamesEncoder.EncodeBlobName(textContent.UUID) })
                                      .ToArray()));
        }
Beispiel #5
0
        private static void MoveContent(MediaContent oldMediaContent, MediaContent newMediaContent)
        {
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

            var oldContentBlob = blobClient.GetBlockBlobReference(oldMediaContent.GetMediaBlobPath());
            var newContentBlob = blobClient.GetBlockBlobReference(newMediaContent.GetMediaBlobPath());

            if (oldContentBlob.Exists() && !newContentBlob.Exists())
            {
                try
                {
                    newContentBlob.CopyFromBlob(oldContentBlob);
                }
                catch (Exception e)
                {
                    using (Stream stream = new MemoryStream())
                    {
                        oldContentBlob.DownloadToStream(stream);
                        stream.Position = 0;
                        newContentBlob.UploadFromStream(stream);
                        stream.Dispose();
                    }
                }
                newContentBlob.Metadata["FileName"] = StorageNamesEncoder.EncodeBlobName(newMediaContent.FileName);
                if (!string.IsNullOrEmpty(oldContentBlob.Metadata["UserId"]))
                {
                    newContentBlob.Metadata["UserId"] = oldContentBlob.Metadata["UserId"];
                }
                if (!string.IsNullOrEmpty(oldContentBlob.Metadata["Published"]))
                {
                    newContentBlob.Metadata["Published"] = oldContentBlob.Metadata["Published"];
                }
                if (!string.IsNullOrEmpty(oldContentBlob.Metadata["Size"]))
                {
                    newContentBlob.Metadata["Size"] = oldContentBlob.Metadata["Size"];
                }
                if (oldContentBlob.Metadata.AllKeys.Contains("AlternateText"))
                {
                    newContentBlob.Metadata["AlternateText"] = oldContentBlob.Metadata["AlternateText"];
                }
                if (oldContentBlob.Metadata.AllKeys.Contains("Description"))
                {
                    newContentBlob.Metadata["Description"] = oldContentBlob.Metadata["Description"];
                }
                if (oldContentBlob.Metadata.AllKeys.Contains("Title"))
                {
                    newContentBlob.Metadata["Title"] = oldContentBlob.Metadata["Title"];
                }

                newContentBlob.SetMetadata();
                oldContentBlob.DeleteIfExists();
            }
        }
Beispiel #6
0
        public static CloudBlob MediaContentToBlob(this MediaContent mediaContent, CloudBlob blob)
        {
            if (mediaContent.Published.HasValue)
            {
                blob.Metadata["Published"] = mediaContent.Published.Value.ToString();
            }
            if (!string.IsNullOrEmpty(mediaContent.UserId))
            {
                blob.Metadata["UserId"] = mediaContent.UserId;
            }
            if (!string.IsNullOrEmpty(mediaContent.FileName))
            {
                blob.Metadata["FileName"] = StorageNamesEncoder.EncodeBlobName(mediaContent.FileName);
            }
            if (mediaContent.ContentFile != null)
            {
                blob.Metadata["Size"] = mediaContent.ContentFile.Stream.Length.ToString();
            }
            if (mediaContent.Metadata != null)
            {
                if (!string.IsNullOrEmpty(mediaContent.Metadata.AlternateText))
                {
                    blob.Metadata["AlternateText"] = StorageNamesEncoder.EncodeBlobName(mediaContent.Metadata.AlternateText);
                }
                else if (blob.Metadata.AllKeys.Contains("AlternateText"))
                {
                    blob.Metadata.Remove("AlternateText");
                }

                if (!string.IsNullOrEmpty(mediaContent.Metadata.Description))
                {
                    blob.Metadata["Description"] = StorageNamesEncoder.EncodeBlobName(mediaContent.Metadata.Description);
                }
                else if (blob.Metadata.AllKeys.Contains("Description"))
                {
                    blob.Metadata.Remove("Description");
                }

                if (!string.IsNullOrEmpty(mediaContent.Metadata.Title))
                {
                    blob.Metadata["Title"] = StorageNamesEncoder.EncodeBlobName(mediaContent.Metadata.Title);
                }
                else if (blob.Metadata.AllKeys.Contains("Title"))
                {
                    blob.Metadata.Remove("Title");
                }
            }

            blob.Properties.ContentType = Kooboo.IO.IOUtility.MimeType(mediaContent.FileName);
            return(blob);
        }
Beispiel #7
0
        private void zipFolder(CloudBlobClient blobClient, string basePrefix, string folderName, string zipDir, ref ZipFile zipFile)
        {
            zipDir = string.IsNullOrEmpty(zipDir) ? folderName : zipDir + "/" + folderName;
            zipFile.AddDirectoryByName(zipDir);
            var folderPrefix = basePrefix + StorageNamesEncoder.EncodeBlobName(folderName) + "/";

            var blobs = blobClient.ListBlobsWithPrefix(folderPrefix,
                                                       new BlobRequestOptions()
            {
                BlobListingDetails = Microsoft.WindowsAzure.StorageClient.BlobListingDetails.Metadata, UseFlatBlobListing = false
            });

            foreach (var blob in blobs)
            {
                if (blob is CloudBlobDirectory)
                {
                    var dir = blob as CloudBlobDirectory;

                    var names = dir.Uri.ToString().Split('/');
                    for (var i = names.Length - 1; i >= 0; i--)
                    {
                        if (!string.IsNullOrEmpty(names[i]))
                        {
                            zipFolder(blobClient, folderPrefix, StorageNamesEncoder.DecodeBlobName(names[i]), zipDir, ref zipFile);
                            break;
                        }
                    }
                }
                if (blob is CloudBlob)
                {
                    var cloudBlob = blob as CloudBlob;
                    var subStr    = cloudBlob.Uri.ToString().Substring(cloudBlob.Uri.ToString().IndexOf(folderPrefix) + folderPrefix.Length);
                    var index     = subStr.LastIndexOf('/');
                    if (index < 0)
                    {
                        index = 0;
                    }

                    var subFolderName = subStr.Substring(0, index);
                    var fileName      = subStr.Substring(index);
                    var bytes         = cloudBlob.DownloadByteArray();
                    if (!string.IsNullOrEmpty(subFolderName))
                    {
                        zipFile.AddDirectoryByName(zipDir + "/" + StorageNamesEncoder.DecodeBlobName(subFolderName));
                    }
                    zipFile.AddEntry(zipDir + "/" + StorageNamesEncoder.DecodeBlobName(subStr), bytes);
                }
            }
        }
Beispiel #8
0
        public static CloudBlobContainer InitializeRepositoryContainer(Repository repository)
        {
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

            var container = blobClient.GetContainerReference(StorageNamesEncoder.EncodeContainerName(repository.Name));

            var created = container.CreateIfNotExist();

            if (created)
            {
                container.SetPermissions(new BlobContainerPermissions()
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });
            }

            return(container);
        }
Beispiel #9
0
        private void MoveDirectory(CloudBlobClient blobClient, string newPrefix, string oldPrefix)
        {
            var blobs = blobClient.ListBlobsWithPrefix(oldPrefix,
                                                       new BlobRequestOptions()
            {
                BlobListingDetails = Microsoft.WindowsAzure.StorageClient.BlobListingDetails.Metadata, UseFlatBlobListing = false
            });

            foreach (var blob in blobs)
            {
                if (blob is CloudBlobDirectory)
                {
                    var dir = blob as CloudBlobDirectory;

                    var names = dir.Uri.ToString().Split('/');
                    for (var i = names.Length - 1; i >= 0; i--)
                    {
                        if (!string.IsNullOrEmpty(names[i]))
                        {
                            MoveDirectory(blobClient, newPrefix + StorageNamesEncoder.EncodeBlobName(names[i]) + "/", oldPrefix + StorageNamesEncoder.EncodeBlobName(names[i]) + "/");
                            break;
                        }
                    }
                }
                else if (blob is CloudBlob)
                {
                    var cloudBlob = blob as CloudBlob;

                    if (cloudBlob.Exists())
                    {
                        cloudBlob.FetchAttributes();
                        var newContentBlob = blobClient.GetBlockBlobReference(newPrefix + cloudBlob.Metadata["FileName"]);
                        try
                        {
                            newContentBlob.CopyFromBlob(cloudBlob);
                        }
                        catch (Exception e)
                        {
                            using (Stream stream = new MemoryStream())
                            {
                                cloudBlob.DownloadToStream(stream);
                                stream.Position = 0;
                                newContentBlob.UploadFromStream(stream);
                                stream.Dispose();
                            }
                        }
                        newContentBlob.Metadata["FileName"] = cloudBlob.Metadata["FileName"];

                        if (!string.IsNullOrEmpty(cloudBlob.Metadata["UserId"]))
                        {
                            newContentBlob.Metadata["UserId"] = cloudBlob.Metadata["UserId"];
                        }
                        if (!string.IsNullOrEmpty(cloudBlob.Metadata["Published"]))
                        {
                            newContentBlob.Metadata["Published"] = cloudBlob.Metadata["Published"];
                        }
                        if (!string.IsNullOrEmpty(cloudBlob.Metadata["Size"]))
                        {
                            newContentBlob.Metadata["Size"] = cloudBlob.Metadata["Size"];
                        }
                        if (cloudBlob.Metadata.AllKeys.Contains("AlternateText"))
                        {
                            newContentBlob.Metadata["AlternateText"] = cloudBlob.Metadata["AlternateText"];
                        }
                        if (cloudBlob.Metadata.AllKeys.Contains("Description"))
                        {
                            newContentBlob.Metadata["Description"] = cloudBlob.Metadata["Description"];
                        }
                        if (cloudBlob.Metadata.AllKeys.Contains("Title"))
                        {
                            newContentBlob.Metadata["Title"] = cloudBlob.Metadata["Title"];
                        }

                        newContentBlob.SetMetadata();
                        cloudBlob.DeleteIfExists();
                    }
                }
            }
        }
 public static string GetTextContentFilePath(this TextContent textContent, ContentFile contentFile)
 {
     return(UrlUtility.Combine(GetTextContentDirectoryPath(textContent), StorageNamesEncoder.EncodeBlobName(contentFile.FileName)));
 }
Beispiel #11
0
 public static string GetMediaDirectoryPath(this MediaFolder mediaFolder)
 {
     return(UrlUtility.Combine(new string[] { StorageNamesEncoder.EncodeContainerName(mediaFolder.Repository.Name), MediaDirectoryName }
                               .Concat(mediaFolder.NamePaths)
                               .ToArray()));
 }