mirror of https://github.com/perkeep/perkeep.git
79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
/*
|
|
Copyright 2013 The Perkeep Authors
|
|
|
|
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 "perkeep.org/pkg/blobserver/google/drive"
|
|
|
|
import (
|
|
"go4.org/jsonconfig"
|
|
"perkeep.org/pkg/blobserver"
|
|
"perkeep.org/pkg/blobserver/google/drive/service"
|
|
|
|
"context"
|
|
|
|
"go4.org/oauthutil"
|
|
"golang.org/x/oauth2"
|
|
"golang.org/x/oauth2/google"
|
|
)
|
|
|
|
// Scope is the OAuth2 scope used for Google Drive.
|
|
const Scope = "https://www.googleapis.com/auth/drive"
|
|
|
|
type driveStorage struct {
|
|
service *service.DriveService
|
|
}
|
|
|
|
func newFromConfig(_ blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, error) {
|
|
auth := config.RequiredObject("auth")
|
|
oAuthClient := oauth2.NewClient(context.Background(), oauthutil.NewRefreshTokenSource(&oauth2.Config{
|
|
Scopes: []string{Scope},
|
|
Endpoint: google.Endpoint,
|
|
ClientID: auth.RequiredString("client_id"),
|
|
ClientSecret: auth.RequiredString("client_secret"),
|
|
RedirectURL: oauthutil.TitleBarRedirectURL,
|
|
}, auth.RequiredString("refresh_token")))
|
|
parent := config.RequiredString("parent_id")
|
|
if err := config.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
service, err := service.New(oAuthClient, parent)
|
|
sto := &driveStorage{
|
|
service: service,
|
|
}
|
|
return sto, err
|
|
}
|
|
|
|
func init() {
|
|
blobserver.RegisterStorageConstructor("googledrive", blobserver.StorageConstructor(newFromConfig))
|
|
}
|