-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypes.go
More file actions
63 lines (55 loc) · 2.01 KB
/
types.go
File metadata and controls
63 lines (55 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"io"
"sync/atomic"
"time"
)
type Action struct {
Type string //either ToDevice or FromDevice. If type is "" the action is ignored
FileName string //always set if type is ToDevice, set when the download has begun in case of FromDevice
UUID string
UploadPreWriterStatus string //status of a FromDevice action before a ProgressedWriter has been created, either RequestReceived or Canceled
Begun bool //Whether the device already has been informed of the download
BegunTime time.Time //Timestamp when the download begun
UploadReader io.ReadSeekCloser //only populated if type is ToDevice
DownloadBegins *func(name string) io.WriteCloser //only populated if type is FromDevice, called when the download has begun.
ProgressedWriter *ProgressedWriter //set when the download has begun. Should NOT be written to.
}
type Device struct {
Ip string //main identifier
Name string //extracted from user agent
LastPing int64 //if we've gone more than 3 seconds without a ping, we dont include it
PendingAction Action
}
type PingResponse struct {
Status string `json:"status"`
FileName string `json:"filename"`
Url string `json:"url"`
}
type ProgressedWriter struct {
writer io.Writer
closer io.Closer
progress atomic.Int64
Size int64 //Size can be set during initalization and may be used to track the expected size of the data to be written to the writer.
}
func GetProgressedWriter(writer io.Writer, size int64) *ProgressedWriter {
return &ProgressedWriter{
writer: writer,
progress: atomic.Int64{},
Size: size,
}
}
//BytesWritten returns the amount of bytes written to the ProgressedWriter
func (w *ProgressedWriter) BytesWritten() int64 {
return w.progress.Load()
}
//Write calls write on the underlying io.Writer
func (w *ProgressedWriter) Write(p []byte) (n int, err error) {
n, err = w.writer.Write(p)
w.progress.Add(int64(n))
return
}
//Close calls close on the underlying io.Closer
func (w *ProgressedWriter) Close() error {
return w.closer.Close()
}