forked from perkeep/perkeep
/
drive.go
89 lines (76 loc) · 2.43 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
/*
Copyright 2013 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Package drive registers the "googledrive" blobserver storage
type, storing blobs in a Google Drive folder.
Example low-level config:
"/storage-googledrive/": {
"handler": "storage-googledrive",
"handlerArgs": map[string]interface{}{
"parent_id": parentId,
"auth": map[string]interface{}{
"client_id": clientId,
"client_secret": clientSecret,
"refresh_token": refreshToken,
},
},
},
*/
package drive
import (
"net/http"
"time"
"camlistore.org/pkg/blobserver"
"camlistore.org/pkg/blobserver/google/drive/service"
"camlistore.org/pkg/jsonconfig"
"camlistore.org/third_party/code.google.com/p/goauth2/oauth"
)
const (
GoogleOAuth2AuthURL = "https://accounts.google.com/o/oauth2/auth"
GoogleOAuth2TokenURL = "https://accounts.google.com/o/oauth2/token"
)
type driveStorage struct {
service *service.DriveService
}
func newFromConfig(_ blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, error) {
auth := config.RequiredObject("auth")
oauthConf := &oauth.Config{
ClientId: auth.RequiredString("client_id"),
ClientSecret: auth.RequiredString("client_secret"),
AuthURL: GoogleOAuth2AuthURL,
TokenURL: GoogleOAuth2TokenURL,
}
// force refreshes the access token on start, make sure
// refresh request in parallel are being started
transport := &oauth.Transport{
Token: &oauth.Token{
AccessToken: "",
RefreshToken: auth.RequiredString("refresh_token"),
Expiry: time.Now(),
},
Config: oauthConf,
Transport: http.DefaultTransport,
}
parent := config.RequiredString("parent_id")
if err := config.Validate(); err != nil {
return nil, err
}
service, err := service.New(transport, parent)
sto := &driveStorage{
service: service,
}
return sto, err
}
func init() {
blobserver.RegisterStorageConstructor("googledrive", blobserver.StorageConstructor(newFromConfig))
}