git-lfs/gitmedia/util.go

36 lines
630 B
Go
Raw Normal View History

2014-08-06 22:04:34 +00:00
package gitmedia
import (
"io"
)
type CallbackWriter struct {
C func(int64, int64) error
TotalSize int64
WrittenSize int64
io.Writer
}
func (w *CallbackWriter) Write(p []byte) (int, error) {
n, err := w.Writer.Write(p)
if n > 0 {
w.WrittenSize += int64(n)
}
if err == nil && w.C != nil {
err = w.C(w.TotalSize, w.WrittenSize)
}
return n, err
}
func CopyWithCallback(writer io.Writer, reader io.Reader, totalSize int64, cb func(int64, int64) error) (int64, error) {
cbWriter := &CallbackWriter{
C: cb,
TotalSize: totalSize,
Writer: writer,
}
return io.Copy(cbWriter, reader)
}