mirror of
https://github.com/grafana/grafana.git
synced 2024-11-22 08:56:43 -06:00
webdav: allow specification of a different public_url than upload url
This commit is contained in:
parent
368e847d12
commit
6d9e8bd147
@ -444,3 +444,4 @@ secret_key =
|
|||||||
url =
|
url =
|
||||||
username =
|
username =
|
||||||
password =
|
password =
|
||||||
|
public_url =
|
||||||
|
@ -399,5 +399,6 @@
|
|||||||
|
|
||||||
[external_image_storage.webdav]
|
[external_image_storage.webdav]
|
||||||
;url =
|
;url =
|
||||||
|
;public_url =
|
||||||
;username =
|
;username =
|
||||||
;password =
|
;password =
|
||||||
|
@ -644,6 +644,9 @@ Secret key. e.g. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
|||||||
### url
|
### url
|
||||||
Url to where Grafana will send PUT request with images
|
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
|
### username
|
||||||
basic auth username
|
basic auth username
|
||||||
|
|
||||||
|
@ -47,10 +47,11 @@ func NewImageUploader() (ImageUploader, error) {
|
|||||||
return nil, fmt.Errorf("Could not find url key for image.uploader.webdav")
|
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()
|
username := webdavSec.Key("username").String()
|
||||||
password := webdavSec.Key("password").String()
|
password := webdavSec.Key("password").String()
|
||||||
|
|
||||||
return NewWebdavImageUploader(url, username, password)
|
return NewWebdavImageUploader(url, username, password, public_url)
|
||||||
}
|
}
|
||||||
|
|
||||||
return NopImageUploader{}, nil
|
return NopImageUploader{}, nil
|
||||||
|
@ -14,9 +14,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type WebdavUploader struct {
|
type WebdavUploader struct {
|
||||||
url string
|
url string
|
||||||
username string
|
username string
|
||||||
password string
|
password string
|
||||||
|
public_url string
|
||||||
}
|
}
|
||||||
|
|
||||||
var netTransport = &http.Transport{
|
var netTransport = &http.Transport{
|
||||||
@ -33,7 +34,8 @@ var netClient = &http.Client{
|
|||||||
|
|
||||||
func (u *WebdavUploader) Upload(pa string) (string, error) {
|
func (u *WebdavUploader) Upload(pa string) (string, error) {
|
||||||
url, _ := url.Parse(u.url)
|
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)
|
imgData, err := ioutil.ReadFile(pa)
|
||||||
req, err := http.NewRequest("PUT", url.String(), bytes.NewReader(imgData))
|
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 "", 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{
|
return &WebdavUploader{
|
||||||
url: url,
|
url: url,
|
||||||
username: username,
|
username: username,
|
||||||
password: passwrod,
|
password: password,
|
||||||
|
public_url: public_url,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestUploadToWebdav(t *testing.T) {
|
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() {
|
SkipConvey("[Integration test] for external_image_store.webdav", t, func() {
|
||||||
path, err := webdavUploader.Upload("../../../public/img/logo_transparent_400x.png")
|
path, err := webdavUploader.Upload("../../../public/img/logo_transparent_400x.png")
|
||||||
|
Loading…
Reference in New Issue
Block a user