Exemplo n.º 1
0
        /// <summary>
        /// Creates a thumbnail image of a file in the storage.
        /// </summary>
        /// <param name="originalMetadataId">Metadata entry ID for the original image file.</param>
        /// <param name="maxWidth">Maximum width of the thumbnail image.</param>
        /// <param name="maxHeight">Maximum height of the thumbnail image.</param>
        /// <param name="sourceFullStorageFileName">Name of the source image file.</param>
        /// <param name="thumbFullStorageFileName">Name of the thumbnail image cache file.</param>
        /// <param name="loadFromCache">Whether to use a cached thumbnail or not.</param>
        /// <param name="storage">Storage service used to get file data.</param>
        /// <param name="fileMetadataProvider">File metadata provider used to create thumbnail image metadata.</param>
        /// <returns>Returns the result of the thumbnail creation process.</returns>
        public static CreateThumbResult TryGetOrCreateThumb(
            int originalMetadataId,
            int maxWidth,
            int maxHeight,
            string sourceFullStorageFileName,
            string thumbFullStorageFileName,
            bool loadFromCache,
            IBlobStorageManager storage,
            IFileMetadataProvider fileMetadataProvider)
        {
            var srcBlobLocation = new BlobLocation(sourceFullStorageFileName);
            var thumbBlobLocation = new BlobLocation(thumbFullStorageFileName);

            if (loadFromCache && !string.IsNullOrEmpty(thumbFullStorageFileName))
            {
                var thumbStream = storage.DownloadFileFromStorage(thumbBlobLocation);
                if (thumbStream != null)
                {
                    using (thumbStream)
                    using (var stream = new MemoryStream((int)thumbStream.Length))
                    {
                        thumbStream.CopyTo(stream);
                        {
                            return new CreateThumbResult(
                                CreateThumbStatus.Ok,
                                stream.ToArray(),
                                MimeTypesHelper.GetContentType(Path.GetExtension(thumbFullStorageFileName)));
                        }
                    }
                }
            }

            if (!StringHelper.IsImageFileName(sourceFullStorageFileName))
                return new CreateThumbResult(CreateThumbStatus.SourceIsNotImage, null, null);

            var srcStream = storage.DownloadFileFromStorage(srcBlobLocation);

            if (srcStream == null)
                return new CreateThumbResult(CreateThumbStatus.SourceFileNotFound, null, null);

            string contentType;
            byte[] array;
            using (srcStream)
            using (var srcImage = Image.FromStream(srcStream))
            {
                var imageSizeMegabytes = srcImage.Width * srcImage.Height * 4 / 1024000.0;
                if (imageSizeMegabytes > 40.0)
                    return new CreateThumbResult(CreateThumbStatus.SourceImageTooLarge, null, null);

                using (var newImage = ResizeImage(srcImage, maxWidth, maxHeight, keepAspect: true, canGrow: false))
                using (var newStream = new MemoryStream())
                {
                    if (newImage == null)
                    {
                        srcStream.Position = 0;
                        srcStream.CopyTo(newStream);
                        contentType = MimeTypesHelper.GetContentType(Path.GetExtension(sourceFullStorageFileName));
                    }
                    else
                    {
                        var imageFormat = (newImage.Width * newImage.Height > 10000)
                            ? ImageFormat.Jpeg
                            : ImageFormat.Png;

                        contentType = (newImage.Width * newImage.Height > 10000)
                            ? "image/jpeg"
                            : "image/png";

                        newImage.Save(newStream, imageFormat);
                    }

                    array = newStream.ToArray();

                    if (loadFromCache && newImage != null && !string.IsNullOrEmpty(thumbFullStorageFileName))
                    {
                        // saving thumbnail image file metadata
                        var relationType = string.Format("thumb-{0}x{1}", maxWidth, maxHeight);
                        var metadata = fileMetadataProvider.CreateRelated(
                            originalMetadataId,
                            relationType,
                            thumbBlobLocation.ContainerName,
                            thumbBlobLocation.FileName,
                            thumbBlobLocation.BlobName,
                            null);

                        fileMetadataProvider.SaveChanges();

                        storage.UploadFileToStorage(new MemoryStream(array), thumbBlobLocation);
                    }
                }
            }

            return new CreateThumbResult(CreateThumbStatus.Ok, array, contentType);
        }
 /// <summary>
 /// Downloads a file from the Windows Azure Storage.
 /// </summary>
 /// <param name="this">Blob storage manager to use.</param>
 /// <param name="location">Location of the blob to download.</param>
 /// <returns>Returns a valid stream that can be used to read file data, or null if the file does not exist.</returns>
 public static Stream DownloadFileFromStorage(this IBlobStorageManager @this, BlobLocation location)
 {
     return(@this.DownloadFileFromStorage(location.ContainerName, location.BlobName));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a thumbnail image of a file in the storage.
        /// </summary>
        /// <param name="originalMetadataId">Metadata entry ID for the original image file.</param>
        /// <param name="maxWidth">Maximum width of the thumbnail image.</param>
        /// <param name="maxHeight">Maximum height of the thumbnail image.</param>
        /// <param name="sourceFullStorageFileName">Name of the source image file.</param>
        /// <param name="thumbFullStorageFileName">Name of the thumbnail image cache file.</param>
        /// <param name="loadFromCache">Whether to use a cached thumbnail or not.</param>
        /// <param name="storage">Storage service used to get file data.</param>
        /// <param name="fileMetadataProvider">File metadata provider used to create thumbnail image metadata.</param>
        /// <returns>Returns the result of the thumbnail creation process.</returns>
        public static CreateThumbResult TryGetOrCreateThumb(
            int originalMetadataId,
            int maxWidth,
            int maxHeight,
            string sourceFullStorageFileName,
            string thumbFullStorageFileName,
            bool loadFromCache,
            IBlobStorageManager storage,
            IFileMetadataProvider fileMetadataProvider)
        {
            var srcBlobLocation   = new BlobLocation(sourceFullStorageFileName);
            var thumbBlobLocation = new BlobLocation(thumbFullStorageFileName);

            if (loadFromCache && !string.IsNullOrEmpty(thumbFullStorageFileName))
            {
                var thumbStream = storage.DownloadFileFromStorage(thumbBlobLocation);
                if (thumbStream != null)
                {
                    using (thumbStream)
                        using (var stream = new MemoryStream((int)thumbStream.Length))
                        {
                            thumbStream.CopyTo(stream);
                            {
                                return(new CreateThumbResult(
                                           CreateThumbStatus.Ok,
                                           stream.ToArray(),
                                           MimeTypesHelper.GetContentType(Path.GetExtension(thumbFullStorageFileName))));
                            }
                        }
                }
            }

            if (!StringHelper.IsImageFileName(sourceFullStorageFileName))
            {
                return(new CreateThumbResult(CreateThumbStatus.SourceIsNotImage, null, null));
            }

            var srcStream = storage.DownloadFileFromStorage(srcBlobLocation);

            if (srcStream == null)
            {
                return(new CreateThumbResult(CreateThumbStatus.SourceFileNotFound, null, null));
            }

            string contentType;

            byte[] array;
            using (srcStream)
                using (var srcImage = Image.FromStream(srcStream))
                {
                    var imageSizeMegabytes = srcImage.Width * srcImage.Height * 4 / 1024000.0;
                    if (imageSizeMegabytes > 40.0)
                    {
                        return(new CreateThumbResult(CreateThumbStatus.SourceImageTooLarge, null, null));
                    }

                    using (var newImage = ResizeImage(srcImage, maxWidth, maxHeight, keepAspect: true, canGrow: false))
                        using (var newStream = new MemoryStream())
                        {
                            if (newImage == null)
                            {
                                srcStream.Position = 0;
                                srcStream.CopyTo(newStream);
                                contentType = MimeTypesHelper.GetContentType(Path.GetExtension(sourceFullStorageFileName));
                            }
                            else
                            {
                                var imageFormat = (newImage.Width * newImage.Height > 10000)
                            ? ImageFormat.Jpeg
                            : ImageFormat.Png;

                                contentType = (newImage.Width * newImage.Height > 10000)
                            ? "image/jpeg"
                            : "image/png";

                                newImage.Save(newStream, imageFormat);
                            }

                            array = newStream.ToArray();

                            if (loadFromCache && newImage != null && !string.IsNullOrEmpty(thumbFullStorageFileName))
                            {
                                // saving thumbnail image file metadata
                                var relationType = string.Format("thumb-{0}x{1}", maxWidth, maxHeight);
                                var metadata     = fileMetadataProvider.CreateRelated(
                                    originalMetadataId,
                                    relationType,
                                    thumbBlobLocation.ContainerName,
                                    thumbBlobLocation.FileName,
                                    thumbBlobLocation.BlobName,
                                    null);

                                fileMetadataProvider.SaveChanges();

                                storage.UploadFileToStorage(new MemoryStream(array), thumbBlobLocation);
                            }
                        }
                }

            return(new CreateThumbResult(CreateThumbStatus.Ok, array, contentType));
        }