Home Download Docs Code Community
     1	/*
     2	Copyright 2013 The Perkeep Authors
     3	
     4	Licensed under the Apache License, Version 2.0 (the "License");
     5	you may not use this file except in compliance with the License.
     6	You may obtain a copy of the License at
     7	
     8	     http://www.apache.org/licenses/LICENSE-2.0
     9	
    10	Unless required by applicable law or agreed to in writing, software
    11	distributed under the License is distributed on an "AS IS" BASIS,
    12	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13	See the License for the specific language governing permissions and
    14	limitations under the License.
    15	*/
    16	
    17	/*
    18	Package drive registers the "googledrive" blobserver storage
    19	type, storing blobs in a Google Drive folder.
    20	
    21	Example low-level config:
    22	
    23		"/storage-googledrive/": {
    24		  "handler": "storage-googledrive",
    25		  "handlerArgs": map[string]interface{}{
    26		    "parent_id": parentId,
    27		    "auth": map[string]interface{}{
    28		      "client_id":     clientId,
    29		      "client_secret": clientSecret,
    30		      "refresh_token": refreshToken,
    31		    },
    32		  },
    33		},
    34	*/
    35	package drive // import "perkeep.org/pkg/blobserver/google/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	// Scope is the OAuth2 scope used for Google Drive.
    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	}
Website layout inspired by memcached.
Content by the authors.