-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapiwrapper.go
182 lines (147 loc) · 3.98 KB
/
apiwrapper.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package gdrive // nolint: golint
import (
"bytes"
"fmt"
"sync/atomic"
log "github.com/fclairamb/go-log"
"google.golang.org/api/drive/v3"
"google.golang.org/api/googleapi"
"github.com/fclairamb/afero-gdrive/cache"
)
// APIWrapper allows to wrap some GDrive API calls to perform some caching
type APIWrapper struct {
UseCache bool
srv *drive.Service
cache *cache.Cache
logger log.Logger
calls map[string]*int32
}
// NewAPIWrapper instantiates a new APIWrapper
func NewAPIWrapper(srv *drive.Service, logger log.Logger) *APIWrapper {
return &APIWrapper{
srv: srv,
cache: cache.NewCache(),
logger: logger,
calls: map[string]*int32{
"Files.Create": new(int32),
"Files.Update": new(int32),
"Files.Delete": new(int32),
"Files.List": new(int32),
},
UseCache: true,
}
}
func (a *APIWrapper) calling(apiName string) {
atomic.AddInt32(a.calls[apiName], 1)
}
// TotalNbCalls returns the total number of calls performed to the API
func (a *APIWrapper) TotalNbCalls() int {
nb := int32(0)
for _, c := range a.calls {
nb += *c
}
return int(nb)
}
// createFile wraps a call to the Files.Create
func (a *APIWrapper) createFile(
folderID string,
fileName string,
mimeType string,
fields ...googleapi.Field,
) (*drive.File, error) {
a.calling("Files.Create")
call := a.srv.Files.Create(&drive.File{
Name: sanitizeName(fileName),
MimeType: mimeType,
Description: "Created by https://github.com/fclairamb/afero-gdrive",
Parents: []string{
folderID,
},
}).Fields(fields...)
if mimeType != mimeTypeFolder {
call.Media(bytes.NewReader([]byte{}))
}
file, err := call.Do()
if err == nil {
a.cache.CleanupByPrefix(fmt.Sprintf("%s-", folderID))
} else {
err = &DriveAPICallError{Err: err}
}
return file, err
}
// nolint: unused
func (a *APIWrapper) renameFile(file *drive.File, targetFolder *drive.File, targetName string) error {
a.calling("Files.Update")
call := a.srv.Files.Update(
file.Id,
&drive.File{
Name: sanitizeName(targetName),
},
)
if file.Parents[0] != targetFolder.Id {
call = call.
RemoveParents(file.Parents[0]).
AddParents(targetFolder.Id)
}
_, err := call.Do()
if err != nil {
return &DriveAPICallError{Err: err}
}
// Removing cache of source and target folders
a.cache.CleanupByPrefix(fmt.Sprintf("%s-", file.Parents[0]))
a.cache.CleanupByPrefix(fmt.Sprintf("%s-", targetFolder.Id))
return nil
}
// deleteFile wraps a call to Files.Update or Files.Delete
// To keep it simple and yet true, when a folder is deleted the entire cache is trashed
func (a *APIWrapper) deleteFile(file *drive.File, trash bool) error {
var err error
if trash {
a.calling("Files.Update")
_, err = a.srv.Files.Update(file.Id, &drive.File{Trashed: true}).Do()
} else {
a.calling("Files.Delete")
err = a.srv.Files.Delete(file.Id).Do()
}
if err != nil {
return &DriveAPICallError{Err: err}
}
if file.MimeType == mimeTypeFolder {
a.cache.CleanupEverything()
} else {
for _, p := range file.Parents {
a.cache.CleanupByPrefix(p)
}
}
return nil
}
func (a *APIWrapper) getFileByFolderAndName(
folderID string,
fileName string,
fields ...googleapi.Field,
) (*drive.FileList, error) {
queryFields := googleapi.CombineFields(fields)
if queryFields == "" {
queryFields = "files(id,mimeType,parents)"
}
cacheKey := fmt.Sprintf("%s-getFileByFolderAndName-%s-%s", folderID, fileName, queryFields)
value, ok := a.cache.Get(cacheKey)
if ok {
return value.(*drive.FileList), nil
}
fileList, err := a._getFileByFolderAndName(folderID, fileName, googleapi.Field(queryFields))
if err == nil && a.UseCache {
a.cache.Set(cacheKey, fileList)
}
return fileList, err
}
func (a *APIWrapper) _getFileByFolderAndName(
folderID string,
fileName string,
fields googleapi.Field,
) (*drive.FileList, error) {
a.calling("Files.List")
query := fmt.Sprintf("'%s' in parents and name='%s' and trashed = false", folderID, sanitizeName(fileName))
call := a.srv.Files.List().Q(query).Fields(fields)
return call.Do()
}