Esempio n. 1
0
        protected internal virtual string GetProcessedImageUrl(object source, int?pictureId, string seoFileName, string extension, int targetSize = 0, string storeLocation = null)
        {
            var cachedImage = _imageCache.GetCachedImage(
                pictureId,
                seoFileName,
                extension,
                this.CreateResizeSettings(targetSize));

            if (!cachedImage.Exists)
            {
                lock (s_lock)
                {
                    if (!File.Exists(cachedImage.LocalPath)) // check again
                    {
                        var buffer = source as byte[];
                        if (buffer == null)
                        {
                            if (!(source is string))
                            {
                                return(string.Empty);
                            }

                            try
                            {
                                buffer = File.ReadAllBytes((string)source);
                            }
                            catch (Exception ex)
                            {
                                _logger.Error("Error reading media file '{0}'.".FormatInvariant(source), ex);
                                return(string.Empty);
                            }
                        }

                        try
                        {
                            if (targetSize == 0)
                            {
                                _imageCache.AddImageToCache(cachedImage, buffer);
                            }
                            else
                            {
                                var sourceStream = new MemoryStream(buffer);
                                using (var resultStream = _imageResizerService.ResizeImage(sourceStream, targetSize, targetSize, _mediaSettings.DefaultImageQuality))
                                {
                                    _imageCache.AddImageToCache(cachedImage, resultStream.GetBuffer());
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.Error("Error processing/writing media file '{0}'.".FormatInvariant(cachedImage.LocalPath), ex);
                            return(string.Empty);
                        }
                    }
                }
            }

            var url = _imageCache.GetImageUrl(cachedImage.Path, storeLocation);

            return(url);
        }
Esempio n. 2
0
        protected internal virtual string GetProcessedImageUrl(
            object source,             // byte[], string or Picture
            string seoFileName,
            string extension,
            int targetSize       = 0,
            string storeLocation = null)
        {
            var resizeSettings = new ResizeSettings();

            if (targetSize > 0)
            {
                resizeSettings.MaxWidth  = targetSize;
                resizeSettings.MaxHeight = targetSize;
            }

            var picture = source as Picture;

            var cachedImage = _imageCache.GetCachedImage(
                picture?.Id,
                seoFileName,
                extension,
                resizeSettings);

            if (!cachedImage.Exists)
            {
                lock (String.Intern(cachedImage.Path))
                {
                    byte[] buffer = null;

                    try
                    {
                        if (source is string)
                        {
                            // static default image
                            buffer = File.ReadAllBytes((string)source);
                        }
                        else if (source is Picture)
                        {
                            buffer = LoadPictureBinary((Picture)source);
                        }
                        else if (source is byte[])
                        {
                            buffer = (byte[])source;
                        }

                        if (buffer == null || buffer.LongLength == 0)
                        {
                            return(string.Empty);
                        }
                    }
                    catch (Exception exception)
                    {
                        _logger.ErrorFormat(exception, "Error reading media file '{0}'.", source);
                        return(string.Empty);
                    }

                    try
                    {
                        _imageCache.ProcessAndAddImageToCache(cachedImage, buffer, targetSize);
                    }
                    catch (Exception exception)
                    {
                        _logger.ErrorFormat(exception, "Error processing/writing media file '{0}'.", cachedImage.Path);
                        return(string.Empty);
                    }
                }
            }

            var url = _imageCache.GetImageUrl(cachedImage.Path, storeLocation);

            return(url);
        }