refactor(up): use tRun to start session

This commit is contained in:
iyear 2023-12-11 00:42:32 +08:00
parent dfbb368ed9
commit ef751457d8
2 changed files with 33 additions and 32 deletions

View File

@ -5,6 +5,7 @@ import (
"github.com/fatih/color"
"github.com/go-faster/errors"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/peers"
"github.com/gotd/td/tg"
"github.com/spf13/viper"
@ -13,6 +14,7 @@ import (
"github.com/iyear/tdl/app/internal/tgc"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/dcpool"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/uploader"
@ -27,7 +29,7 @@ type Options struct {
Photo bool
}
func Run(ctx context.Context, opts *Options) error {
func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr error) {
files, err := walk(opts.Paths, opts.Excludes)
if err != nil {
return err
@ -35,46 +37,39 @@ func Run(ctx context.Context, opts *Options) error {
color.Blue("Files count: %d", len(files))
c, kvd, err := tgc.NoLogin(ctx)
middlewares, err := tgc.NewDefaultMiddlewares(ctx)
if err != nil {
return err
return errors.Wrap(err, "create middlewares")
}
return tgc.RunWithAuth(ctx, c, func(ctx context.Context) (rerr error) {
middlewares, err := tgc.NewDefaultMiddlewares(ctx)
if err != nil {
return errors.Wrap(err, "create middlewares")
}
pool := dcpool.NewPool(c, int64(viper.GetInt(consts.FlagPoolSize)), middlewares...)
defer multierr.AppendInvoke(&rerr, multierr.Close(pool))
pool := dcpool.NewPool(c, int64(viper.GetInt(consts.FlagPoolSize)), middlewares...)
defer multierr.AppendInvoke(&rerr, multierr.Close(pool))
manager := peers.Options{Storage: storage.NewPeers(kvd)}.Build(pool.Default(ctx))
manager := peers.Options{Storage: storage.NewPeers(kvd)}.Build(pool.Default(ctx))
to, err := resolveDestPeer(ctx, manager, opts.Chat)
if err != nil {
return errors.Wrap(err, "get target peer")
}
to, err := resolveDestPeer(ctx, manager, opts.Chat)
if err != nil {
return errors.Wrap(err, "get target peer")
}
upProgress := prog.New(utils.Byte.FormatBinaryBytes)
upProgress.SetNumTrackersExpected(len(files))
prog.EnablePS(ctx, upProgress)
upProgress := prog.New(utils.Byte.FormatBinaryBytes)
upProgress.SetNumTrackersExpected(len(files))
prog.EnablePS(ctx, upProgress)
options := uploader.Options{
Client: pool.Default(ctx),
PartSize: viper.GetInt(consts.FlagPartSize),
Threads: viper.GetInt(consts.FlagThreads),
Iter: newIter(files, to, opts.Photo, opts.Remove),
Progress: newProgress(upProgress),
}
options := uploader.Options{
Client: pool.Default(ctx),
PartSize: viper.GetInt(consts.FlagPartSize),
Threads: viper.GetInt(consts.FlagThreads),
Iter: newIter(files, to, opts.Photo, opts.Remove),
Progress: newProgress(upProgress),
}
up := uploader.New(options)
up := uploader.New(options)
go upProgress.Render()
defer prog.Wait(ctx, upProgress)
go upProgress.Render()
defer prog.Wait(ctx, upProgress)
return up.Upload(ctx, viper.GetInt(consts.FlagLimit))
})
return up.Upload(ctx, viper.GetInt(consts.FlagLimit))
}
func resolveDestPeer(ctx context.Context, manager *peers.Manager, chat string) (peers.Peer, error) {

View File

@ -1,9 +1,13 @@
package cmd
import (
"context"
"github.com/gotd/td/telegram"
"github.com/spf13/cobra"
"github.com/iyear/tdl/app/up"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/logger"
)
@ -15,7 +19,9 @@ func NewUpload() *cobra.Command {
Aliases: []string{"up"},
Short: "Upload anything to Telegram",
RunE: func(cmd *cobra.Command, args []string) error {
return up.Run(logger.Named(cmd.Context(), "up"), &opts)
return tRun(cmd.Context(), false, func(ctx context.Context, c *telegram.Client, kvd kv.KV) error {
return up.Run(logger.Named(ctx, "up"), c, kvd, opts)
})
},
}