mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-11 13:18:53 +00:00
ipn/localapi: add support for multipart POST to file-put
This allows sending multiple files via Taildrop in one request. Progress is tracked via ipn.Notify. Updates tailscale/corp#18202 Signed-off-by: Percy Wegmann <percy@tailscale.com>
This commit is contained in:

committed by
Percy Wegmann

parent
0d8cd1645a
commit
bed818a978
39
util/progresstracking/progresstracking.go
Normal file
39
util/progresstracking/progresstracking.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// Copyright (c) Tailscale Inc & AUTHORS
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
// Package progresstracking provides wrappers around io.Reader and io.Writer
|
||||
// that track progress.
|
||||
package progresstracking
|
||||
|
||||
import (
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
// NewReader wraps the given Reader with a progress tracking Reader that
|
||||
// reports progress at the following points:
|
||||
//
|
||||
// - First read
|
||||
// - Every read spaced at least interval since the prior read
|
||||
// - Last read
|
||||
func NewReader(r io.Reader, interval time.Duration, onProgress func(totalRead int, err error)) io.Reader {
|
||||
return &reader{Reader: r, interval: interval, onProgress: onProgress}
|
||||
}
|
||||
|
||||
type reader struct {
|
||||
io.Reader
|
||||
interval time.Duration
|
||||
onProgress func(int, error)
|
||||
lastTracked time.Time
|
||||
totalRead int
|
||||
}
|
||||
|
||||
func (r *reader) Read(p []byte) (int, error) {
|
||||
n, err := r.Reader.Read(p)
|
||||
r.totalRead += n
|
||||
if time.Since(r.lastTracked) > r.interval || err != nil {
|
||||
r.onProgress(r.totalRead, err)
|
||||
r.lastTracked = time.Now()
|
||||
}
|
||||
return n, err
|
||||
}
|
Reference in New Issue
Block a user