forked from plexdrive/plexdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.go
317 lines (263 loc) · 8.22 KB
/
drive.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"context"
"fmt"
"net/http"
gdrive "google.golang.org/api/drive/v3"
"time"
"strings"
. "github.com/claudetech/loggo/default"
"golang.org/x/oauth2"
"google.golang.org/api/googleapi"
)
// Fields are the fields that should be returned by the Google Drive API
var Fields string
// BlackListObjects is a list of blacklisted items that will not be
// fetched from cache or the API
var BlackListObjects map[string]bool
// init initializes the global configurations
func init() {
Fields = "id, name, mimeType, modifiedTime, size, explicitlyTrashed, parents"
BlackListObjects = make(map[string]bool)
BlackListObjects[".git"] = true
BlackListObjects["HEAD"] = true
BlackListObjects[".Trash"] = true
BlackListObjects[".Trash-1000"] = true
BlackListObjects[".metadata_never_index"] = true
}
// Drive holds the Google Drive API connection(s)
type Drive struct {
cache *Cache
context context.Context
token *oauth2.Token
config *oauth2.Config
}
// NewDriveClient creates a new Google Drive client
func NewDriveClient(config *Config, cache *Cache, refreshInterval time.Duration) (*Drive, error) {
drive := Drive{
cache: cache,
context: context.Background(),
config: &oauth2.Config{
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
},
RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
Scopes: []string{gdrive.DriveScope},
},
}
if err := drive.authorize(); nil != err {
return nil, err
}
go drive.startWatchChanges(refreshInterval)
return &drive, nil
}
func (d *Drive) startWatchChanges(refreshInterval time.Duration) {
client, err := d.getClient()
if nil != err {
Log.Debugf("%v", err)
Log.Warningf("Could not get Google Drive client to watch for changes")
return
}
checkChanges := func(firstCheck bool) {
Log.Debugf("Checking for changes")
// get the last token
pageToken, err := d.cache.GetStartPageToken()
if nil != err {
pageToken = "1"
Log.Info("No last change id found, starting from beginning...")
} else {
Log.Debugf("Last change id found, continuing getting changes (%v)", pageToken)
}
if firstCheck {
Log.Infof("First cache build process started...")
}
deletedItems := 0
updatedItems := 0
processedItems := 0
for {
query := client.Changes.
List(pageToken).
Fields(googleapi.Field(fmt.Sprintf("nextPageToken, newStartPageToken, changes(removed, fileId, file(%v))", Fields))).
PageSize(1000).
IncludeRemoved(true)
results, err := query.Do()
if nil != err {
Log.Debugf("%v", err)
Log.Warningf("Could not get changes")
break
}
for _, change := range results.Changes {
Log.Tracef("Change %v", change)
if change.Removed || (nil != change.File && change.File.ExplicitlyTrashed) {
d.cache.DeleteObject(change.FileId)
deletedItems++
} else {
object, err := d.mapFileToObject(change.File)
if nil != err {
Log.Debugf("%v", err)
Log.Warningf("Could not map Google Drive file to object")
} else {
err := d.cache.UpdateObject(object)
if nil != err {
Log.Debugf("%v", err)
Log.Warningf("Could not update object %v", object.ObjectID)
}
updatedItems++
}
}
processedItems++
}
if processedItems > 0 {
Log.Infof("Processed %v items / deleted %v items / updated %v items",
processedItems, deletedItems, updatedItems)
}
if "" != results.NextPageToken {
pageToken = results.NextPageToken
d.cache.StoreStartPageToken(pageToken)
} else {
pageToken = results.NewStartPageToken
d.cache.StoreStartPageToken(pageToken)
break
}
}
if firstCheck {
Log.Infof("First cache build process finished!")
}
}
checkChanges(true)
for _ = range time.Tick(refreshInterval) {
checkChanges(false)
}
}
func (d *Drive) authorize() error {
Log.Debugf("Authorizing against Google Drive API")
token, err := d.cache.LoadToken()
if nil != err {
Log.Debugf("Token could not be found, fetching new one")
t, err := getTokenFromWeb(d.config)
if nil != err {
return err
}
token = t
if err := d.cache.StoreToken(token); nil != err {
return err
}
}
d.token = token
return nil
}
// getTokenFromWeb uses Config to request a Token.
// It returns the retrieved Token.
func getTokenFromWeb(config *oauth2.Config) (*oauth2.Token, error) {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the following link in your browser %v\n", authURL)
fmt.Printf("Paste the authorization code: ")
var code string
if _, err := fmt.Scan(&code); err != nil {
return nil, fmt.Errorf("Unable to read authorization code %v", err)
}
tok, err := config.Exchange(oauth2.NoContext, code)
if err != nil {
return nil, fmt.Errorf("Unable to retrieve token from web %v", err)
}
return tok, err
}
// getClient gets a new Google Drive client
func (d *Drive) getClient() (*gdrive.Service, error) {
return gdrive.New(d.config.Client(d.context, d.token))
}
// getNativeClient gets a native http client
func (d *Drive) getNativeClient() *http.Client {
return oauth2.NewClient(d.context, d.config.TokenSource(d.context, d.token))
}
// GetRoot gets the root node directly from the API
func (d *Drive) GetRoot() (*APIObject, error) {
Log.Debugf("Getting root from API")
id := "root"
client, err := d.getClient()
if nil != err {
Log.Debugf("%v", err)
return nil, fmt.Errorf("Could not get Google Drive client")
}
file, err := client.Files.
Get(id).
Fields(googleapi.Field(Fields)).
Do()
if nil != err {
Log.Debugf("%v", err)
return nil, fmt.Errorf("Could not get object %v from API", id)
}
// getting file size
if file.MimeType != "application/vnd.google-apps.folder" && 0 == file.Size {
res, err := client.Files.Get(id).Download()
if nil != err {
Log.Debugf("%v", err)
return nil, fmt.Errorf("Could not get file size for object %v", id)
}
file.Size = res.ContentLength
}
return d.mapFileToObject(file)
}
// GetObject gets an object by id
func (d *Drive) GetObject(id string) (*APIObject, error) {
return d.cache.GetObject(id)
}
// GetObjectsByParent get all objects under parent id
func (d *Drive) GetObjectsByParent(parent string) ([]*APIObject, error) {
return d.cache.GetObjectsByParent(parent)
}
// GetObjectByParentAndName finds a child element by name and its parent id
func (d *Drive) GetObjectByParentAndName(parent, name string) (*APIObject, error) {
if _, exists := BlackListObjects[name]; exists {
return nil, fmt.Errorf("Object %v is blacklisted and will not be returned", name)
}
return d.cache.GetObjectByParentAndName(parent, name)
}
// Open a file
func (d *Drive) Open(object *APIObject) (*Buffer, error) {
nativeClient := d.getNativeClient()
return GetBufferInstance(nativeClient, object)
}
// Remove removes file from Google Drive
func (d *Drive) Remove(object *APIObject) error {
client, err := d.getClient()
if nil != err {
Log.Debugf("%v", err)
return fmt.Errorf("Could not get Google Drive client")
}
if err := client.Files.Delete(object.ObjectID).Do(); nil != err {
Log.Debugf("%v", err)
return fmt.Errorf("Could not delete object %v from API", object.Name)
}
if err := d.cache.DeleteObject(object.ObjectID); nil != err {
Log.Debugf("%v", err)
return fmt.Errorf("Could not delete object %v from cache", object.Name)
}
return nil
}
// mapFileToObject maps a Google Drive file to APIObject
func (d *Drive) mapFileToObject(file *gdrive.File) (*APIObject, error) {
Log.Tracef("Converting Google Drive file: %v", file)
lastModified, err := time.Parse(time.RFC3339, file.ModifiedTime)
if nil != err {
Log.Debugf("%v", err)
Log.Warningf("Could not parse last modified date for object %v", file.Id)
lastModified = time.Now()
}
var parents []string
for _, parent := range file.Parents {
parents = append(parents, parent)
}
return &APIObject{
ObjectID: file.Id,
Name: file.Name,
IsDir: file.MimeType == "application/vnd.google-apps.folder",
LastModified: lastModified,
Size: uint64(file.Size),
DownloadURL: fmt.Sprintf("https://www.googleapis.com/drive/v3/files/%v?alt=media", file.Id),
Parents: fmt.Sprintf("|%v|", strings.Join(parents, "|")),
}, nil
}