Ejemplo n.º 1
0
        public ImageTypeRecord Get(string type, ImageQuality quality = ImageQuality.BestAvailable, bool acceptLower = true, bool mustHaveImage = false)
        {
            var typeName = type.ToUpper();
            var sorted   = ImageMappings.Where(r => r.Name == typeName).OrderByDescending(r => r.Quality);

            if (quality == ImageQuality.BestAvailable)
            {
                return(sorted.FirstOrDefault());
            }

            var exactMatch = sorted.SingleOrDefault(r => r.Quality == quality);

            if (exactMatch != null)
            {
                return(exactMatch);
            }

            // Don't have explicit size or have not asked for best available.
            if (acceptLower)
            {
                Debug.Assert(quality != ImageQuality.BestAvailable, "Must be an explicit quality");
                var newQuality = quality == ImageQuality.Large ? ImageQuality.Medium : ImageQuality.Small;
                if (newQuality != quality)
                {
                    return(Get(type, newQuality));
                }
            }

            return(mustHaveImage ? Get("Unknown", quality) : null);
        }
Ejemplo n.º 2
0
        public ImageTypeRecord Get(
            string type,
            ImageOptions options)
        {
            type.ThrowIfNullOrWhiteSpace(nameof(type));
            options.ThrowIfNull(nameof(options));

            var quality = options.Quality;

            var typeName = type.ToUpper();
            var sorted   = ImageMappings.Where(r => r.Name == typeName).OrderByDescending(r => r.Quality);

            if (quality == ImageQuality.BestAvailable)
            {
                return(sorted.FirstOrDefault());
            }

            var exactMatch = sorted.SingleOrDefault(r => r.Quality == quality);

            if (exactMatch != null)
            {
                return(exactMatch);
            }

            // Don't have explicit size or have not asked for best available.
            if (options.AcceptLowerQuality)
            {
                Debug.Assert(quality != ImageQuality.BestAvailable, "Must be an explicit Quality");
                var newQuality = quality == ImageQuality.Large ? ImageQuality.Medium : ImageQuality.Small;
                if (newQuality != quality)
                {
                    // Update options
                    // TODO: ideally this should clone
                    var newOptions = new ImageOptions
                    {
                        Quality            = newQuality,
                        AcceptLowerQuality = options.AcceptLowerQuality,
                        ImageMustExist     = options.ImageMustExist
                    };

                    // Recursive
                    return(Get(type, newOptions));
                }
            }

            return(options.ImageMustExist ? Get("Unknown", options) : null);
        }