-
Notifications
You must be signed in to change notification settings - Fork 1
/
uploader.go
73 lines (57 loc) · 1.66 KB
/
uploader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package uploader
import (
"bytes"
"context"
"fmt"
"io"
"time"
_ "embed"
"cloud.google.com/go/storage"
"google.golang.org/api/option"
)
//go:embed gcp-credentials.json
var credentials []byte
const (
CACHE_CONTROL = "Cache-Control:private, max-age=0, no-transform" // disables bucket caching
CONTENT_TYPE = "image/svg+xml"
)
type GoogleBucketUploader struct {
bucket string
objectPath string
}
func NewGoogleBucketUploader(bucket, objectPath string) *GoogleBucketUploader {
return &GoogleBucketUploader{
bucket: bucket,
objectPath: objectPath,
}
}
// UploadSVG uploads an SVG by using google storage package.
func (u *GoogleBucketUploader) UploadSVG(pair, svg string) error {
err := fileUpload(u.bucket, fmt.Sprintf("%v/%v/chart.svg", u.objectPath, pair), []byte(svg))
if err != nil {
return err
}
// TODO: add flag to disable uploading
// os.WriteFile(fmt.Sprintf("%v.html", pair), []byte(svg), 0644)
return nil
}
func fileUpload(bucket, object string, data []byte) error {
ctx := context.Background()
client, err := storage.NewClient(ctx, option.WithCredentialsJSON(credentials))
if err != nil {
return fmt.Errorf("storage.NewClient: %w", err)
}
defer client.Close()
ctx, cancel := context.WithTimeout(ctx, time.Second*3)
defer cancel()
bucketWriter := client.Bucket(bucket).Object(object).NewWriter(ctx)
defer bucketWriter.Close()
bucketWriter.ChunkSize = 0 // note retries are not supported for chunk size 0.
bucketWriter.CacheControl = CACHE_CONTROL
bucketWriter.ContentType = CONTENT_TYPE
buf := bytes.NewBuffer(data)
if _, err = io.Copy(bucketWriter, buf); err != nil {
return fmt.Errorf("io.Copy: %w", err)
}
return nil
}