Skip to content

Commit

Permalink
webdav: allow specification of a different public_url than upload url
Browse files Browse the repository at this point in the history
  • Loading branch information
fir4 authored and daniellee committed Apr 25, 2017
1 parent 368e847 commit 6d9e8bd
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions conf/defaults.ini
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,4 @@ secret_key =
url =
username =
password =
public_url =
1 change: 1 addition & 0 deletions conf/sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -399,5 +399,6 @@

[external_image_storage.webdav]
;url =
;public_url =
;username =
;password =
3 changes: 3 additions & 0 deletions docs/sources/installation/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion pkg/components/imguploader/imguploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 16 additions & 9 deletions pkg/components/imguploader/webdavuploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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))
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion pkg/components/imguploader/webdavuploader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 6d9e8bd

Please sign in to comment.