-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
61 lines (49 loc) · 1.23 KB
/
upload.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
package firebase
import (
"context"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"cloud.google.com/go/storage"
)
func (c *Firebase) UploadFile(ctx context.Context, file multipart.File, name, contentType string) (string, error) {
bucket, err := c.Storage.DefaultBucket()
if err != nil {
return "", err
}
oh := bucket.Object(name)
writer := oh.NewWriter(ctx)
writer.ObjectAttrs = storage.ObjectAttrs{
Name: name,
ContentType: contentType,
}
if _, err := io.Copy(writer, file); err != nil {
return "", err
}
if err := writer.Close(); err != nil {
return "", err
}
metaURL := fmt.Sprintf("https://firebasestorage.googleapis.com/v0/b/%s/o/%s", oh.BucketName(), url.QueryEscape(oh.ObjectName()))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, metaURL, nil)
if err != nil {
return "", err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
var meta map[string]any
if err := json.Unmarshal(b, &meta); err != nil {
return "", err
}
downloadURL := fmt.Sprintf("%s?alt=media&token=%s", metaURL, meta["downloadTokens"].(string))
return downloadURL, nil
}