@@ -24,14 +24,12 @@ import (
2424type GDrive struct {
2525 service * drive.Service
2626 rootID string
27- basedir string
2827 localConfigPath string
2928 authType string
3029 chunkSize int
3130 logger * log.Logger
3231}
3332
34- const gDriveRootConfigFile = "root_id.conf"
3533const gDriveTokenJSONFile = "token.json"
3634const gDriveDirectoryMimeType = "application/vnd.google-apps.folder"
3735
@@ -50,7 +48,7 @@ func NewGDriveStorage(clientJSONFilepath string, localConfigPath string, basedir
5048 var AuthType string
5149
5250 if strings .Contains (string (b ), `"type": "service_account"` ) {
53- AuthType = "service_account "
51+ AuthType = "service "
5452
5553 logger .Println ("GDrive: using Service Account credentials" )
5654 config , err := google .JWTConfigFromJSON (b , drive .DriveScope , drive .DriveMetadataScope )
@@ -59,7 +57,11 @@ func NewGDriveStorage(clientJSONFilepath string, localConfigPath string, basedir
5957 }
6058 httpClient = config .Client (ctx )
6159 } else {
62- AuthType = "user_account"
60+ AuthType = "oauth"
61+
62+ if localConfigPath == "" {
63+ return nil , fmt .Errorf ("gdrive-local-config-path not set" )
64+ }
6365
6466 logger .Println ("GDrive: using OAuth2 credentials" )
6567 config , err := google .ConfigFromJSON (b , drive .DriveScope , drive .DriveMetadataScope )
@@ -74,45 +76,26 @@ func NewGDriveStorage(clientJSONFilepath string, localConfigPath string, basedir
7476 return nil , err
7577 }
7678
77- storage := & GDrive {service : srv , basedir : basedir , rootID : "" , localConfigPath : localConfigPath , authType : AuthType , chunkSize : chunkSize , logger : logger }
78- err = storage .setupRoot ()
79+ storage := & GDrive {service : srv , rootID : basedir , localConfigPath : localConfigPath , authType : AuthType , chunkSize : chunkSize , logger : logger }
80+ err = storage .checkRoot ()
7981 if err != nil {
8082 return nil , err
8183 }
8284
8385 return storage , nil
8486}
8587
86- func (s * GDrive ) setupRoot () error {
87- rootFileConfig := filepath .Join (s .localConfigPath , gDriveRootConfigFile )
88-
89- rootID , err := ioutil .ReadFile (rootFileConfig )
90- if err != nil && ! os .IsNotExist (err ) {
91- return err
92- }
93-
94- if string (rootID ) != "" {
95- s .rootID = string (rootID )
96- return nil
97- }
98-
99- dir := & drive.File {
100- Name : s .basedir ,
101- MimeType : gDriveDirectoryMimeType ,
102- }
103-
104- di , err := s .service .Files .Create (dir ).Fields ("id" ).SupportsAllDrives (true ).Do ()
105- if err != nil {
106- return err
107- }
108-
109- s .rootID = di .Id
110- err = ioutil .WriteFile (rootFileConfig , []byte (s .rootID ), os .FileMode (0600 ))
111- if err != nil {
112- return err
88+ func (s * GDrive ) checkRoot () error {
89+ if s .rootID == "root" {
90+ switch s .authType {
91+ case "service" :
92+ return fmt .Errorf ("GDrive: Folder \" root\" is not available when using Service Account credentials" )
93+ case "oauth" :
94+ s .logger .Println ("GDrive: Warning: Folder \" root\" is not recommended." )
95+ }
11396 }
114-
115- return nil
97+ _ , err := s . service . Files . Get ( s . rootID ). SupportsAllDrives ( true ). Do ()
98+ return err
11699}
117100
118101func (s * GDrive ) hasChecksum (f * drive.File ) bool {
0 commit comments