MM-14686 Send all image proxy requests through /api/v4/image (#10775)

* MM-14686 Implement /api/v4/image when proxy is disabled

* MM-14686 Send all image proxy requests through /api/v4/image

* Update unit tests
Этот коммит содержится в:
Harrison Healey
2019-05-06 09:22:37 -04:00
коммит произвёл GitHub
родитель 4552c20d5b
Коммит dce6cb601f
10 изменённых файлов: 128 добавлений и 213 удалений

Просмотреть файл

@@ -7,6 +7,8 @@ import (
"errors"
"io"
"net/http"
"net/url"
"strings"
"sync"
"github.com/mattermost/mattermost-server/mlog"
@@ -39,13 +41,6 @@ type ImageProxyBackend interface {
// GetImageDirect returns a proxied image along with its content type.
GetImageDirect(imageURL string) (io.ReadCloser, string, error)
// GetProxiedImageURL returns the URL to access a given image through the image proxy, whether the image proxy is
// running externally or as part of the Mattermost server itself.
GetProxiedImageURL(imageURL string) string
// GetUnproxiedImageURL returns the original URL of an image from one that has been directed at the image proxy.
GetUnproxiedImageURL(proxiedURL string) string
}
func MakeImageProxy(configService configservice.ConfigService, httpService httpservice.HTTPService, logger *mlog.Logger) *ImageProxy {
@@ -123,24 +118,36 @@ func (proxy *ImageProxy) GetImageDirect(imageURL string) (io.ReadCloser, string,
// GetProxiedImageURL takes the URL of an image and returns a URL that can be used to view that image through the
// image proxy.
func (proxy *ImageProxy) GetProxiedImageURL(imageURL string) string {
proxy.lock.RLock()
defer proxy.lock.RUnlock()
return getProxiedImageURL(imageURL, *proxy.ConfigService.Config().ServiceSettings.SiteURL)
}
if proxy.backend == nil {
func getProxiedImageURL(imageURL, siteURL string) string {
if imageURL == "" || imageURL[0] == '/' || strings.HasPrefix(imageURL, siteURL) {
return imageURL
}
return proxy.backend.GetProxiedImageURL(imageURL)
return siteURL + "/api/v4/image?url=" + url.QueryEscape(imageURL)
}
// GetUnproxiedImageURL takes the URL of an image on the image proxy and returns the original URL of the image.
func (proxy *ImageProxy) GetUnproxiedImageURL(proxiedURL string) string {
proxy.lock.RLock()
defer proxy.lock.RUnlock()
return getUnproxiedImageURL(proxiedURL, *proxy.ConfigService.Config().ServiceSettings.SiteURL)
}
if proxy.backend == nil {
func getUnproxiedImageURL(proxiedURL, siteURL string) string {
if !strings.HasPrefix(proxiedURL, siteURL+"/api/v4/image?url=") {
return proxiedURL
}
return proxy.backend.GetUnproxiedImageURL(proxiedURL)
parsed, err := url.Parse(proxiedURL)
if err != nil {
return proxiedURL
}
u := parsed.Query()["url"]
if len(u) == 0 {
return proxiedURL
}
return u[0]
}