Skip to content

Commit

Permalink
use tus uploader from go-putio pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Sep 8, 2020
1 parent 245d388 commit 9bc035c
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 187 deletions.
1 change: 0 additions & 1 deletion cmd/putio-sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ var (
date = ""
)

// TODO Move Uploader inside go-putio pkg
// TODO Watch fs events for uploading files larger than 4M
// TODO Watch fs events in local root
// TODO Watch remote file events
Expand Down
7 changes: 3 additions & 4 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ const (

var ErrInvalidCredentials = errors.New("invalid credentials")

func Authenticate(ctx context.Context, httpClient *http.Client, timeout time.Duration, username, password string) (token string, client *putio.Client, err error) {
func Authenticate(ctx context.Context, httpClient *http.Client, timeout time.Duration, username, password string) (client *putio.Client, err error) {
if strings.HasPrefix(password, "token/") {
// User may use a token instead of password.
log.Infof("Validating authentication token")
token = password[6:]
token := password[6:]
client = newClient(ctx, httpClient, token)
authCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
Expand Down Expand Up @@ -89,8 +89,7 @@ func Authenticate(ctx context.Context, httpClient *http.Client, timeout time.Dur
return
}

token = tokenResponse.AccessToken
client = newClient(ctx, httpClient, token)
client = newClient(ctx, httpClient, tokenResponse.AccessToken)
return
}

Expand Down
172 changes: 0 additions & 172 deletions internal/tus/tus.go

This file was deleted.

2 changes: 1 addition & 1 deletion job_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (j *deleteStateJob) Run(ctx context.Context) error {
}
}
if j.state.UploadURL != "" {
err := uploader.TerminateUpload(ctx, j.state.UploadURL)
err := client.Upload.TerminateUpload(ctx, j.state.UploadURL)
if err != nil {
log.Errorln("cannot remove upload:", err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions job_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (d *uploadJob) tryResume(ctx context.Context) bool {
if d.state.LocalInode != in {
return false
}
offset, err := uploader.GetOffset(ctx, d.state.UploadURL)
offset, err := client.Upload.GetOffset(ctx, d.state.UploadURL)
if err != nil {
return false
}
Expand All @@ -57,7 +57,7 @@ func (d *uploadJob) Run(ctx context.Context) error {
if err != nil {
return err
}
location, err := uploader.CreateUpload(ctx, filename, parentID, d.localFile.Info().Size())
location, err := client.Upload.CreateUpload(ctx, filename, parentID, d.localFile.Info().Size())
if err != nil {
return err
}
Expand All @@ -84,7 +84,7 @@ func (d *uploadJob) Run(ctx context.Context) error {
}
pr := progress.New(f, d.state.Offset, d.state.Size, d.String())
pr.Start()
fileID, crc32, err := uploader.SendFile(ctx, pr, d.state.UploadURL, d.state.Offset)
fileID, crc32, err := client.Upload.SendFile(ctx, pr, d.state.UploadURL, d.state.Offset)
pr.Stop()
if err != nil {
return err
Expand Down
7 changes: 1 addition & 6 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/putdotio/putio-sync/v2/internal/auth"
"github.com/putdotio/putio-sync/v2/internal/dircache"
"github.com/putdotio/putio-sync/v2/internal/tmpdir"
"github.com/putdotio/putio-sync/v2/internal/tus"
"github.com/putdotio/putio-sync/v2/internal/walker"
"go.etcd.io/bbolt"
)
Expand All @@ -29,13 +28,11 @@ var ErrInvalidCredentials = errors.New("invalid credentials")
var (
cfg Config
db *bbolt.DB
token string
client *putio.Client
localPath string
remoteFolderID int64
dirCache *dircache.DirCache
tempDirPath string
uploader *tus.Uploader
syncing bool
syncStatus = "Starting sync..."
)
Expand Down Expand Up @@ -107,7 +104,7 @@ REPEAT_LOOP:

func syncOnce(ctx context.Context) error {
var err error
token, client, err = auth.Authenticate(ctx, httpClient, defaultTimeout, cfg.Username, cfg.Password)
client, err = auth.Authenticate(ctx, httpClient, defaultTimeout, cfg.Username, cfg.Password)
if err != nil {
return err
}
Expand All @@ -120,8 +117,6 @@ func syncOnce(ctx context.Context) error {
return err
}
dirCache = dircache.New(client, defaultTimeout, remoteFolderID)
uploader = tus.NewUploader(httpClient, defaultTimeout, token)

return syncRoots(ctx)
}

Expand Down

0 comments on commit 9bc035c

Please sign in to comment.