Skip to content

Commit be00bb8

Browse files
committed
GDrive: basedir as rootID
1 parent 0327122 commit be00bb8

File tree

2 files changed

+21
-39
lines changed

2 files changed

+21
-39
lines changed

cmd/cmd.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/dutchcoders/transfer.sh/server/storage"
65
"log"
76
"os"
87
"strings"
98

109
"github.com/dutchcoders/transfer.sh/server"
10+
"github.com/dutchcoders/transfer.sh/server/storage"
1111
"github.com/fatih/color"
1212
"github.com/urfave/cli"
1313
"google.golang.org/api/googleapi"
@@ -471,11 +471,10 @@ func New() *Cmd {
471471
}
472472
case "gdrive":
473473
chunkSize := c.Int("gdrive-chunk-size") * 1024 * 1024
474+
localConfigPath := c.String("gdrive-local-config-path")
474475

475476
if clientJSONFilepath := c.String("gdrive-client-json-filepath"); clientJSONFilepath == "" {
476-
panic("client-json-filepath not set.")
477-
} else if localConfigPath := c.String("gdrive-local-config-path"); localConfigPath == "" {
478-
panic("local-config-path not set.")
477+
panic("gdrive-client-json-filepath not set.")
479478
} else if basedir := c.String("basedir"); basedir == "" {
480479
panic("basedir not set.")
481480
} else if store, err := storage.NewGDriveStorage(clientJSONFilepath, localConfigPath, basedir, chunkSize, logger); err != nil {

server/storage/gdrive.go

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ import (
2424
type 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"
3533
const gDriveTokenJSONFile = "token.json"
3634
const 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

118101
func (s *GDrive) hasChecksum(f *drive.File) bool {

0 commit comments

Comments
 (0)