pass context to image uploaders

This commit is contained in:
bergquist 2017-09-15 15:05:48 +02:00
parent d0ab028cfc
commit c7698a09ed
5 changed files with 13 additions and 10 deletions

View File

@ -4,12 +4,13 @@ import (
"context"
"errors"
"fmt"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/util"
"golang.org/x/oauth2/google"
"io/ioutil"
"net/http"
"os"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/util"
"golang.org/x/oauth2/google"
)
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"
log.Debug("Opening key file ", u.keyFile)
ctx := context.Background()
data, err := ioutil.ReadFile(u.keyFile)
if err != nil {
return "", err

View File

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

View File

@ -1,6 +1,7 @@
package imguploader
import (
"context"
"os"
"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()
if err != nil {
return "", err

View File

@ -2,6 +2,7 @@ package imguploader
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net"
@ -33,7 +34,7 @@ var netClient = &http.Client{
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)
filename := util.GetRandomString(20) + ".png"
url.Path = path.Join(url.Path, filename)

View File

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