Beispiel #1
0
        public async Task <ImageResizeUploadResult> CreateImageAsync(long tenantId, IBlobContent content, IEnumerable <ResizeOptions> options)
        {
            // Get streams to resized images
            IEnumerable <Stream> streams = options.Select(o => _imageAnalysisService.ResizeImage(content.Stream, o));

            // Create blobs
            long imageBlobId = await CreateBlobAsync(tenantId, content);

            List <long> blobIds = new List <long>();

            foreach (Stream stream in streams)
            {
                blobIds.Add(await CreateBlobAsync(tenantId, new BlobContent {
                    Name = content.Name, Type = content.Type, Stream = stream
                }));
            }

            // Return result
            return(new ImageResizeUploadResult
            {
                Name = content.Name,
                Size = content.Stream.Length,
                ImageBlobId = imageBlobId,
                PreviewImageBlobId = blobIds[0],
                ThumbnailImageBlobId = blobIds[1]
            });
        }
        public async Task <long> CreateBlobAsync(long tenantId, IBlobContent content)
        {
            // Construct blob object
            bool isImage = ContentTypeIsImage(content.Type);
            Blob blob    = isImage ? GetBlobImage(content) : new Blob()
            {
                BlobType = BlobType.Document
            };
            DateTime utcNow = DateTime.UtcNow;

            blob.ContentType = content.Type;
            blob.Created     = utcNow;
            blob.Name        = content.Name;
            blob.Path        = UncommittedPath;
            blob.Size        = (int)content.Stream.Length;
            blob.TenantId    = tenantId;
            blob.Updated     = utcNow;

            // Create blob record and get back newly allocated blob identifier
            blob.BlobId = isImage ? await _storageRepository.CreateBlobImageAsync(tenantId, (BlobImage)blob) : await _storageRepository.CreateBlobAsync(tenantId, blob);

            // Create blob content
            await _blobService.CreateBlobContentAsync(blob, content.Stream);

            // Return newly allocated blob identifier
            return(blob.BlobId);
        }
Beispiel #3
0
        /// <summary>
        /// Gets the upload stream.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <returns>The upload stream</returns>
        public override Stream GetUploadStream(IBlobContent content)
        {
            var            container = this.GetOrCreateContainer(this.containerName);
            CloudBlockBlob blob      = container.GetBlockBlobReference(GetBlobName(content));

            return(blob.OpenWrite());
        }
 /// <summary>
 /// Gets the download stream for a specific content..
 /// </summary>
 /// <param name="content">Descriptor of the item on the remote blob storage.</param>
 /// <returns>The binary stream of the content item.</returns>
 public override Stream GetDownloadStream(IBlobContent content)
 {
     TransferUtilityOpenStreamRequest request = new TransferUtilityOpenStreamRequest()
        .WithBucketName(this.bucketName).WithKey(content.FilePath);
     var stream = this.transferUtility.OpenStream(request);
     return stream;
 }
Beispiel #5
0
        public async Task ValidateCreatePageImagesAsync(long tenantId, long pageId, IBlobContent content)
        {
            // Get page details
            Page page = await _pageRepository.ReadPageAsync(tenantId, pageId);

            // Check that master page associated with page allows images
            MasterPage masterPage = await _masterPageRepository.ReadMasterPageAsync(tenantId, page.MasterPageId);

            if (!masterPage.HasImage)
            {
                throw new ValidationErrorException(new ValidationError(PagePropertyNames.ImageBlobId, PageResource.ImageNotAllowedMessage));
            }

            // Check that blob type is correct
            if (content.Type != ContentTypes.Gif && content.Type != ContentTypes.Jpeg && content.Type != ContentTypes.Png)
            {
                throw new ValidationErrorException(new ValidationError(PagePropertyNames.ImageBlobId, PageResource.ImageInvalidMessage));
            }

            // Check that supplied upload is an image
            ImageMetadata metadata = _imageAnalysisService.GetImageMetadata(content.Stream);

            if (metadata == null)
            {
                throw new ValidationErrorException(new ValidationError(PagePropertyNames.ImageBlobId, PageResource.ImageInvalidMessage));
            }

            // Check image dimension constraints (minimum width and height)
            if (metadata.Width < masterPage.ImageMinWidth.Value || metadata.Height < masterPage.ImageMinHeight.Value)
            {
                throw new ValidationErrorException(new ValidationError(PagePropertyNames.ImageBlobId, string.Format(PageResource.ImageDimensionsInvalidMessage, masterPage.ImageMinWidth.Value, masterPage.ImageMinHeight.Value)));
            }
        }
        /// <summary>
        /// Gets the download stream for a specific content..
        /// </summary>
        /// <param name="content">Descriptor of the item on the remote blob storage.</param>
        /// <returns>The binary stream of the content item.</returns>
        public override Stream GetDownloadStream(IBlobContent content)
        {
            TransferUtilityOpenStreamRequest request = new TransferUtilityOpenStreamRequest()
                                                       .WithBucketName(this.bucketName).WithKey(content.FilePath);
            var stream = this.transferUtility.OpenStream(request);

            return(stream);
        }
        private BlobImage GetBlobImage(IBlobContent content)
        {
            long          position = content.Stream.Position;
            ImageMetadata metadata = _imageAnalysisService.GetImageMetadata(content.Stream);

            content.Stream.Position = position;
            return(new BlobImage
            {
                BlobType = BlobType.Image,
                Height = metadata.Height,
                Width = metadata.Width
            });
        }
        /// <summary>
        /// Uploads the specified content item to the remote blob storage.
        /// </summary>
        /// <param name="content">Descriptor of the item on the remote blob storage.</param>
        /// <param name="source">The source item's content stream.</param>
        /// <param name="bufferSize">Size of the upload buffer.</param>
        /// <returns>The length of the uploaded stream.</returns>
        public override long Upload(IBlobContent content, Stream source, int bufferSize)
        {
            var request = new TransferUtilityUploadRequest()
            {
                BucketName  = this.bucketName,
                Key         = content.FilePath,
                PartSize    = bufferSize,
                ContentType = content.MimeType,
                CannedACL   = S3CannedACL.PublicRead
            };

            //get it before the upload, because afterwards the stream is closed already
            long sourceLength = source.Length;

            using (MemoryStream str = new MemoryStream())
            {
                source.CopyTo(str);
                request.InputStream = str;

                this.transferUtility.Upload(request);
            }
            return(sourceLength);
        }
        /// <summary>
        /// Uploads the specified content item to the remote blob storage.
        /// </summary>
        /// <param name="content">Descriptor of the item on the remote blob storage.</param>
        /// <param name="source">The source item's content stream.</param>
        /// <param name="bufferSize">Size of the upload buffer.</param>
        /// <returns>The length of the uploaded stream.</returns>
        public override long Upload(IBlobContent content, Stream source, int bufferSize)
        {
            var request = new TransferUtilityUploadRequest()
                          .WithBucketName(this.bucketName)
                          .WithKey(content.FilePath)
                          .WithPartSize(bufferSize)
                          .WithContentType(content.MimeType);

            //set the item's accessibility as public
            request.AddHeader("x-amz-acl", "public-read");

            //get it before the upload, because afterwards the stream is closed already
            long sourceLength = source.Length;

            using (MemoryStream str = new MemoryStream())
            {
                source.CopyTo(str);
                request.InputStream = str;

                this.transferUtility.Upload(request);
            }
            return(sourceLength);
        }
 /// <summary>
 /// Gets the upload stream for a specific content.
 /// </summary>
 /// <param name="content">Descriptor of the item on the remote blob storage.</param>
 /// <returns>The upload stream for a specific content.</returns>
 public override Stream GetUploadStream(IBlobContent content)
 {
     throw new NotSupportedException("GetUploadStream() is not supported. Override Upload method.");
 }
        /// <summary>
        /// Uploads the specified content item to the remote blob storage.
        /// </summary>
        /// <param name="content">Descriptor of the item on the remote blob storage.</param>
        /// <param name="source">The source item's content stream.</param>
        /// <param name="bufferSize">Size of the upload buffer.</param>
        /// <returns>The length of the uploaded stream.</returns>
        public override long Upload(IBlobContent content, Stream source, int bufferSize)
        {
            var request = new TransferUtilityUploadRequest()
            {
                BucketName = this.bucketName,
                Key = content.FilePath,
                PartSize = 6291456,
                ContentType = content.MimeType,
                CannedACL = S3CannedACL.PublicRead
            };

            //get it before the upload, because afterwards the stream is closed already
            long sourceLength = source.Length;
            using (MemoryStream str = new MemoryStream())
            {
                source.CopyTo(str);
                request.InputStream = str;

                this.transferUtility.Upload(request);
            }
            return sourceLength;
        }
        /// <summary>
        /// Gets the download stream.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <returns></returns>
        public override Stream GetDownloadStream(IBlobContent content)
        {
            string filename = this.GetBlobName(content);
            try
            {
                var cloudObject = this.ContainerBucket.GetObject(filename);
                return cloudObject.Read();
            }
            catch (ObjectNotFoundException oex)
            {
                Logger.Writer.Write("Object not found: Error obtaining the download stream for file {0} on the CDN as {1} CDN. {2}".Arrange(content.FilePath, filename, oex.Message));
            }catch(Exception ex){
                Logger.Writer.Write("Error obtaining the download stream for file {0} on the CDN as {1} CDN. {2}".Arrange(content.FilePath, filename, ex.Message));
            }

            return null;
        }
        /// <summary>
        /// Uploads the specified content item to the remote blob storage.
        /// </summary>
        /// <param name="content">Descriptor of the item on the remote blob storage.</param>
        /// <param name="source">The source item's content stream.</param>
        /// <param name="bufferSize">Size of the upload buffer.</param>
        /// <returns>The length of the uploaded stream.</returns>
        public override long Upload(IBlobContent content, Stream source, int bufferSize)
        {
            var request = new TransferUtilityUploadRequest()
                .WithBucketName(this.bucketName)
                .WithKey(content.FilePath)
                .WithPartSize(bufferSize)
                .WithContentType(content.MimeType);

            //set the item's accessibility as public
            request.AddHeader("x-amz-acl", "public-read");

            //get it before the upload, because afterwards the stream is closed already
            long sourceLength = source.Length;
            using (MemoryStream str = new MemoryStream())
            {
                source.CopyTo(str);
                request.InputStream = str;

                this.transferUtility.Upload(request);
            }
            return sourceLength;
        }
 public override Stream GetUploadStream(IBlobContent content)
 {
     EnsureFilePath(content);
     return base.GetUploadStream(content);
 }
Beispiel #15
0
        /// <summary>
        /// Downloads a content to the stream
        /// </summary>
        /// <param name="content">The content</param>
        /// <param name="target">The target stream</param>
        public void DownloadToStream(IBlobContent content, Stream target)
        {
            CloudBlob blob = this.GetBlob(content);

            blob.DownloadToStream(target);
        }
Beispiel #16
0
        /// <summary>
        /// Gets the download stream.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <returns>The download stream</returns>
        public override Stream GetDownloadStream(IBlobContent content)
        {
            CloudBlob blob = this.GetBlob(content);

            return(blob.OpenRead());
        }
        /// <summary>
        /// Uploads the specified content item to the remote blob storage.
        /// </summary>
        /// <param name="content">Descriptor of the item on the remote blob storage.</param>
        /// <param name="source">The source item's content stream.</param>
        /// <param name="bufferSize">Size of the upload buffer.</param>
        /// <returns>The length of the uploaded stream.</returns>
        public override long Upload(IBlobContent content, Stream source, int bufferSize)
        {
            var filename = this.GetBlobName(content);
            var obj = new CF_Object(this.Connection, this.ContainerBucket, this.Client, filename);

            obj.Write(source);

            return obj.ContentLength;
        }
 /// <summary>
 /// Gets the download stream.
 /// </summary>
 /// <param name="content">The content.</param>
 /// <returns>The download stream</returns>
 public override Stream GetDownloadStream(IBlobContent content)
 {
     CloudBlob blob = this.GetBlob(content);
     return blob.OpenRead();
 }
Beispiel #19
0
        public async Task <IEnumerable <ImageResizeUploadResult> > CreatePageImagesAsync(long tenantId, long pageId, IBlobContent content)
        {
            // Validate the request to create page images
            await _pageValidator.ValidateCreatePageImagesAsync(tenantId, pageId, content);

            // Create thumbnail and preview images
            Page page = await _pageRepository.ReadPageAsync(tenantId, pageId);

            MasterPage masterPage = await _masterPageRepository.ReadMasterPageAsync(tenantId, page.MasterPageId);

            ResizeOptions thumbnailImageResizeOptions = new ResizeOptions {
                Mode = masterPage.ThumbnailImageResizeMode.Value, Width = masterPage.ThumbnailImageWidth.Value, Height = masterPage.ThumbnailImageHeight.Value
            };
            ResizeOptions previewImageResizeOptions = new ResizeOptions {
                Mode = masterPage.PreviewImageResizeMode.Value, Width = masterPage.PreviewImageWidth.Value, Height = masterPage.PreviewImageHeight.Value
            };
            Stream thumbnailImageContentStream = _imageAnalysisService.ResizeImage(content.Stream, thumbnailImageResizeOptions);
            Stream previewImageContentStream   = _imageAnalysisService.ResizeImage(content.Stream, previewImageResizeOptions);

            // Create blobs for thumbnail, preview and original image
            long imageBlobId = await _storageService.CreateBlobAsync(tenantId, content);

            long previewImageBlobId = await _storageService.CreateBlobAsync(tenantId, new BlobContent { Name = content.Name, Type = content.Type, Stream = previewImageContentStream });

            long thumbnailImageBlobId = await _storageService.CreateBlobAsync(tenantId, new BlobContent { Name = content.Name, Type = content.Type, Stream = thumbnailImageContentStream });

            // Return result
            return(new List <ImageResizeUploadResult>
            {
                new ImageResizeUploadResult
                {
                    Name = content.Name,
                    Size = content.Stream.Length,
                    ImageBlobId = imageBlobId,
                    PreviewImageBlobId = previewImageBlobId,
                    ThumbnailImageBlobId = thumbnailImageBlobId
                }
            });
        }
 /// <summary>
 /// Gets the upload stream for a specific content.
 /// </summary>
 /// <param name="content">Descriptor of the item on the remote blob storage.</param>
 /// <returns>The upload stream for a specific content.</returns>
 public override Stream GetUploadStream(IBlobContent content)
 {
     throw new NotSupportedException("GetUploadStream() is not supported. Override Upload method.");
 }
 /// <summary>
 /// Downloads a content to the stream
 /// </summary>
 /// <param name="content">The content</param>
 /// <param name="target">The target stream</param>
 public void DownloadToStream(IBlobContent content, Stream target)
 {
     CloudBlob blob = this.GetBlob(content);
     blob.DownloadToStream(target);
 }
 /// <summary>
 /// Gets the upload stream.
 /// </summary>
 /// <param name="content">The content.</param>
 /// <returns>The upload stream</returns>
 public override Stream GetUploadStream(IBlobContent content)
 {
     var container = this.GetOrCreateContainer(this.containerName);
     CloudBlockBlob blob = container.GetBlockBlobReference(GetBlobName(content));
     return blob.OpenWrite();
 }