/// <summary>
        ///  Build the URL using defined options and additional passed options
        /// </summary>
        /// <param name="url">URL of original image</param>
        /// <param name="options">Additional options to be used in URL generation</param>
        /// <param name="encode">If true, original image URL will be base64-encoded</param>
        /// <returns></returns>
        public string Build(string url, IReadOnlyCollection <ImgProxyOption> options, bool encode = false)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException(nameof(url));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            FormatOption overrideFormatOption = null;

            var dict = new Dictionary <string, ImgProxyOption>(Options);

            foreach (var option in options)
            {
                if (option is FormatOption formatOption)
                {
                    overrideFormatOption = formatOption;
                }
                else
                {
                    dict[option.GetType().Name] = option;
                }
            }

            return(overrideFormatOption != null
                ? BuildWithFormatAndOptions(url, encode, dict, overrideFormatOption)
                : BuildWithFormatAndOptions(url, encode, dict, FormatOption));
        }
Esempio n. 2
0
        protected void AddOption(ImgProxyOption option)
        {
            switch (option)
            {
            case null:
                throw new ArgumentNullException(nameof(option));

            case FormatOption formatOption:
                FormatOption = formatOption;
                break;

            default:
                Options[option.GetType().Name] = option;
                break;
            }
        }
Esempio n. 3
0
        protected string BuildWithFormatAndOptions(string url, bool encode, Dictionary <string, ImgProxyOption> dict, FormatOption formatOption)
        {
            var processingOptions = string.Join("/", dict.Values);

            string path;

            if (encode)
            {
                path = formatOption != null
                    ? $"/{processingOptions}/{HexHelper.StringToSafeBase64(url)}.{formatOption.Format}"
                    : $"/{processingOptions}/{HexHelper.StringToSafeBase64(url)}";
            }
            else
            {
                path = formatOption != null
                    ? $"/{processingOptions}/plain/{url}@{formatOption.Format}"
                    : $"/{processingOptions}/plain/{url}";
            }

            var signature = GetSignature(path);

            return($"{_host}/{signature}{path}");
        }