Browse Source

pass context to image uploaders

bergquist 8 years ago
parent
commit
c7698a09ed

+ 5 - 5
pkg/components/imguploader/gcsuploader.go

@@ -4,12 +4,13 @@ import (
 	"context"
 	"context"
 	"errors"
 	"errors"
 	"fmt"
 	"fmt"
-	"github.com/grafana/grafana/pkg/log"
-	"github.com/grafana/grafana/pkg/util"
-	"golang.org/x/oauth2/google"
 	"io/ioutil"
 	"io/ioutil"
 	"net/http"
 	"net/http"
 	"os"
 	"os"
+
+	"github.com/grafana/grafana/pkg/log"
+	"github.com/grafana/grafana/pkg/util"
+	"golang.org/x/oauth2/google"
 )
 )
 
 
 type GCSUploader struct {
 type GCSUploader struct {
@@ -26,12 +27,11 @@ func NewGCSUploader(keyFile, bucket string) *GCSUploader {
 	}
 	}
 }
 }
 
 
-func (u *GCSUploader) Upload(imageDiskPath string) (string, error) {
+func (u *GCSUploader) Upload(ctx context.Context, imageDiskPath string) (string, error) {
 	key := util.GetRandomString(20) + ".png"
 	key := util.GetRandomString(20) + ".png"
 
 
 	log.Debug("Opening key file ", u.keyFile)
 	log.Debug("Opening key file ", u.keyFile)
 
 
-	ctx := context.Background()
 	data, err := ioutil.ReadFile(u.keyFile)
 	data, err := ioutil.ReadFile(u.keyFile)
 	if err != nil {
 	if err != nil {
 		return "", err
 		return "", err

+ 3 - 2
pkg/components/imguploader/imguploader.go

@@ -1,6 +1,7 @@
 package imguploader
 package imguploader
 
 
 import (
 import (
+	"context"
 	"fmt"
 	"fmt"
 	"regexp"
 	"regexp"
 
 
@@ -8,13 +9,13 @@ import (
 )
 )
 
 
 type ImageUploader interface {
 type ImageUploader interface {
-	Upload(path string) (string, error)
+	Upload(ctx context.Context, path string) (string, error)
 }
 }
 
 
 type NopImageUploader struct {
 type NopImageUploader struct {
 }
 }
 
 
-func (NopImageUploader) Upload(path string) (string, error) {
+func (NopImageUploader) Upload(ctx context.Context, path string) (string, error) {
 	return "", nil
 	return "", nil
 }
 }
 
 

+ 2 - 1
pkg/components/imguploader/s3uploader.go

@@ -1,6 +1,7 @@
 package imguploader
 package imguploader
 
 
 import (
 import (
+	"context"
 	"os"
 	"os"
 	"time"
 	"time"
 
 
@@ -34,7 +35,7 @@ func NewS3Uploader(region, bucket, acl, accessKey, secretKey string) *S3Uploader
 	}
 	}
 }
 }
 
 
-func (u *S3Uploader) Upload(imageDiskPath string) (string, error) {
+func (u *S3Uploader) Upload(ctx context.Context, imageDiskPath string) (string, error) {
 	sess, err := session.NewSession()
 	sess, err := session.NewSession()
 	if err != nil {
 	if err != nil {
 		return "", err
 		return "", err

+ 2 - 1
pkg/components/imguploader/webdavuploader.go

@@ -2,6 +2,7 @@ package imguploader
 
 
 import (
 import (
 	"bytes"
 	"bytes"
+	"context"
 	"fmt"
 	"fmt"
 	"io/ioutil"
 	"io/ioutil"
 	"net"
 	"net"
@@ -33,7 +34,7 @@ var netClient = &http.Client{
 	Transport: netTransport,
 	Transport: netTransport,
 }
 }
 
 
-func (u *WebdavUploader) Upload(pa string) (string, error) {
+func (u *WebdavUploader) Upload(ctx context.Context, pa string) (string, error) {
 	url, _ := url.Parse(u.url)
 	url, _ := url.Parse(u.url)
 	filename := util.GetRandomString(20) + ".png"
 	filename := util.GetRandomString(20) + ".png"
 	url.Path = path.Join(url.Path, filename)
 	url.Path = path.Join(url.Path, filename)

+ 1 - 1
pkg/services/alerting/notifier.go

@@ -100,7 +100,7 @@ func (n *notificationService) uploadImage(context *EvalContext) (err error) {
 		context.ImageOnDiskPath = imagePath
 		context.ImageOnDiskPath = imagePath
 	}
 	}
 
 
-	context.ImagePublicUrl, err = uploader.Upload(context.ImageOnDiskPath)
+	context.ImagePublicUrl, err = uploader.Upload(context.Ctx, context.ImageOnDiskPath)
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}