Esempio n. 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);
        }
Esempio n. 2
0
        public static Dictionary <string, MediaFolder> GetList(Repository repository)
        {
            var container = MediaBlobHelper.InitializeRepositoryContainer(repository);

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

            var folderBlob = container.GetBlobReference(MediaBlobHelper.MediaDirectoryName);

            Dictionary <string, MediaFolder> folders = null;

            try
            {
                folderBlob.FetchAttributes();
                if (folderBlob.CheckIfMediaFolder())
                {
                    var xml = folderBlob.DownloadText();
                    folders = DataContractSerializationHelper.DeserializeFromXml <Dictionary <string, MediaFolder> >(xml);
                }
            }
            catch { }

            if (folders == null)
            {
                folders = new Dictionary <string, MediaFolder>();
            }
            return(new Dictionary <string, MediaFolder>(folders, StringComparer.OrdinalIgnoreCase));
        }
Esempio n. 3
0
        private static void SaveList(Repository repository, Dictionary <string, MediaFolder> folders)
        {
            if (folders != null && folders.Count > 0)
            {
                var xml = DataContractSerializationHelper.SerializeAsXml(folders);

                var container = MediaBlobHelper.InitializeRepositoryContainer(repository);

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

                var folderBlob = container.GetBlobReference(MediaBlobHelper.MediaDirectoryName);

                folderBlob.SetMediaFolderContentType();

                BlobRequestOptions header = new BlobRequestOptions();

                folderBlob.UploadText(xml);
            }
            else
            {
                var container = MediaBlobHelper.InitializeRepositoryContainer(repository);

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

                var folderBlob = container.GetBlobReference(MediaBlobHelper.MediaDirectoryName);

                folderBlob.DeleteIfExists();
            }
        }
Esempio n. 4
0
        public void Delete(MediaContent content)
        {
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

            var contentBlob = blobClient.GetBlobReference(content.GetMediaBlobPath());

            contentBlob.DeleteIfExists();
        }
Esempio n. 5
0
        public static void DeleteRepositoryContainer(Repository repository)
        {
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

            var container = blobClient.GetContainerReference(repository.Name.ToLower());

            container.Delete();
        }
Esempio n. 6
0
        public void Rename(MediaFolder @new, MediaFolder old)
        {
            MediaFolders.RenameFolder(@new, old);

            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();
            //var dir = blobContainer.GetDirectoryReference();
            var oldPrefix = old.GetMediaFolderItemPath(null) + "/";
            var newPrefix = @new.GetMediaFolderItemPath(null) + "/";

            MoveDirectory(blobClient, newPrefix, oldPrefix);
        }
Esempio n. 7
0
        private static void MoveContent(MediaContent oldMediaContent, MediaContent newMediaContent)
        {
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

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

            newContentBlob.CopyFromBlob(oldContentBlob);

            oldContentBlob.DeleteIfExists();
        }
Esempio n. 8
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();
            }
        }
        public string Save(Models.TextContent content, Models.ContentFile file)
        {
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

            var contentBlob = blobClient.GetBlobReference(content.GetTextContentFilePath(file));

            contentBlob.Properties.ContentType = Kooboo.IO.IOUtility.MimeType(file.FileName);

            contentBlob.UploadFromStream(file.Stream);

            return(contentBlob.Uri.ToString());
        }
Esempio n. 10
0
        public void SaveContentStream(MediaContent content, Stream stream)
        {
            if (stream.Length == 0)
            {
                return;
            }
            var blobClient  = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();
            var contentBlob = blobClient.GetBlobReference(content.GetMediaBlobPath());

            if (contentBlob.Exists())
            {
                contentBlob.UploadFromStream(stream);
            }
        }
Esempio n. 11
0
        public void DeleteFiles(Models.TextContent content)
        {
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

            var contentDir = blobClient.GetBlobDirectoryReference(content.GetTextContentDirectoryPath());

            foreach (var item in contentDir.ListBlobs())
            {
                CloudBlob blob = item as CloudBlob;
                if (item != null)
                {
                    blob.DeleteIfExists();
                }
            }
        }
Esempio n. 12
0
        public void Update(MediaContent @new, MediaContent old)
        {
            if ([email protected](old.FileName, StringComparison.OrdinalIgnoreCase))
            {
                MoveContent(old, @new);
            }
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

            var contentBlob = blobClient.GetBlobReference(@new.GetMediaBlobPath());

            contentBlob = @new.MediaContentToBlob(contentBlob);
            contentBlob.SetMetadata();

            @new.VirtualPath = contentBlob.Uri.ToString();
        }
Esempio n. 13
0
        public object Execute(IContentQuery <MediaContent> query)
        {
            var mediaQuery = (MediaContentQuery)query;

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

            QueryExpressionTranslator translator = new QueryExpressionTranslator();

            var blobs = translator.Translate(query.Expression, blobClient, mediaQuery.MediaFolder)
                        .Where(it => it != null)
                        .Select(it => it.BlobToMediaContent(new MediaContent(mediaQuery.Repository.Name, mediaQuery.MediaFolder.FullName)));

            foreach (var item in translator.OrderFields)
            {
                if (item.Descending)
                {
                    blobs = blobs.OrderByDescending(it => it.GetType().GetProperty(item.FieldName).GetValue(it, null));
                }
                else
                {
                    blobs = blobs.OrderBy(it => it.GetType().GetProperty(item.FieldName).GetValue(it, null));
                }
            }
            //translator.Visite(query.Expression);

            switch (translator.CallType)
            {
            case CallType.Count:
                return(blobs.Count());

            case CallType.First:
                return(blobs.First());

            case CallType.Last:
                return(blobs.Last());

            case CallType.LastOrDefault:
                return(blobs.LastOrDefault());

            case CallType.FirstOrDefault:
                return(blobs.FirstOrDefault());

            case CallType.Unspecified:
            default:
                return(blobs);
            }
        }
Esempio n. 14
0
        public void Delete(MediaFolder mediaFolder)
        {
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

            var contentBlob = blobClient.GetBlobDirectoryReference(mediaFolder.GetMediaDirectoryPath());

            foreach (var item in contentBlob.ListBlobs(new BlobRequestOptions()
            {
                UseFlatBlobListing = true
            }))
            {
                var blobContent = item as CloudBlob;
                if (blobContent != null)
                {
                    blobContent.DeleteIfExists();
                }
            }
        }
Esempio n. 15
0
        public static CloudBlobContainer InitializeRepositoryContainer(Repository repository)
        {
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

            var container = blobClient.GetContainerReference(repository.Name.ToLower());

            var created = container.CreateIfNotExist();

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

            return(container);
        }
Esempio n. 16
0
        public void Add(MediaContent content, bool overrided)
        {
            if (content.ContentFile != null)
            {
                content.FileName = content.ContentFile.FileName;

                content.UserKey = content.FileName;
                content.UUID    = content.FileName;

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

                var contentBlob = blobClient.GetBlobReference(content.GetMediaBlobPath());
                contentBlob = content.MediaContentToBlob(contentBlob);

                contentBlob.UploadFromStream(content.ContentFile.Stream);

                content.VirtualPath = contentBlob.Uri.ToString();
            }
        }
Esempio n. 17
0
        public byte[] GetContentStream(MediaContent content)
        {
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();
            var path       = string.Empty;

            if (string.IsNullOrEmpty(content.GetRepository().Name))
            {
                path = content.VirtualPath.Substring(AzureBlobServiceSettings.Instance.Endpoint.Length);
                //path = UrlUtility.Combine(path.Split('/').Select(it => StorageNamesEncoder.EncodeBlobName(it)).ToArray());
            }
            else
            {
                path = content.GetMediaBlobPath();
            }
            var contentBlob = blobClient.GetBlobReference(path);

            byte[] data = null;
            if (contentBlob.Exists())
            {
                data = contentBlob.DownloadByteArray();
            }
            return(data);
        }
Esempio n. 18
0
        public object Execute(IContentQuery <MediaContent> query)
        {
            var mediaQuery = (MediaContentQuery)query;

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

            QueryExpressionTranslator translator = new QueryExpressionTranslator();

            var blobs = translator.Translate(query.Expression, blobClient, mediaQuery.MediaFolder)
                        .Where(it => it != null)
                        .Select(it => it.BlobToMediaContent(new MediaContent(mediaQuery.Repository.Name, mediaQuery.MediaFolder.FullName)));

            //translator.Visite(query.Expression);

            switch (translator.CallType)
            {
            case CallType.Count:
                return(blobs.Count());

            case CallType.First:
                return(blobs.First());

            case CallType.Last:
                return(blobs.Last());

            case CallType.LastOrDefault:
                return(blobs.LastOrDefault());

            case CallType.FirstOrDefault:
                return(blobs.FirstOrDefault());

            case CallType.Unspecified:
            default:
                return(blobs);
            }
        }