-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
gcloudstorage.go
141 lines (130 loc) · 3.24 KB
/
gcloudstorage.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package gcloudstorage
import (
"cloud.google.com/go/storage"
"context"
"errors"
"github.com/cshum/imagor"
"github.com/cshum/imagor/imagorpath"
"io"
"net/http"
"path/filepath"
"strings"
"time"
)
type GCloudStorage struct {
BaseDir string
PathPrefix string
ACL string
SafeChars string
Expiration time.Duration
client *storage.Client
Bucket string
safeChars imagorpath.SafeChars
}
type statKey struct {
Key string
}
func New(client *storage.Client, bucket string, options ...Option) *GCloudStorage {
s := &GCloudStorage{client: client, Bucket: bucket}
for _, option := range options {
option(s)
}
s.safeChars = imagorpath.NewSafeChars(s.SafeChars)
return s
}
func (s *GCloudStorage) Get(r *http.Request, image string) (imageData *imagor.Blob, err error) {
ctx := r.Context()
image, ok := s.Path(image)
if !ok {
return nil, imagor.ErrInvalid
}
object := s.client.Bucket(s.Bucket).Object(image)
attrs, err := object.Attrs(ctx)
if err != nil {
if errors.Is(err, storage.ErrObjectNotExist) {
return nil, imagor.ErrNotFound
}
return nil, err
}
if attrs != nil {
imagor.ContextCachePut(ctx, statKey{image}, imagor.Stat{
Size: attrs.Size,
ModifiedTime: attrs.Updated,
})
}
if s.Expiration > 0 {
if attrs != nil && time.Now().Sub(attrs.Updated) > s.Expiration {
return nil, imagor.ErrExpired
}
}
return imagor.NewBlob(func() (reader io.ReadCloser, size int64, err error) {
if attrs != nil {
size = attrs.Size
}
reader, err = object.NewReader(ctx)
return
}), err
}
func (s *GCloudStorage) Put(ctx context.Context, image string, blob *imagor.Blob) (err error) {
image, ok := s.Path(image)
if !ok {
return imagor.ErrInvalid
}
reader, _, err := blob.NewReader()
if err != nil {
return err
}
objectHandle := s.client.Bucket(s.Bucket).Object(image)
writer := objectHandle.NewWriter(ctx)
defer func() {
_ = reader.Close()
_ = writer.Close()
}()
if s.ACL != "" {
writer.PredefinedACL = s.ACL
}
writer.ContentType = blob.ContentType()
if _, err = io.Copy(writer, reader); err != nil {
return err
}
return
}
func (s *GCloudStorage) Delete(ctx context.Context, image string) error {
image, ok := s.Path(image)
if !ok {
return imagor.ErrInvalid
}
return s.client.Bucket(s.Bucket).Object(image).Delete(ctx)
}
func (s *GCloudStorage) Path(image string) (string, bool) {
image = "/" + imagorpath.Normalize(image, s.safeChars)
if !strings.HasPrefix(image, s.PathPrefix) {
return "", false
}
joinedPath := filepath.Join(s.BaseDir, strings.TrimPrefix(image, s.PathPrefix))
// Google cloud paths don't need to start with "/"
return strings.Trim(joinedPath, "/"), true
}
func (s *GCloudStorage) Stat(ctx context.Context, image string) (stat *imagor.Stat, err error) {
image, ok := s.Path(image)
if !ok {
return nil, imagor.ErrInvalid
}
if s, ok := imagor.ContextCacheGet(ctx, statKey{image}); ok && s != nil {
if stat, ok2 := s.(imagor.Stat); ok2 {
return &stat, nil
}
}
object := s.client.Bucket(s.Bucket).Object(image)
attrs, err := object.Attrs(ctx)
if err != nil {
if errors.Is(err, storage.ErrObjectNotExist) {
return nil, imagor.ErrNotFound
}
return nil, err
}
return &imagor.Stat{
Size: attrs.Size,
ModifiedTime: attrs.Updated,
}, nil
}