Commit 1188f8df by Konstantin Chukhlomin Committed by bergquist

gofmt fixes

parent 72d7c4d6
package imguploader package imguploader
import ( import (
"github.com/grafana/grafana/pkg/log" "cloud.google.com/go/storage"
"github.com/grafana/grafana/pkg/util" "fmt"
"google.golang.org/api/option" "github.com/grafana/grafana/pkg/log"
"cloud.google.com/go/storage" "github.com/grafana/grafana/pkg/util"
"golang.org/x/net/context" "golang.org/x/net/context"
"io/ioutil" "google.golang.org/api/option"
"fmt" "io/ioutil"
) )
type GCPUploader struct { type GCPUploader struct {
keyFile string keyFile string
bucket string bucket string
log log.Logger log log.Logger
} }
func NewGCPUploader(keyFile, bucket string) *GCPUploader { func NewGCPUploader(keyFile, bucket string) *GCPUploader {
return &GCPUploader{ return &GCPUploader{
keyFile: keyFile, keyFile: keyFile,
bucket: bucket, bucket: bucket,
log: log.New("gcpuploader"), log: log.New("gcpuploader"),
} }
} }
func (u *GCPUploader) Upload(imageDiskPath string) (string, error) { func (u *GCPUploader) Upload(imageDiskPath string) (string, error) {
ctx := context.Background() ctx := context.Background()
client, err := storage.NewClient(ctx, option.WithServiceAccountFile(u.keyFile)) client, err := storage.NewClient(ctx, option.WithServiceAccountFile(u.keyFile))
if err != nil { if err != nil {
return "", err return "", err
} }
key := util.GetRandomString(20) + ".png" key := util.GetRandomString(20) + ".png"
log.Debug("Uploading image to GCP bucket = %s key = %s", u.bucket, key) log.Debug("Uploading image to GCP bucket = %s key = %s", u.bucket, key)
file, err := ioutil.ReadFile(imageDiskPath) file, err := ioutil.ReadFile(imageDiskPath)
if err != nil { if err != nil {
return "", err return "", err
} }
wc := client.Bucket(u.bucket).Object(key).NewWriter(ctx) wc := client.Bucket(u.bucket).Object(key).NewWriter(ctx)
wc.ContentType = "image/png" wc.ContentType = "image/png"
wc.ACL = []storage.ACLRule{{Entity: storage.AllUsers, Role: storage.RoleReader}} wc.ACL = []storage.ACLRule{{Entity: storage.AllUsers, Role: storage.RoleReader}}
if _, err := wc.Write(file); err != nil { if _, err := wc.Write(file); err != nil {
return "", err return "", err
} }
if err := wc.Close(); err != nil { if err := wc.Close(); err != nil {
return "", err return "", err
} }
return fmt.Sprintf("https://storage.googleapis.com/%s/%s", u.bucket, key), nil return fmt.Sprintf("https://storage.googleapis.com/%s/%s", u.bucket, key), nil
} }
...@@ -52,16 +52,16 @@ func NewImageUploader() (ImageUploader, error) { ...@@ -52,16 +52,16 @@ func NewImageUploader() (ImageUploader, error) {
password := webdavSec.Key("password").String() password := webdavSec.Key("password").String()
return NewWebdavImageUploader(url, username, password, public_url) return NewWebdavImageUploader(url, username, password, public_url)
case "gcp": case "gcp":
gcpsec, err := setting.Cfg.GetSection("external_image_storage.gcp") gcpsec, err := setting.Cfg.GetSection("external_image_storage.gcp")
if err != nil { if err != nil {
return nil, err return nil, err
} }
keyFile := gcpsec.Key("key_file").MustString("") keyFile := gcpsec.Key("key_file").MustString("")
bucketName := gcpsec.Key("bucket").MustString("") bucketName := gcpsec.Key("bucket").MustString("")
return NewGCPUploader(keyFile, bucketName), nil return NewGCPUploader(keyFile, bucketName), nil
} }
return NopImageUploader{}, nil return NopImageUploader{}, nil
......
...@@ -97,7 +97,7 @@ func TestImageUploaderFactory(t *testing.T) { ...@@ -97,7 +97,7 @@ func TestImageUploaderFactory(t *testing.T) {
So(original.password, ShouldEqual, "password") So(original.password, ShouldEqual, "password")
}) })
Convey("GCP uploader", func() { Convey("GCP uploader", func() {
var err error var err error
setting.NewConfigContext(&setting.CommandLineArgs{ setting.NewConfigContext(&setting.CommandLineArgs{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment