Esempio n. 1
0
        protected internal virtual async Task <string> GetProcessedImageUrlAsync(
            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)
            {
                byte[] buffer = null;

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

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

                try
                {
                    await _imageCache.ProcessAndAddImageToCacheAsync(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);
        }