feat(upload): support --delay flag

This commit is contained in:
XMLHexagram 2024-03-24 23:16:54 +08:00
parent 12c4f060de
commit 24ab0b50d9
4 changed files with 16 additions and 0 deletions

View File

@ -41,6 +41,10 @@ func newIter(files []*file, to peers.Peer, photo, remove bool) *iter {
}
}
func (i *iter) HasNext() bool {
return i.err == nil && i.cur < len(i.files)
}
func (i *iter) Next(ctx context.Context) bool {
select {
case <-ctx.Done():

View File

@ -59,6 +59,7 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr
Threads: viper.GetInt(consts.FlagThreads),
Iter: newIter(files, to, opts.Photo, opts.Remove),
Progress: newProgress(upProgress),
Delay: viper.GetDuration(consts.FlagDelay),
}
up := uploader.New(options)

View File

@ -9,6 +9,7 @@ import (
type Iter interface {
Next(ctx context.Context) bool
HasNext() bool
Value() Elem
Err() error
}

View File

@ -2,6 +2,9 @@ package uploader
import (
"context"
"github.com/fatih/color"
"github.com/iyear/tdl/pkg/logger"
"go.uber.org/zap"
"io"
"time"
@ -26,6 +29,7 @@ type Options struct {
Threads int
Iter Iter
Progress Progress
Delay time.Duration
}
func New(o Options) *Uploader {
@ -52,6 +56,12 @@ func (u *Uploader) Upload(ctx context.Context, limit int) error {
// don't return error, just log it
}
if u.opts.Delay != 0 && u.opts.Iter.HasNext() {
logger.From(ctx).Debug("Delay", zap.Duration("delay", u.opts.Delay))
color.Yellow("Delay %s", u.opts.Delay.String())
<-time.After(u.opts.Delay)
}
return nil
})
}