2017-03-04 05:06:32 -05:00
|
|
|
package oss
|
|
|
|
|
|
|
|
import "io"
|
|
|
|
|
2019-10-28 17:31:07 -04:00
|
|
|
// ProgressEventType transfer progress event type
|
2017-03-04 05:06:32 -05:00
|
|
|
type ProgressEventType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TransferStartedEvent transfer started, set TotalBytes
|
|
|
|
TransferStartedEvent ProgressEventType = 1 + iota
|
|
|
|
// TransferDataEvent transfer data, set ConsumedBytes anmd TotalBytes
|
|
|
|
TransferDataEvent
|
|
|
|
// TransferCompletedEvent transfer completed
|
|
|
|
TransferCompletedEvent
|
|
|
|
// TransferFailedEvent transfer encounters an error
|
|
|
|
TransferFailedEvent
|
|
|
|
)
|
|
|
|
|
2019-10-28 17:31:07 -04:00
|
|
|
// ProgressEvent progress event
|
2017-03-04 05:06:32 -05:00
|
|
|
type ProgressEvent struct {
|
|
|
|
ConsumedBytes int64
|
|
|
|
TotalBytes int64
|
|
|
|
EventType ProgressEventType
|
|
|
|
}
|
|
|
|
|
2019-10-28 17:31:07 -04:00
|
|
|
// ProgressListener listen progress change
|
2017-03-04 05:06:32 -05:00
|
|
|
type ProgressListener interface {
|
|
|
|
ProgressChanged(event *ProgressEvent)
|
|
|
|
}
|
|
|
|
|
2019-10-28 17:31:07 -04:00
|
|
|
// -------------------- private --------------------
|
2017-03-04 05:06:32 -05:00
|
|
|
|
|
|
|
func newProgressEvent(eventType ProgressEventType, consumed, total int64) *ProgressEvent {
|
|
|
|
return &ProgressEvent{
|
|
|
|
ConsumedBytes: consumed,
|
|
|
|
TotalBytes: total,
|
|
|
|
EventType: eventType}
|
|
|
|
}
|
|
|
|
|
|
|
|
// publishProgress
|
|
|
|
func publishProgress(listener ProgressListener, event *ProgressEvent) {
|
|
|
|
if listener != nil && event != nil {
|
|
|
|
listener.ProgressChanged(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type readerTracker struct {
|
|
|
|
completedBytes int64
|
|
|
|
}
|
|
|
|
|
|
|
|
type teeReader struct {
|
|
|
|
reader io.Reader
|
|
|
|
writer io.Writer
|
|
|
|
listener ProgressListener
|
|
|
|
consumedBytes int64
|
|
|
|
totalBytes int64
|
|
|
|
tracker *readerTracker
|
|
|
|
}
|
|
|
|
|
|
|
|
// TeeReader returns a Reader that writes to w what it reads from r.
|
|
|
|
// All reads from r performed through it are matched with
|
|
|
|
// corresponding writes to w. There is no internal buffering -
|
|
|
|
// the write must complete before the read completes.
|
|
|
|
// Any error encountered while writing is reported as a read error.
|
2019-10-28 17:31:07 -04:00
|
|
|
func TeeReader(reader io.Reader, writer io.Writer, totalBytes int64, listener ProgressListener, tracker *readerTracker) io.Reader {
|
2017-03-04 05:06:32 -05:00
|
|
|
return &teeReader{
|
|
|
|
reader: reader,
|
|
|
|
writer: writer,
|
|
|
|
listener: listener,
|
|
|
|
consumedBytes: 0,
|
|
|
|
totalBytes: totalBytes,
|
|
|
|
tracker: tracker,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *teeReader) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = t.reader.Read(p)
|
|
|
|
|
2019-10-28 17:31:07 -04:00
|
|
|
// read encountered error
|
2017-03-04 05:06:32 -05:00
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
event := newProgressEvent(TransferFailedEvent, t.consumedBytes, t.totalBytes)
|
|
|
|
publishProgress(t.listener, event)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n > 0 {
|
|
|
|
t.consumedBytes += int64(n)
|
2019-10-28 17:31:07 -04:00
|
|
|
// crc
|
2017-03-04 05:06:32 -05:00
|
|
|
if t.writer != nil {
|
|
|
|
if n, err := t.writer.Write(p[:n]); err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
}
|
2019-10-28 17:31:07 -04:00
|
|
|
// progress
|
2017-03-04 05:06:32 -05:00
|
|
|
if t.listener != nil {
|
|
|
|
event := newProgressEvent(TransferDataEvent, t.consumedBytes, t.totalBytes)
|
|
|
|
publishProgress(t.listener, event)
|
|
|
|
}
|
2019-10-28 17:31:07 -04:00
|
|
|
// track
|
2017-03-04 05:06:32 -05:00
|
|
|
if t.tracker != nil {
|
|
|
|
t.tracker.completedBytes = t.consumedBytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|