Skip to content
This repository was archived by the owner on Jul 10, 2022. It is now read-only.

Commit d50eff5

Browse files
Implement upload updates via channel.
1 parent 2fab99a commit d50eff5

File tree

5 files changed

+62
-10
lines changed

5 files changed

+62
-10
lines changed

Diff for: .travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ language: go
22
go:
33
- 1.6
44
- 1.7
5+
- 1.8
6+
- 1.9
57
- tip
68
install:
79
- go get github.com/stretchr/testify

Diff for: Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
FROM golang:1.7
1+
FROM golang:1.9
22

33
RUN mkdir -p /go/src/github.com/eventials/go-tus
44

55
WORKDIR /go/src/github.com/eventials/go-tus
66

77
RUN go get github.com/stretchr/testify
88
RUN go get github.com/tus/tusd
9+
RUN go get github.com/syndtr/goleveldb/leveldb

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ Store is used to map an upload's fingerprint with the corresponding upload URL.
4949

5050
| Name | Backend | Dependencies |
5151
|:----:|:-------:|:------------:|
52-
| MemoryStore | In-Memory | None |
53-
| LeveldbStore | LevelDB | [goleveldb](https://github.com/syndtr/goleveldb) |
52+
| MemoryStore | In-Memory | None |
53+
| LeveldbStore | LevelDB | [goleveldb](https://github.com/syndtr/goleveldb) |
5454

5555
## Future Work
5656

Diff for: upload.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,33 @@ type Metadata map[string]string
1313
type Upload struct {
1414
stream io.ReadSeeker
1515
size int64
16+
offset int64
1617

1718
Fingerprint string
1819
Metadata Metadata
1920
}
2021

21-
// Size returns the size of the upload body.
22+
// Updates the Upload information based on offset.
23+
func (u *Upload) updateProgress(offset int64) {
24+
u.offset = offset
25+
}
26+
27+
// Returns whether this upload is finished or not.
28+
func (u *Upload) Finished() bool {
29+
return u.offset >= u.size
30+
}
31+
32+
// Returns the progress in a percentage.
33+
func (u *Upload) Progress() int64 {
34+
return (u.offset * 100) / u.size
35+
}
36+
37+
// Returns the current upload offset.
38+
func (u *Upload) Offset() int64 {
39+
return u.offset
40+
}
41+
42+
// Returns the size of the upload body.
2243
func (u *Upload) Size() int64 {
2344
return u.size
2445
}

Diff for: uploader.go

+34-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ import (
55
)
66

77
type Uploader struct {
8-
client *Client
9-
url string
10-
upload *Upload
11-
offset int64
12-
aborted bool
8+
client *Client
9+
url string
10+
upload *Upload
11+
offset int64
12+
aborted bool
13+
uploadSubs []chan Upload
14+
notifyChan chan bool
15+
}
16+
17+
// Subscribes to progress updates.
18+
func (u *Uploader) NotifyUploadProgress(c chan Upload) {
19+
u.uploadSubs = append(u.uploadSubs, c)
1320
}
1421

1522
// Abort aborts the upload process.
@@ -72,16 +79,37 @@ func (u *Uploader) UploadChunck() error {
7279

7380
u.offset = newOffset
7481

82+
u.upload.updateProgress(u.offset)
83+
84+
u.notifyChan <- true
85+
7586
return nil
7687
}
7788

89+
// Waits for a signal to broadcast to all subscribers
90+
func (u *Uploader) broadcastProgress() {
91+
for _ = range u.notifyChan {
92+
for _, c := range u.uploadSubs {
93+
c <- *u.upload
94+
}
95+
}
96+
}
97+
7898
// NewUploader creates a new Uploader.
7999
func NewUploader(client *Client, url string, upload *Upload, offset int64) *Uploader {
80-
return &Uploader{
100+
notifyChan := make(chan bool)
101+
102+
uploader := &Uploader{
81103
client,
82104
url,
83105
upload,
84106
offset,
85107
false,
108+
nil,
109+
notifyChan,
86110
}
111+
112+
go uploader.broadcastProgress()
113+
114+
return uploader
87115
}

0 commit comments

Comments
 (0)