Esempio n. 1
0
        public virtual string GetThumbLocalPath(Picture picture, int targetSize = 0, bool showDefaultPicture = true)
        {
            // 'GetPictureUrl' takes care of creating the thumb when not created already
            string url = this.GetPictureUrl(picture, targetSize, showDefaultPicture);

            if (url.HasValue())
            {
                var settings = this.CreateResizeSettings(targetSize);

                var cachedImage = _imageCache.GetCachedImage(picture, settings);
                if (cachedImage.Exists)
                {
                    return(cachedImage.LocalPath);
                }

                if (showDefaultPicture)
                {
                    var fileName = this.GetDefaultImageFileName();
                    cachedImage = _imageCache.GetCachedImage(
                        0,
                        Path.GetFileNameWithoutExtension(fileName),
                        Path.GetExtension(fileName).TrimStart('.'),
                        settings);
                    if (cachedImage.Exists)
                    {
                        return(cachedImage.LocalPath);
                    }
                }
            }

            return(string.Empty);
        }
 /// <summary>
 /// Gets an instance of the <see cref="CachedImageResult"/> object, which contains information about a cached image.
 /// </summary>
 /// <param name="picture">The picture object for which to resolve a cached image.</param>
 /// <param name="settings">The image processing settings.</param>
 /// <returns>An instance of the <see cref="CachedImageResult"/> object</returns>
 /// <remarks>If the requested image does not exist in the cache, the value of the <c>Exists</c> property will be <c>false</c>.</remarks>
 public static CachedImageResult GetCachedImage(this IImageCache imageCache, Picture picture, object settings = null)
 {
     Guard.ArgumentNotNull(() => picture);
     return(imageCache.GetCachedImage(picture.Id, picture.SeoFilename, MimeTypes.MapMimeTypeToExtension(picture.MimeType), settings));
 }
        /// <summary>
        /// Adds an image to the cache.
        /// </summary>
        /// <param name="pictureId">The picture id, which will be part of the resulting file name.</param>
        /// <param name="seoFileName">The seo friendly picture name, which will be part of the resulting file name.</param>
        /// <param name="extension">The extension of the resulting file</param>
        /// <param name="buffer">The image binary data.</param>
        /// <param name="settings">The image processing settings.This object, if not <c>null</c>, is hashed and appended to the resulting file name.</param>
        public static void AddImageToCache(this IImageCache imageCache, int?pictureId, string seoFileName, string extension, byte[] buffer, object settings = null)
        {
            var cachedImage = imageCache.GetCachedImage(pictureId, seoFileName, extension, settings);

            imageCache.AddImageToCache(cachedImage, buffer);
        }
Esempio n. 4
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);
        }