Skip to content

Commit 6942259

Browse files
committed
refactor uploader
1 parent 7562e91 commit 6942259

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

internal/tus/tus.go

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,23 @@ const (
1717
uploadURL = "https://upload.put.io/files/"
1818
)
1919

20-
func CreateUpload(baseCtx context.Context, httpClient *http.Client, timeout time.Duration, token string, filename string, parentID, length int64) (location string, err error) {
20+
type Uploader struct {
21+
client *http.Client
22+
timeout time.Duration
23+
token string
24+
}
25+
26+
func NewUploader(client *http.Client, timeout time.Duration, token string) *Uploader {
27+
return &Uploader{
28+
client: client,
29+
timeout: timeout,
30+
token: token,
31+
}
32+
}
33+
34+
func (u *Uploader) CreateUpload(baseCtx context.Context, filename string, parentID, length int64) (location string, err error) {
2135
log.Debugf("Creating upload %q at parent=%d", filename, parentID)
22-
ctx, cancel := context.WithTimeout(baseCtx, timeout)
36+
ctx, cancel := context.WithTimeout(baseCtx, u.timeout)
2337
defer cancel()
2438
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uploadURL, nil)
2539
if err != nil {
@@ -33,9 +47,9 @@ func CreateUpload(baseCtx context.Context, httpClient *http.Client, timeout time
3347
req.Header.Set("Content-Length", "0")
3448
req.Header.Set("Upload-Length", strconv.FormatInt(length, 10))
3549
req.Header.Set("Upload-Metadata", encodeMetadata(metadata))
36-
req.Header.Set("Authorization", "token "+token)
50+
req.Header.Set("Authorization", "token "+u.token)
3751

38-
resp, err := httpClient.Do(req)
52+
resp, err := u.client.Do(req)
3953
if err != nil {
4054
return
4155
}
@@ -50,15 +64,15 @@ func CreateUpload(baseCtx context.Context, httpClient *http.Client, timeout time
5064
return
5165
}
5266

53-
func SendFile(ctx context.Context, httpClient *http.Client, timeout time.Duration, token string, r io.Reader, location string, offset int64) (fileID int64, crc32 string, err error) {
67+
func (u *Uploader) SendFile(ctx context.Context, r io.Reader, location string, offset int64) (fileID int64, crc32 string, err error) {
5468
log.Debugf("Sending file %q offset=%d", location, offset)
5569

5670
ctx, cancel := context.WithCancel(ctx)
5771
defer cancel()
5872

5973
// Stop upload if speed is too slow.
6074
// Wrap reader so each read call resets the timer that cancels the request on certain duration.
61-
r = &timerResetReader{r: r, timer: time.AfterFunc(timeout, cancel), timeout: timeout}
75+
r = &timerResetReader{r: r, timer: time.AfterFunc(u.timeout, cancel), timeout: u.timeout}
6276

6377
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, location, r)
6478
if err != nil {
@@ -67,8 +81,8 @@ func SendFile(ctx context.Context, httpClient *http.Client, timeout time.Duratio
6781

6882
req.Header.Set("content-type", "application/offset+octet-stream")
6983
req.Header.Set("upload-offset", strconv.FormatInt(offset, 10))
70-
req.Header.Set("Authorization", "token "+token)
71-
resp, err := httpClient.Do(req)
84+
req.Header.Set("Authorization", "token "+u.token)
85+
resp, err := u.client.Do(req)
7286
if err != nil {
7387
return
7488
}
@@ -88,17 +102,17 @@ func SendFile(ctx context.Context, httpClient *http.Client, timeout time.Duratio
88102
return
89103
}
90104

91-
func GetOffset(ctx context.Context, httpClient *http.Client, timeout time.Duration, token string, location string) (n int64, err error) {
105+
func (u *Uploader) GetOffset(ctx context.Context, location string) (n int64, err error) {
92106
log.Debugf("Getting upload offset %q", location)
93-
ctx, cancel := context.WithTimeout(ctx, timeout)
107+
ctx, cancel := context.WithTimeout(ctx, u.timeout)
94108
defer cancel()
95109
req, err := http.NewRequestWithContext(ctx, http.MethodHead, location, nil)
96110
if err != nil {
97111
return
98112
}
99113

100-
req.Header.Set("Authorization", "token "+token)
101-
resp, err := httpClient.Do(req)
114+
req.Header.Set("Authorization", "token "+u.token)
115+
resp, err := u.client.Do(req)
102116
if err != nil {
103117
return
104118
}
@@ -114,17 +128,17 @@ func GetOffset(ctx context.Context, httpClient *http.Client, timeout time.Durati
114128
return n, err
115129
}
116130

117-
func TerminateuploadJob(ctx context.Context, httpClient *http.Client, timeout time.Duration, token string, location string) (err error) {
131+
func (u *Uploader) TerminateUpload(ctx context.Context, location string) (err error) {
118132
log.Debugf("Terminating upload %q", location)
119-
ctx, cancel := context.WithTimeout(ctx, timeout)
133+
ctx, cancel := context.WithTimeout(ctx, u.timeout)
120134
defer cancel()
121135
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, location, nil)
122136
if err != nil {
123137
return
124138
}
125139

126-
req.Header.Set("Authorization", "token "+token)
127-
resp, err := httpClient.Do(req)
140+
req.Header.Set("Authorization", "token "+u.token)
141+
resp, err := u.client.Do(req)
128142
if err != nil {
129143
return
130144
}

job_state.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/cenkalti/log"
1010
"github.com/putdotio/putio-sync/v2/internal/inode"
11-
"github.com/putdotio/putio-sync/v2/internal/tus"
1211
)
1312

1413
type deleteStateJob struct {
@@ -27,7 +26,7 @@ func (j *deleteStateJob) Run(ctx context.Context) error {
2726
}
2827
}
2928
if j.state.UploadURL != "" {
30-
err := tus.TerminateuploadJob(ctx, httpClient, defaultTimeout, token, j.state.UploadURL)
29+
err := uploader.TerminateUpload(ctx, j.state.UploadURL)
3130
if err != nil {
3231
log.Errorln("cannot remove upload:", err.Error())
3332
}

sync.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/putdotio/putio-sync/v2/internal/auth"
1313
"github.com/putdotio/putio-sync/v2/internal/dircache"
1414
"github.com/putdotio/putio-sync/v2/internal/tmpdir"
15+
"github.com/putdotio/putio-sync/v2/internal/tus"
1516
"github.com/putdotio/putio-sync/v2/internal/walker"
1617
"go.etcd.io/bbolt"
1718
)
@@ -31,6 +32,7 @@ var (
3132
remoteFolderID int64
3233
dirCache *dircache.DirCache
3334
tempDirPath string
35+
uploader *tus.Uploader
3436
)
3537

3638
func Sync(ctx context.Context, config Config) error {
@@ -106,6 +108,7 @@ func syncOnce(ctx context.Context) error {
106108
return err
107109
}
108110
dirCache = dircache.New(client, defaultTimeout, remoteFolderID)
111+
uploader = tus.NewUploader(httpClient, defaultTimeout, token)
109112

110113
return syncRoots(ctx)
111114
}

upload.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/putdotio/putio-sync/v2/internal/inode"
1111
"github.com/putdotio/putio-sync/v2/internal/progress"
12-
"github.com/putdotio/putio-sync/v2/internal/tus"
1312
)
1413

1514
type uploadJob struct {
@@ -38,7 +37,7 @@ func (d *uploadJob) tryResume(ctx context.Context) bool {
3837
if d.state.LocalInode != in {
3938
return false
4039
}
41-
offset, err := tus.GetOffset(ctx, httpClient, defaultTimeout, token, d.state.UploadURL)
40+
offset, err := uploader.GetOffset(ctx, d.state.UploadURL)
4241
if err != nil {
4342
return false
4443
}
@@ -58,7 +57,7 @@ func (d *uploadJob) Run(ctx context.Context) error {
5857
if err != nil {
5958
return err
6059
}
61-
location, err := tus.CreateUpload(ctx, httpClient, defaultTimeout, token, filename, parentID, d.localFile.Info().Size())
60+
location, err := uploader.CreateUpload(ctx, filename, parentID, d.localFile.Info().Size())
6261
if err != nil {
6362
return err
6463
}
@@ -85,7 +84,7 @@ func (d *uploadJob) Run(ctx context.Context) error {
8584
}
8685
pr := progress.New(f, d.state.Offset, d.state.Size, d.String())
8786
pr.Start()
88-
fileID, crc32, err := tus.SendFile(ctx, httpClient, defaultTimeout, token, pr, d.state.UploadURL, d.state.Offset)
87+
fileID, crc32, err := uploader.SendFile(ctx, pr, d.state.UploadURL, d.state.Offset)
8988
pr.Stop()
9089
if err != nil {
9190
return err

0 commit comments

Comments
 (0)