webdav: allow specification of a different public_url than upload url

This commit is contained in:
fir4 2017-03-23 16:23:46 +01:00 committed by Daniel Lee
parent 368e847d12
commit 6d9e8bd147
6 changed files with 24 additions and 11 deletions

View File

@ -444,3 +444,4 @@ secret_key =
url =
username =
password =
public_url =

View File

@ -399,5 +399,6 @@
[external_image_storage.webdav]
;url =
;public_url =
;username =
;password =

View File

@ -644,6 +644,9 @@ Secret key. e.g. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
### url
Url to where Grafana will send PUT request with images
### public_url
Url to send to users in notifications, directly appended with the resulting uploaded file name
### username
basic auth username

View File

@ -47,10 +47,11 @@ func NewImageUploader() (ImageUploader, error) {
return nil, fmt.Errorf("Could not find url key for image.uploader.webdav")
}
public_url := webdavSec.Key("public_url").String()
username := webdavSec.Key("username").String()
password := webdavSec.Key("password").String()
return NewWebdavImageUploader(url, username, password)
return NewWebdavImageUploader(url, username, password, public_url)
}
return NopImageUploader{}, nil

View File

@ -14,9 +14,10 @@ import (
)
type WebdavUploader struct {
url string
username string
password string
url string
username string
password string
public_url string
}
var netTransport = &http.Transport{
@ -33,7 +34,8 @@ var netClient = &http.Client{
func (u *WebdavUploader) Upload(pa string) (string, error) {
url, _ := url.Parse(u.url)
url.Path = path.Join(url.Path, util.GetRandomString(20)+".png")
filename := util.GetRandomString(20) + ".png"
url.Path = path.Join(url.Path, filename)
imgData, err := ioutil.ReadFile(pa)
req, err := http.NewRequest("PUT", url.String(), bytes.NewReader(imgData))
@ -53,13 +55,18 @@ func (u *WebdavUploader) Upload(pa string) (string, error) {
return "", fmt.Errorf("Failed to upload image. Returned statuscode %v body %s", res.StatusCode, body)
}
return url.String(), nil
if u.public_url != "" {
return (u.public_url + filename), nil
} else {
return url.String(), nil
}
}
func NewWebdavImageUploader(url, username, passwrod string) (*WebdavUploader, error) {
func NewWebdavImageUploader(url, username, password, public_url string) (*WebdavUploader, error) {
return &WebdavUploader{
url: url,
username: username,
password: passwrod,
url: url,
username: username,
password: password,
public_url: public_url,
}, nil
}

View File

@ -7,7 +7,7 @@ import (
)
func TestUploadToWebdav(t *testing.T) {
webdavUploader, _ := NewWebdavImageUploader("http://localhost:9998/dav/", "username", "password")
webdavUploader, _ := NewWebdavImageUploader("http://localhost:9998/dav/", "username", "password", "")
SkipConvey("[Integration test] for external_image_store.webdav", t, func() {
path, err := webdavUploader.Upload("../../../public/img/logo_transparent_400x.png")