forked from lengzuo/supa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
49 lines (42 loc) · 1.46 KB
/
storage.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
package supabase
import (
"context"
"fmt"
"io"
"net/http"
"github.com/lengzuo/supa/pkg/catch"
"github.com/lengzuo/supa/pkg/httpclient"
"github.com/lengzuo/supa/pkg/logger"
"github.com/lengzuo/supa/utils/enum"
)
type storageAPI interface {
GetPublicUrl(mediaPath string) string
UploadFile(ctx context.Context, targetFilePath, mimeType string, fileData io.Reader) error
}
type storage struct {
client
bucket string
}
func newStorage(c client, bucket string) *storage {
return &storage{c, bucket}
}
func (i *storage) UploadFile(ctx context.Context, targetFilePath, mimeType string, fileData io.Reader) error {
reqURL := fmt.Sprintf("%s/object/%s/%s", i.storageHost, i.bucket, targetFilePath)
httpResp, err := i.httpClient.Upload(ctx, reqURL, http.MethodPost, fileData, func(req *http.Request) {
req.Header.Set(authorizationHeader, i.apiKey)
req.Header.Set(enum.Authorization.String(), fmt.Sprintf("%s %s", authPrefix, i.apiKey))
req.Header.Set(enum.ContentType.String(), mimeType)
})
if err != nil {
logger.Logger.Error("failed in httpclient call with catch: %s", err)
return err
}
if !httpclient.IsHTTPSuccess(httpResp.StatusCode) {
logger.Logger.Warn("getting %d in sign out due to catch: %s", httpResp.StatusCode, httpResp.Body.String())
return catch.External(httpResp.Body.Bytes(), httpResp.StatusCode)
}
return nil
}
func (i *storage) GetPublicUrl(mediaPath string) string {
return i.storageHost + "/object/public/" + i.bucket + "/" + mediaPath
}