-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploader.go
73 lines (62 loc) · 1.74 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 robots
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"path"
"cloud.google.com/go/storage"
)
const bucketName = "media.geomodul.us"
type Uploader struct {
client *storage.Client
slackToken string
prefix string
}
func NewUploader(ctx context.Context, slackToken string, prefix string) (*Uploader, error) {
client, err := storage.NewClient(ctx)
if err != nil {
return nil, err
}
return &Uploader{
client: client,
slackToken: slackToken,
prefix: prefix,
}, nil
}
func (u *Uploader) Upload(ctx context.Context, slug, downloadURL string) (string, error) {
var objectKey string
if slug == "" {
objectKey = fmt.Sprintf("img/%s", path.Base(downloadURL))
} else {
parsedURL, err := url.Parse(downloadURL)
if err != nil {
return "", fmt.Errorf("url.Parse: %v", err)
}
objectKey = fmt.Sprintf("%s/%s/%s", u.prefix, slug, path.Base(parsedURL.Path))
}
// Create a new HTTP request to download the file.
req, err := http.NewRequest("GET", downloadURL, nil)
if err != nil {
return "", fmt.Errorf("http.NewRequest: %v", err)
}
// Add the authorization header to the request.
req.Header.Add("Authorization", "Bearer "+u.slackToken)
// Do the request.
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", fmt.Errorf("http.DefaultClient.Do: %v", err)
}
defer resp.Body.Close()
// Write the file to the specified GCS bucket.
wc := u.client.Bucket(bucketName).Object(objectKey).NewWriter(ctx)
if _, err = io.Copy(wc, resp.Body); err != nil {
return "", fmt.Errorf("io.Copy: %v", err)
}
if err := wc.Close(); err != nil {
return "", fmt.Errorf("Writer.Close: %v", err)
}
fmt.Printf("Blob %s uploaded.\n", wc.Attrs().Name)
return fmt.Sprintf("https://%s/%s", bucketName, objectKey), nil
}