1
0
mirror of https://github.com/dutchcoders/transfer.sh.git synced 2020-11-18 19:53:40 -08:00

save token in gdrive local config path (#170)

This commit is contained in:
Andrea Spacca 2018-10-26 23:28:47 +02:00 committed by GitHub
parent 60c1c1a116
commit 84ee48d8b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 5 deletions

View File

@ -141,7 +141,7 @@ If you want to use TLS using your own certificates, set tls-listener to :443, fo
Make sure your GOPATH is set correctly. Make sure your GOPATH is set correctly.
```bash ```bash
go run main.go -provider=local --listener :8080 --temp-path=/tmp/ --basedir=/tmp/ go run main.go --provider=local --listener :8080 --temp-path=/tmp/ --basedir=/tmp/
``` ```
## Build ## Build

View File

@ -334,7 +334,7 @@ func NewGDriveStorage(clientJsonFilepath string, localConfigPath string, basedir
return nil, err return nil, err
} }
srv, err := drive.New(getGDriveClient(config)) srv, err := drive.New(getGDriveClient(config, localConfigPath))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -349,6 +349,7 @@ func NewGDriveStorage(clientJsonFilepath string, localConfigPath string, basedir
} }
const GDriveRootConfigFile = "root_id.conf" const GDriveRootConfigFile = "root_id.conf"
const GDriveTokenJsonFile = "token.json"
const GDriveDirectoryMimeType = "application/vnd.google-apps.folder" const GDriveDirectoryMimeType = "application/vnd.google-apps.folder"
func (s *GDrive) setupRoot() error { func (s *GDrive) setupRoot() error {
@ -569,13 +570,14 @@ func (s *GDrive) Put(token string, filename string, reader io.Reader, contentTyp
} }
// Retrieve a token, saves the token, then returns the generated client. // Retrieve a token, saves the token, then returns the generated client.
func getGDriveClient(config *oauth2.Config) *http.Client { func getGDriveClient(config *oauth2.Config, localConfigPath string) *http.Client {
tokenFile := "token.json" tokenFile := filepath.Join(localConfigPath, GDriveTokenJsonFile)
tok, err := gDriveTokenFromFile(tokenFile) tok, err := gDriveTokenFromFile(tokenFile)
if err != nil { if err != nil {
tok = getGDriveTokenFromWeb(config) tok = getGDriveTokenFromWeb(config)
saveGDriveToken(tokenFile, tok) saveGDriveToken(tokenFile, tok)
} }
return config.Client(context.Background(), tok) return config.Client(context.Background(), tok)
} }
@ -590,7 +592,7 @@ func getGDriveTokenFromWeb(config *oauth2.Config) *oauth2.Token {
log.Fatalf("Unable to read authorization code %v", err) log.Fatalf("Unable to read authorization code %v", err)
} }
tok, err := config.Exchange(oauth2.NoContext, authCode) tok, err := config.Exchange(context.TODO(), authCode)
if err != nil { if err != nil {
log.Fatalf("Unable to retrieve token from web %v", err) log.Fatalf("Unable to retrieve token from web %v", err)
} }