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 package drive
36
37 import (
38 "go4.org/jsonconfig"
39 "perkeep.org/pkg/blobserver"
40 "perkeep.org/pkg/blobserver/google/drive/service"
41
42 "context"
43
44 "go4.org/oauthutil"
45 "golang.org/x/oauth2"
46 "golang.org/x/oauth2/google"
47 )
48
49
50 const Scope = "https://www.googleapis.com/auth/drive"
51
52 type driveStorage struct {
53 service *service.DriveService
54 }
55
56 func newFromConfig(_ blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, error) {
57 auth := config.RequiredObject("auth")
58 oAuthClient := oauth2.NewClient(context.Background(), oauthutil.NewRefreshTokenSource(&oauth2.Config{
59 Scopes: []string{Scope},
60 Endpoint: google.Endpoint,
61 ClientID: auth.RequiredString("client_id"),
62 ClientSecret: auth.RequiredString("client_secret"),
63 RedirectURL: oauthutil.TitleBarRedirectURL,
64 }, auth.RequiredString("refresh_token")))
65 parent := config.RequiredString("parent_id")
66 if err := config.Validate(); err != nil {
67 return nil, err
68 }
69 service, err := service.New(oAuthClient, parent)
70 sto := &driveStorage{
71 service: service,
72 }
73 return sto, err
74 }
75
76 func init() {
77 blobserver.RegisterStorageConstructor("googledrive", blobserver.StorageConstructor(newFromConfig))
78 }