diff --git a/docs/configuration.md b/docs/configuration.md index d447045f..93af3cf4 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -334,7 +334,7 @@ imgproxy can send logs to syslog, but this feature is disabled by default. To en ## Miscellaneous -* `IMGPROXY_BASE_URL`: base URL prefix that will be added to every requested image URL. For example, if the base URL is `http://example.com/images` and `/path/to/image.png` is requested, imgproxy will download the source image from `http://example.com/images/path/to/image.png`. Default: blank. +* `IMGPROXY_BASE_URL`: base URL prefix that will be added to every requested image URL. For example, if the base URL is `http://example.com/images` and `/path/to/image.png` is requested, imgproxy will download the source image from `http://example.com/images/path/to/image.png`. If the image URL already contains the prefix, it won't be added. Default: blank. * `IMGPROXY_USE_LINEAR_COLORSPACE`: when `true`, imgproxy will process images in linear colorspace. This will slow down processing. Note that images won't be fully processed in linear colorspace while shrink-on-load is enabled (see below). * `IMGPROXY_DISABLE_SHRINK_ON_LOAD`: when `true`, disables shrink-on-load for JPEG and WebP. Allows to process the whole image in linear colorspace but dramatically slows down resizing and increases memory usage when working with large images. * `IMGPROXY_STRIP_METADATA`: when `true`, imgproxy will strip all metadata (EXIF, IPTC, etc.) from JPEG and WebP output images. Default: `true`. diff --git a/processing_options.go b/processing_options.go index bbd47f3a..1bf3b575 100644 --- a/processing_options.go +++ b/processing_options.go @@ -304,6 +304,14 @@ func colorFromHex(hexcolor string) (rgbColor, error) { return c, nil } +func addBaseURL(u string) string { + if len(conf.BaseURL) == 0 || strings.HasPrefix(u, conf.BaseURL) { + return u + } + + return fmt.Sprintf("%s%s", conf.BaseURL, u) +} + func decodeBase64URL(parts []string) (string, string, error) { var format string @@ -327,9 +335,7 @@ func decodeBase64URL(parts []string) (string, string, error) { return "", "", fmt.Errorf("Invalid url encoding: %s", encoded) } - fullURL := fmt.Sprintf("%s%s", conf.BaseURL, string(imageURL)) - - return fullURL, format, nil + return addBaseURL(string(imageURL)), format, nil } func decodePlainURL(parts []string) (string, string, error) { @@ -355,9 +361,7 @@ func decodePlainURL(parts []string) (string, string, error) { return "", "", fmt.Errorf("Invalid url encoding: %s", encoded) } - fullURL := fmt.Sprintf("%s%s", conf.BaseURL, unescaped) - - return fullURL, format, nil + return addBaseURL(unescaped), format, nil } func decodeURL(parts []string) (string, string, error) {