Esempio n. 1
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);
        }