mirror of
https://github.com/iyear/tdl
synced 2025-01-08 11:57:55 +08:00
feat(upload): support --delay flag
This commit is contained in:
parent
12c4f060de
commit
24ab0b50d9
@ -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():
|
||||
|
@ -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)
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
|
||||
type Iter interface {
|
||||
Next(ctx context.Context) bool
|
||||
HasNext() bool
|
||||
Value() Elem
|
||||
Err() error
|
||||
}
|
||||
|
@ -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
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user