Exemple #1
0
        /// <summary>
        /// Refreshes file metadata like size, dimensions etc.
        /// </summary>
        /// <param name="stream">The file stream (can be null)</param>
        public static void RefreshMetadata(this MediaFile file, Stream stream, IImageFactory imageFactory)
        {
            Guard.NotNull(file, nameof(file));

            file.Size      = stream != null ? (int)stream.Length : 0;
            file.Width     = null;
            file.Height    = null;
            file.PixelSize = null;

            if (stream != null && file.MediaType == MediaType.Image)
            {
                try
                {
                    var size = ImageHeader.GetPixelSize(stream, file.MimeType);
                    file.Width     = size.Width;
                    file.Height    = size.Height;
                    file.PixelSize = size.Width * size.Height;
                }
                catch
                {
                    // Don't attempt again
                    file.Width     = 0;
                    file.Height    = 0;
                    file.PixelSize = 0;
                }
            }
        }
Exemple #2
0
        private async Task EnsureMetadataResolvedAsync(MediaFile file, bool saveOnResolve)
        {
            var mediaType = _typeResolver.Resolve(file.Extension, file.MimeType);

            var resolveDimensions = mediaType == MediaType.Image && file.Width == null && file.Height == null;
            var resolveSize       = file.Size <= 0;

            Stream stream = null;

            if (resolveDimensions || resolveSize)
            {
                stream = await _storageProvider.OpenReadAsync(file);
            }

            // Resolve image dimensions
            if (stream != null)
            {
                try
                {
                    if (resolveSize)
                    {
                        file.Size = (int)stream.Length;
                    }

                    if (resolveDimensions)
                    {
                        var size = ImageHeader.GetPixelSize(stream, file.MimeType, true);
                        file.Width     = size.Width;
                        file.Height    = size.Height;
                        file.PixelSize = size.Width * size.Height;
                    }

                    if (saveOnResolve && (resolveSize || resolveDimensions))
                    {
                        try
                        {
                            _db.TryUpdate(file);
                            await _db.SaveChangesAsync();
                        }
                        catch (InvalidOperationException ioe)
                        {
                            // Ignore exception for pictures that already have been processed.
                            if (!ioe.IsAlreadyAttachedEntityException())
                            {
                                throw;
                            }
                        }
                    }
                }
                finally
                {
                    stream.Dispose();
                }
            }
        }
Exemple #3
0
        protected async Task <AsyncOut <IImage> > ProcessImage(MediaFile file, Stream inStream)
        {
            var originalSize = Size.Empty;
            var format       = _imageProcessor.Factory.FindFormatByExtension(file.Extension) ?? new UnsupportedImageFormat(file.MimeType, file.Extension);

            try
            {
                originalSize = ImageHeader.GetPixelSize(inStream, file.MimeType);
            }
            catch
            {
            }

            IImage outImage;

            if (format is UnsupportedImageFormat)
            {
                outImage = new ImageWrapper(inStream, originalSize, format);
                return(new AsyncOut <IImage>(true, outImage));
            }

            var maxSize = _mediaSettings.MaximumImageSize;

            var query = new ProcessImageQuery(inStream)
            {
                Format               = file.Extension,
                DisposeSource        = true,
                ExecutePostProcessor = ImagePostProcessingEnabled,
                IsValidationMode     = true
            };

            if (originalSize.IsEmpty || (originalSize.Height <= maxSize && originalSize.Width <= maxSize))
            {
                // Give subscribers the chance to (pre)-process
                var evt = new ImageUploadedEvent(query, originalSize);
                await _eventPublisher.PublishAsync(evt);

                outImage = evt.ResultImage ?? new ImageWrapper(inStream, originalSize, format);

                return(new AsyncOut <IImage>(true, outImage));
            }

            query.MaxSize = maxSize;

            using (var result = await _imageProcessor.ProcessImageAsync(query, false))
            {
                outImage = result.Image;
                return(new AsyncOut <IImage>(true, outImage));
            }
        }