diff --git a/app/chat/export.go b/app/chat/export.go index 88870d3..ed8222e 100644 --- a/app/chat/export.go +++ b/app/chat/export.go @@ -18,12 +18,12 @@ import ( "github.com/jedib0t/go-pretty/v6/progress" "go.uber.org/multierr" - "github.com/iyear/tdl/core/tutil" + "github.com/iyear/tdl/core/tmedia" + "github.com/iyear/tdl/core/util/tutil" "github.com/iyear/tdl/pkg/kv" "github.com/iyear/tdl/pkg/prog" "github.com/iyear/tdl/pkg/storage" "github.com/iyear/tdl/pkg/texpr" - "github.com/iyear/tdl/pkg/tmedia" ) //go:generate go-enum --names --values --flag --nocase diff --git a/app/chat/ls.go b/app/chat/ls.go index 3c8b938..3bbb974 100644 --- a/app/chat/ls.go +++ b/app/chat/ls.go @@ -16,9 +16,9 @@ import ( "github.com/mattn/go-runewidth" "go.uber.org/zap" - "github.com/iyear/tdl/core/tutil" + "github.com/iyear/tdl/core/logctx" + "github.com/iyear/tdl/core/util/tutil" "github.com/iyear/tdl/pkg/kv" - "github.com/iyear/tdl/pkg/logger" "github.com/iyear/tdl/pkg/storage" "github.com/iyear/tdl/pkg/texpr" ) @@ -56,7 +56,7 @@ type ListOptions struct { } func List(ctx context.Context, c *telegram.Client, kvd kv.KV, opts ListOptions) error { - log := logger.From(ctx) + log := logctx.From(ctx) // align output runewidth.EastAsianWidth = false diff --git a/app/chat/users.go b/app/chat/users.go index 204ba08..9146cf3 100644 --- a/app/chat/users.go +++ b/app/chat/users.go @@ -18,7 +18,7 @@ import ( "github.com/jedib0t/go-pretty/v6/progress" "go.uber.org/multierr" - "github.com/iyear/tdl/core/tutil" + "github.com/iyear/tdl/core/util/tutil" "github.com/iyear/tdl/pkg/kv" "github.com/iyear/tdl/pkg/prog" "github.com/iyear/tdl/pkg/storage" diff --git a/app/dl/dl.go b/app/dl/dl.go index 985f409..b14dfe5 100644 --- a/app/dl/dl.go +++ b/app/dl/dl.go @@ -14,12 +14,12 @@ import ( "go.uber.org/multierr" "go.uber.org/zap" + "github.com/iyear/tdl/core/dcpool" + "github.com/iyear/tdl/core/downloader" + "github.com/iyear/tdl/core/logctx" "github.com/iyear/tdl/pkg/consts" - "github.com/iyear/tdl/pkg/dcpool" - "github.com/iyear/tdl/pkg/downloader" "github.com/iyear/tdl/pkg/key" "github.com/iyear/tdl/pkg/kv" - "github.com/iyear/tdl/pkg/logger" "github.com/iyear/tdl/pkg/prog" "github.com/iyear/tdl/pkg/storage" "github.com/iyear/tdl/pkg/tclient" @@ -66,7 +66,7 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr if err != nil { return err } - logger.From(ctx).Debug("Collect dialogs", + logctx.From(ctx).Debug("Collect dialogs", zap.Any("dialogs", dialogs)) if opts.Serve { @@ -110,7 +110,7 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr } limit := viper.GetInt(consts.FlagLimit) - logger.From(ctx).Info("Start download", + logctx.From(ctx).Info("Start download", zap.String("dir", opts.Dir), zap.Bool("rewrite_ext", opts.RewriteExt), zap.Bool("skip_same", opts.SkipSame), @@ -139,7 +139,7 @@ func collectDialogs(parsers []parser) ([][]*tmessage.Dialog, error) { } func resume(ctx context.Context, kvd kv.KV, iter *iter, ask bool) error { - logger.From(ctx).Debug("Check resume key", + logctx.From(ctx).Debug("Check resume key", zap.String("fingerprint", iter.Fingerprint())) b, err := kvd.Get(key.Resume(iter.Fingerprint())) @@ -173,7 +173,7 @@ func resume(ctx context.Context, kvd kv.KV, iter *iter, ask bool) error { confirm = true } - logger.From(ctx).Debug("Resume download", + logctx.From(ctx).Debug("Resume download", zap.Int("finished", len(finished))) if !confirm { @@ -187,7 +187,7 @@ func resume(ctx context.Context, kvd kv.KV, iter *iter, ask bool) error { func saveProgress(ctx context.Context, kvd kv.KV, it *iter) error { finished := it.Finished() - logger.From(ctx).Debug("Save progress", + logctx.From(ctx).Debug("Save progress", zap.Int("finished", len(finished))) b, err := json.Marshal(finished) diff --git a/app/dl/elem.go b/app/dl/elem.go index a56cb2a..f2b37fe 100644 --- a/app/dl/elem.go +++ b/app/dl/elem.go @@ -7,8 +7,8 @@ import ( "github.com/gotd/td/telegram/peers" "github.com/gotd/td/tg" - "github.com/iyear/tdl/pkg/downloader" - "github.com/iyear/tdl/pkg/tmedia" + "github.com/iyear/tdl/core/downloader" + "github.com/iyear/tdl/core/tmedia" ) type iterElem struct { diff --git a/app/dl/iter.go b/app/dl/iter.go index a4dedac..df524d2 100644 --- a/app/dl/iter.go +++ b/app/dl/iter.go @@ -16,10 +16,11 @@ import ( "github.com/go-faster/errors" "github.com/gotd/td/telegram/peers" - "github.com/iyear/tdl/core/tutil" - "github.com/iyear/tdl/pkg/dcpool" - "github.com/iyear/tdl/pkg/downloader" - "github.com/iyear/tdl/pkg/tmedia" + "github.com/iyear/tdl/core/dcpool" + "github.com/iyear/tdl/core/downloader" + "github.com/iyear/tdl/core/tmedia" + "github.com/iyear/tdl/core/util/fsutil" + "github.com/iyear/tdl/core/util/tutil" "github.com/iyear/tdl/pkg/tmessage" "github.com/iyear/tdl/pkg/tplfunc" "github.com/iyear/tdl/pkg/utils" @@ -73,8 +74,8 @@ func newIter(pool dcpool.Pool, manager *peers.Manager, dialog [][]*tmessage.Dial } // include and exclude - includeMap := filterMap(opts.Include, utils.FS.AddPrefixDot) - excludeMap := filterMap(opts.Exclude, utils.FS.AddPrefixDot) + includeMap := filterMap(opts.Include, fsutil.AddPrefixDot) + excludeMap := filterMap(opts.Exclude, fsutil.AddPrefixDot) // to keep fingerprint stable sortDialogs(dialogs, opts.Desc) @@ -190,7 +191,7 @@ func (i *iter) process(ctx context.Context) (ret bool, skip bool) { if i.opts.SkipSame { if stat, err := os.Stat(filepath.Join(i.opts.Dir, toName.String())); err == nil { - if utils.FS.GetNameWithoutExt(toName.String()) == utils.FS.GetNameWithoutExt(stat.Name()) && + if fsutil.GetNameWithoutExt(toName.String()) == fsutil.GetNameWithoutExt(stat.Name()) && stat.Size() == item.Size { return false, true } diff --git a/app/dl/progress.go b/app/dl/progress.go index c08f60f..7d57c7b 100644 --- a/app/dl/progress.go +++ b/app/dl/progress.go @@ -13,7 +13,8 @@ import ( "github.com/go-faster/errors" pw "github.com/jedib0t/go-pretty/v6/progress" - "github.com/iyear/tdl/pkg/downloader" + "github.com/iyear/tdl/core/downloader" + "github.com/iyear/tdl/core/util/fsutil" "github.com/iyear/tdl/pkg/prog" "github.com/iyear/tdl/pkg/utils" ) @@ -91,7 +92,7 @@ func (p *progress) donePost(elem *iterElem) error { } ext := mime.Extension() if ext != "" && (filepath.Ext(newfile) != ext) { - newfile = utils.FS.GetNameWithoutExt(newfile) + ext + newfile = fsutil.GetNameWithoutExt(newfile) + ext } } diff --git a/app/dl/serve.go b/app/dl/serve.go index fcb54c5..f4b6459 100644 --- a/app/dl/serve.go +++ b/app/dl/serve.go @@ -20,13 +20,13 @@ import ( "github.com/gotd/td/tg" "github.com/spf13/viper" - "github.com/iyear/tdl/core/tutil" + "github.com/iyear/tdl/core/dcpool" + "github.com/iyear/tdl/core/logctx" + "github.com/iyear/tdl/core/tmedia" + "github.com/iyear/tdl/core/util/tutil" "github.com/iyear/tdl/pkg/consts" - "github.com/iyear/tdl/pkg/dcpool" "github.com/iyear/tdl/pkg/kv" - "github.com/iyear/tdl/pkg/logger" "github.com/iyear/tdl/pkg/storage" - "github.com/iyear/tdl/pkg/tmedia" "github.com/iyear/tdl/pkg/tmessage" ) @@ -95,7 +95,7 @@ func serve(ctx context.Context, http_io.NewHandler(u, item.Size). WithContentType(item.MIME). - WithLog(logger.From(ctx).Named("serve")). + WithLog(logctx.From(ctx).Named("serve")). ServeHTTP(w, r) return nil })) diff --git a/app/forward/elem.go b/app/forward/elem.go index 9d478d4..78dde3c 100644 --- a/app/forward/elem.go +++ b/app/forward/elem.go @@ -4,7 +4,7 @@ import ( "github.com/gotd/td/telegram/peers" "github.com/gotd/td/tg" - "github.com/iyear/tdl/pkg/forwarder" + "github.com/iyear/tdl/core/forwarder" ) type iterElem struct { diff --git a/app/forward/forward.go b/app/forward/forward.go index 7b73680..4aac4a5 100644 --- a/app/forward/forward.go +++ b/app/forward/forward.go @@ -17,10 +17,10 @@ import ( "go.uber.org/multierr" "github.com/iyear/tdl/app/internal/tctx" - "github.com/iyear/tdl/core/tutil" + "github.com/iyear/tdl/core/dcpool" + "github.com/iyear/tdl/core/forwarder" + "github.com/iyear/tdl/core/util/tutil" "github.com/iyear/tdl/pkg/consts" - "github.com/iyear/tdl/pkg/dcpool" - "github.com/iyear/tdl/pkg/forwarder" "github.com/iyear/tdl/pkg/kv" "github.com/iyear/tdl/pkg/prog" "github.com/iyear/tdl/pkg/storage" diff --git a/app/forward/iter.go b/app/forward/iter.go index d87e3db..b4cd7e0 100644 --- a/app/forward/iter.go +++ b/app/forward/iter.go @@ -13,9 +13,9 @@ import ( "github.com/gotd/td/tg" "github.com/mitchellh/mapstructure" - "github.com/iyear/tdl/core/tutil" - "github.com/iyear/tdl/pkg/dcpool" - "github.com/iyear/tdl/pkg/forwarder" + "github.com/iyear/tdl/core/dcpool" + "github.com/iyear/tdl/core/forwarder" + "github.com/iyear/tdl/core/util/tutil" "github.com/iyear/tdl/pkg/texpr" "github.com/iyear/tdl/pkg/tmessage" ) diff --git a/app/forward/progress.go b/app/forward/progress.go index cf40def..c995294 100644 --- a/app/forward/progress.go +++ b/app/forward/progress.go @@ -8,7 +8,7 @@ import ( pw "github.com/jedib0t/go-pretty/v6/progress" "github.com/mattn/go-runewidth" - "github.com/iyear/tdl/pkg/forwarder" + "github.com/iyear/tdl/core/forwarder" "github.com/iyear/tdl/pkg/prog" "github.com/iyear/tdl/pkg/utils" ) diff --git a/app/internal/tctx/tctx.go b/app/internal/tctx/tctx.go index 8294b1d..979230b 100644 --- a/app/internal/tctx/tctx.go +++ b/app/internal/tctx/tctx.go @@ -3,7 +3,7 @@ package tctx import ( "context" - "github.com/iyear/tdl/pkg/dcpool" + "github.com/iyear/tdl/core/dcpool" "github.com/iyear/tdl/pkg/kv" ) diff --git a/app/login/desktop.go b/app/login/desktop.go index 3900083..ec371f4 100644 --- a/app/login/desktop.go +++ b/app/login/desktop.go @@ -14,6 +14,7 @@ import ( tdtdesktop "github.com/gotd/td/session/tdesktop" "github.com/spf13/viper" + "github.com/iyear/tdl/core/util/fsutil" "github.com/iyear/tdl/pkg/consts" "github.com/iyear/tdl/pkg/key" "github.com/iyear/tdl/pkg/kv" @@ -21,7 +22,6 @@ import ( "github.com/iyear/tdl/pkg/tclient" "github.com/iyear/tdl/pkg/tdesktop" "github.com/iyear/tdl/pkg/tpath" - "github.com/iyear/tdl/pkg/utils" ) const tdata = "tdata" @@ -124,7 +124,7 @@ func findDesktop(desktop string) (string, error) { func detectAppData() string { for _, p := range tpath.Desktop.AppData(consts.HomeDir) { - if path := appendTData(p); utils.FS.PathExists(path) { + if path := appendTData(p); fsutil.PathExists(path) { return path } } diff --git a/app/up/elem.go b/app/up/elem.go index 8c2d0a0..098fa4b 100644 --- a/app/up/elem.go +++ b/app/up/elem.go @@ -7,7 +7,7 @@ import ( "github.com/gotd/td/telegram/peers" "github.com/gotd/td/tg" - "github.com/iyear/tdl/pkg/uploader" + "github.com/iyear/tdl/core/uploader" ) type iterElem struct { diff --git a/app/up/iter.go b/app/up/iter.go index 11a7d45..7288814 100644 --- a/app/up/iter.go +++ b/app/up/iter.go @@ -9,8 +9,8 @@ import ( "github.com/go-faster/errors" "github.com/gotd/td/telegram/peers" - "github.com/iyear/tdl/pkg/uploader" - "github.com/iyear/tdl/pkg/utils" + "github.com/iyear/tdl/core/uploader" + "github.com/iyear/tdl/core/util/mediautil" ) type file struct { @@ -74,7 +74,7 @@ func (i *iter) Next(ctx context.Context) bool { // has thumbnail if cur.thumb != "" { tMime, err := mimetype.DetectFile(cur.thumb) - if err != nil || !utils.Media.IsImage(tMime.String()) { // TODO(iyear): jpg only + if err != nil || !mediautil.IsImage(tMime.String()) { // TODO(iyear): jpg only i.err = errors.Wrapf(err, "invalid thumbnail file: %v", cur.thumb) return false } diff --git a/app/up/progress.go b/app/up/progress.go index 0145131..b3c6d25 100644 --- a/app/up/progress.go +++ b/app/up/progress.go @@ -9,8 +9,8 @@ import ( "github.com/go-faster/errors" pw "github.com/jedib0t/go-pretty/v6/progress" + "github.com/iyear/tdl/core/uploader" "github.com/iyear/tdl/pkg/prog" - "github.com/iyear/tdl/pkg/uploader" "github.com/iyear/tdl/pkg/utils" ) diff --git a/app/up/up.go b/app/up/up.go index 23a5580..00f2d46 100644 --- a/app/up/up.go +++ b/app/up/up.go @@ -11,14 +11,14 @@ import ( "github.com/spf13/viper" "go.uber.org/multierr" - "github.com/iyear/tdl/core/tutil" + "github.com/iyear/tdl/core/dcpool" + "github.com/iyear/tdl/core/uploader" + "github.com/iyear/tdl/core/util/tutil" "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/tclient" - "github.com/iyear/tdl/pkg/uploader" "github.com/iyear/tdl/pkg/utils" ) diff --git a/app/up/walk.go b/app/up/walk.go index e493b2d..e7f8a32 100644 --- a/app/up/walk.go +++ b/app/up/walk.go @@ -5,8 +5,8 @@ import ( "path/filepath" "strings" + "github.com/iyear/tdl/core/util/fsutil" "github.com/iyear/tdl/pkg/consts" - "github.com/iyear/tdl/pkg/utils" ) func walk(paths, excludes []string) ([]*file, error) { @@ -33,7 +33,7 @@ func walk(paths, excludes []string) ([]*file, error) { f := file{file: path} t := strings.TrimRight(path, filepath.Ext(path)) + consts.UploadThumbExt - if utils.FS.PathExists(t) { + if fsutil.PathExists(t) { f.thumb = t } diff --git a/cmd/chat.go b/cmd/chat.go index 0e3182f..9b11c4e 100644 --- a/cmd/chat.go +++ b/cmd/chat.go @@ -13,8 +13,8 @@ import ( "golang.org/x/time/rate" "github.com/iyear/tdl/app/chat" + "github.com/iyear/tdl/core/logctx" "github.com/iyear/tdl/pkg/kv" - "github.com/iyear/tdl/pkg/logger" ) var limiter = ratelimit.New(rate.Every(500*time.Millisecond), 2) @@ -38,7 +38,7 @@ func NewChatList() *cobra.Command { Short: "List your chats", RunE: func(cmd *cobra.Command, args []string) error { return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd kv.KV) error { - return chat.List(logger.Named(ctx, "ls"), c, kvd, opts) + return chat.List(logctx.Named(ctx, "ls"), c, kvd, opts) }, limiter) }, } @@ -83,7 +83,7 @@ func NewChatExport() *cobra.Command { } return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd kv.KV) error { - return chat.Export(logger.Named(ctx, "export"), c, kvd, opts) + return chat.Export(logctx.Named(ctx, "export"), c, kvd, opts) }, limiter) }, } @@ -138,7 +138,7 @@ func NewChatUsers() *cobra.Command { Short: "export users from (protected) channels", RunE: func(cmd *cobra.Command, args []string) error { return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd kv.KV) error { - return chat.Users(logger.Named(ctx, "users"), c, kvd, opts) + return chat.Users(logctx.Named(ctx, "users"), c, kvd, opts) }, limiter) }, } diff --git a/cmd/dl.go b/cmd/dl.go index 5d71c58..592180e 100644 --- a/cmd/dl.go +++ b/cmd/dl.go @@ -10,9 +10,9 @@ import ( "github.com/spf13/viper" "github.com/iyear/tdl/app/dl" + "github.com/iyear/tdl/core/logctx" "github.com/iyear/tdl/pkg/consts" "github.com/iyear/tdl/pkg/kv" - "github.com/iyear/tdl/pkg/logger" ) func NewDownload() *cobra.Command { @@ -30,7 +30,7 @@ func NewDownload() *cobra.Command { opts.Template = viper.GetString(consts.FlagDlTemplate) return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd kv.KV) error { - return dl.Run(logger.Named(ctx, "dl"), c, kvd, opts) + return dl.Run(logctx.Named(ctx, "dl"), c, kvd, opts) }) }, } diff --git a/cmd/forward.go b/cmd/forward.go index 6eb6595..cffbb37 100644 --- a/cmd/forward.go +++ b/cmd/forward.go @@ -9,9 +9,9 @@ import ( "github.com/spf13/cobra" "github.com/iyear/tdl/app/forward" - "github.com/iyear/tdl/pkg/forwarder" + "github.com/iyear/tdl/core/forwarder" + "github.com/iyear/tdl/core/logctx" "github.com/iyear/tdl/pkg/kv" - "github.com/iyear/tdl/pkg/logger" ) func NewForward() *cobra.Command { @@ -22,7 +22,7 @@ func NewForward() *cobra.Command { Short: "Forward messages with automatic fallback and message routing", RunE: func(cmd *cobra.Command, args []string) error { return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd kv.KV) error { - return forward.Run(logger.Named(ctx, "forward"), c, kvd, opts) + return forward.Run(logctx.Named(ctx, "forward"), c, kvd, opts) }) }, } diff --git a/cmd/gen.go b/cmd/gen.go index 5ae5d0f..0d6420d 100644 --- a/cmd/gen.go +++ b/cmd/gen.go @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/cobra/doc" - "github.com/iyear/tdl/pkg/utils" + "github.com/iyear/tdl/core/util/fsutil" ) func NewGen() *cobra.Command { @@ -44,7 +44,7 @@ bookHidden: true c.DisableAutoGenTag = true }) - if !utils.FS.PathExists(dir) { + if !fsutil.PathExists(dir) { if err := os.MkdirAll(dir, os.ModePerm); err != nil { return errors.Wrap(err, "mkdir") } diff --git a/cmd/login.go b/cmd/login.go index d4afb14..2ee5d46 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" "github.com/iyear/tdl/app/login" - "github.com/iyear/tdl/pkg/logger" + "github.com/iyear/tdl/core/logctx" ) func NewLogin() *cobra.Command { @@ -25,10 +25,10 @@ func NewLogin() *cobra.Command { // Legacy flag if code { - return login.Code(logger.Named(cmd.Context(), "login")) + return login.Code(logctx.Named(cmd.Context(), "login")) } - return login.Run(logger.Named(cmd.Context(), "login"), opts) + return login.Run(logctx.Named(cmd.Context(), "login"), opts) }, } diff --git a/cmd/root.go b/cmd/root.go index 6a2d0f3..04e8ddd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -14,11 +14,12 @@ import ( "go.uber.org/multierr" "go.uber.org/zap" + "github.com/iyear/tdl/core/logctx" + "github.com/iyear/tdl/core/util/fsutil" "github.com/iyear/tdl/pkg/consts" "github.com/iyear/tdl/pkg/kv" "github.com/iyear/tdl/pkg/logger" "github.com/iyear/tdl/pkg/tclient" - "github.com/iyear/tdl/pkg/utils" ) var ( @@ -46,17 +47,17 @@ func New() *cobra.Command { if debug { level = zap.DebugLevel } - cmd.SetContext(logger.With(cmd.Context(), + cmd.SetContext(logctx.With(cmd.Context(), logger.New(level, filepath.Join(consts.LogPath, "latest.log")))) ns := viper.GetString(consts.FlagNamespace) if ns != "" { - logger.From(cmd.Context()).Info("Namespace", + logctx.From(cmd.Context()).Info("Namespace", zap.String("namespace", ns)) } // v0.14.0: default storage changed from legacy to bolt, so we need to auto migrate to keep compatibility - if !cmd.Flags().Lookup(consts.FlagStorage).Changed && !utils.FS.PathExists(defaultBoltPath) { + if !cmd.Flags().Lookup(consts.FlagStorage).Changed && !fsutil.PathExists(defaultBoltPath) { if err := migrateLegacyToBolt(); err != nil { return errors.Wrap(err, "migrate legacy to bolt") } @@ -73,7 +74,7 @@ func New() *cobra.Command { PersistentPostRunE: func(cmd *cobra.Command, args []string) error { return multierr.Combine( kv.From(cmd.Context()).Close(), - logger.From(cmd.Context()).Sync(), + logctx.From(cmd.Context()).Sync(), ) }, } diff --git a/cmd/up.go b/cmd/up.go index 17741fe..2e3aa91 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -7,8 +7,8 @@ import ( "github.com/spf13/cobra" "github.com/iyear/tdl/app/up" + "github.com/iyear/tdl/core/logctx" "github.com/iyear/tdl/pkg/kv" - "github.com/iyear/tdl/pkg/logger" ) func NewUpload() *cobra.Command { @@ -20,7 +20,7 @@ func NewUpload() *cobra.Command { Short: "Upload anything to Telegram", RunE: func(cmd *cobra.Command, args []string) error { return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd kv.KV) error { - return up.Run(logger.Named(ctx, "up"), c, kvd, opts) + return up.Run(logctx.Named(ctx, "up"), c, kvd, opts) }) }, } diff --git a/pkg/dcpool/dcpool.go b/core/dcpool/dcpool.go similarity index 89% rename from pkg/dcpool/dcpool.go rename to core/dcpool/dcpool.go index a43e72d..e929cea 100644 --- a/pkg/dcpool/dcpool.go +++ b/core/dcpool/dcpool.go @@ -9,8 +9,8 @@ import ( "go.uber.org/multierr" "go.uber.org/zap" - "github.com/iyear/tdl/pkg/logger" - "github.com/iyear/tdl/pkg/takeout" + "github.com/iyear/tdl/core/logctx" + "github.com/iyear/tdl/core/middlewares/takeout" ) type Pool interface { @@ -71,7 +71,7 @@ func (p *pool) invoker(ctx context.Context, dc int) tg.Invoker { } if err != nil { - logger.From(ctx).Error("create invoker", zap.Error(err)) + logctx.From(ctx).Error("create invoker", zap.Error(err)) return p.api // degraded } @@ -105,12 +105,12 @@ func (p *pool) Takeout(ctx context.Context, dc int) *tg.Client { if p.takeout == 0 { sid, err := takeout.Takeout(ctx, p.api) if err != nil { - logger.From(ctx).Warn("takeout error", zap.Error(err)) + logctx.From(ctx).Warn("takeout error", zap.Error(err)) // ignore init delay error and return non-takeout client return p.Client(ctx, dc) } p.takeout = sid - logger.From(ctx).Info("get takeout id", zap.Int64("id", sid)) + logctx.From(ctx).Info("get takeout id", zap.Int64("id", sid)) } return tg.NewClient(chainMiddlewares(p.invoker(ctx, dc), takeout.Middleware(p.takeout))) diff --git a/pkg/dcpool/middlewares.go b/core/dcpool/middlewares.go similarity index 100% rename from pkg/dcpool/middlewares.go rename to core/dcpool/middlewares.go diff --git a/pkg/downloader/downloader.go b/core/downloader/downloader.go similarity index 91% rename from pkg/downloader/downloader.go rename to core/downloader/downloader.go index c7c79a8..fded2b5 100644 --- a/pkg/downloader/downloader.go +++ b/core/downloader/downloader.go @@ -8,9 +8,9 @@ import ( "go.uber.org/zap" "golang.org/x/sync/errgroup" - "github.com/iyear/tdl/core/tutil" - "github.com/iyear/tdl/pkg/dcpool" - "github.com/iyear/tdl/pkg/logger" + "github.com/iyear/tdl/core/dcpool" + "github.com/iyear/tdl/core/logctx" + "github.com/iyear/tdl/core/util/tutil" ) type Downloader struct { @@ -69,7 +69,7 @@ func (d *Downloader) download(ctx context.Context, elem Elem) error { default: } - logger.From(ctx).Debug("Start download elem", + logctx.From(ctx).Debug("Start download elem", zap.Any("elem", elem)) client := d.opts.Pool.Client(ctx, elem.File().DC()) diff --git a/pkg/downloader/iter.go b/core/downloader/iter.go similarity index 100% rename from pkg/downloader/iter.go rename to core/downloader/iter.go diff --git a/pkg/downloader/progress.go b/core/downloader/progress.go similarity index 100% rename from pkg/downloader/progress.go rename to core/downloader/progress.go diff --git a/pkg/forwarder/clone.go b/core/forwarder/clone.go similarity index 96% rename from pkg/forwarder/clone.go rename to core/forwarder/clone.go index e92e423..78ea2c9 100644 --- a/pkg/forwarder/clone.go +++ b/core/forwarder/clone.go @@ -12,8 +12,8 @@ import ( "go.uber.org/atomic" "go.uber.org/multierr" - "github.com/iyear/tdl/core/tutil" - "github.com/iyear/tdl/pkg/tmedia" + "github.com/iyear/tdl/core/tmedia" + "github.com/iyear/tdl/core/util/tutil" ) type cloneOptions struct { diff --git a/pkg/forwarder/forwarder.go b/core/forwarder/forwarder.go similarity index 98% rename from pkg/forwarder/forwarder.go rename to core/forwarder/forwarder.go index 3224b7f..0569533 100644 --- a/pkg/forwarder/forwarder.go +++ b/core/forwarder/forwarder.go @@ -12,10 +12,10 @@ import ( "go.uber.org/atomic" "go.uber.org/zap" - "github.com/iyear/tdl/core/tutil" - "github.com/iyear/tdl/pkg/dcpool" - "github.com/iyear/tdl/pkg/logger" - "github.com/iyear/tdl/pkg/tmedia" + "github.com/iyear/tdl/core/dcpool" + "github.com/iyear/tdl/core/logctx" + "github.com/iyear/tdl/core/tmedia" + "github.com/iyear/tdl/core/util/tutil" ) //go:generate go-enum --values --names --flag --nocase @@ -96,7 +96,7 @@ func (f *Forwarder) forwardMessage(ctx context.Context, elem Elem, grouped ...*t f.opts.Progress.OnDone(elem, rerr) }() - log := logger.From(ctx).With( + log := logctx.From(ctx).With( zap.Int64("from", elem.From().ID()), zap.Int64("to", elem.To().ID()), zap.Int("message", elem.Msg().ID)) diff --git a/pkg/forwarder/forwarder_enum.go b/core/forwarder/forwarder_enum.go similarity index 100% rename from pkg/forwarder/forwarder_enum.go rename to core/forwarder/forwarder_enum.go diff --git a/pkg/forwarder/iter.go b/core/forwarder/iter.go similarity index 100% rename from pkg/forwarder/iter.go rename to core/forwarder/iter.go diff --git a/pkg/forwarder/progress.go b/core/forwarder/progress.go similarity index 100% rename from pkg/forwarder/progress.go rename to core/forwarder/progress.go diff --git a/core/go.mod b/core/go.mod index 399a6be..99d0494 100644 --- a/core/go.mod +++ b/core/go.mod @@ -3,12 +3,18 @@ module github.com/iyear/tdl/core go 1.21 require ( + github.com/cenkalti/backoff/v4 v4.3.0 + github.com/gabriel-vasile/mimetype v1.4.4 github.com/go-faster/errors v0.7.1 github.com/gotd/td v0.102.0 + github.com/yapingcat/gomedia v0.0.0-20240601043430-920523f8e5c7 + go.uber.org/atomic v1.11.0 + go.uber.org/multierr v1.11.0 + go.uber.org/zap v1.27.0 + golang.org/x/sync v0.7.0 ) require ( - github.com/cenkalti/backoff/v4 v4.3.0 // indirect github.com/go-faster/jx v1.1.0 // indirect github.com/go-faster/xor v1.0.0 // indirect github.com/gotd/ige v0.2.2 // indirect @@ -17,13 +23,9 @@ require ( github.com/segmentio/asm v1.2.0 // indirect go.opentelemetry.io/otel v1.26.0 // indirect go.opentelemetry.io/otel/trace v1.26.0 // indirect - go.uber.org/atomic v1.11.0 // indirect - go.uber.org/multierr v1.11.0 // indirect - go.uber.org/zap v1.27.0 // indirect - golang.org/x/crypto v0.22.0 // indirect - golang.org/x/net v0.24.0 // indirect - golang.org/x/sync v0.7.0 // indirect - golang.org/x/sys v0.19.0 // indirect + golang.org/x/crypto v0.23.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.20.0 // indirect nhooyr.io/websocket v1.8.11 // indirect rsc.io/qr v0.2.0 // indirect ) diff --git a/core/go.sum b/core/go.sum index b283fea..d7e6a3a 100644 --- a/core/go.sum +++ b/core/go.sum @@ -2,6 +2,8 @@ github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK3 github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I= +github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s= github.com/go-faster/errors v0.7.1 h1:MkJTnDoEdi9pDabt1dpWf7AA8/BaSYZqibYyhZ20AYg= github.com/go-faster/errors v0.7.1/go.mod h1:5ySTjWFiphBs07IKuiL69nxdfd5+fzh1u7FPGZP2quo= github.com/go-faster/jx v1.1.0 h1:ZsW3wD+snOdmTDy9eIVgQdjUpXRRV4rqW8NS3t+20bg= @@ -25,6 +27,8 @@ github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys= github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/yapingcat/gomedia v0.0.0-20240601043430-920523f8e5c7 h1:e9n2WNcfvs20aLgpDhKoaJgrU/EeAvuNnWLBm31Q5Fw= +github.com/yapingcat/gomedia v0.0.0-20240601043430-920523f8e5c7/go.mod h1:WSZ59bidJOO40JSJmLqlkBJrjZCtjbKKkygEMfzY/kc= go.opentelemetry.io/otel v1.26.0 h1:LQwgL5s/1W7YiiRwxf03QGnWLb2HW4pLiAhaA5cZXBs= go.opentelemetry.io/otel v1.26.0/go.mod h1:UmLkJHUAidDval2EICqBMbnAd0/m2vmpf/dAM+fvFs4= go.opentelemetry.io/otel/trace v1.26.0 h1:1ieeAUb4y0TE26jUFrCIXKpTuVK7uJGN9/Z/2LP5sQA= @@ -37,17 +41,17 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= -golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= -golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/exp v0.0.0-20230116083435-1de6713980de h1:DBWn//IJw30uYCgERoxCg84hWtA97F4wMiKOIh00Uf0= golang.org/x/exp v0.0.0-20230116083435-1de6713980de/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= -golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= -golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= -golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0= diff --git a/core/logctx/logctx.go b/core/logctx/logctx.go new file mode 100644 index 0000000..fc4761b --- /dev/null +++ b/core/logctx/logctx.go @@ -0,0 +1,21 @@ +package logctx + +import ( + "context" + + "go.uber.org/zap" +) + +type ctxKey struct{} + +func From(ctx context.Context) *zap.Logger { + return ctx.Value(ctxKey{}).(*zap.Logger) +} + +func With(ctx context.Context, logger *zap.Logger) context.Context { + return context.WithValue(ctx, ctxKey{}, logger) +} + +func Named(ctx context.Context, name string) context.Context { + return With(ctx, From(ctx).Named(name)) +} diff --git a/pkg/recovery/recovery.go b/core/middlewares/recovery/recovery.go similarity index 95% rename from pkg/recovery/recovery.go rename to core/middlewares/recovery/recovery.go index 383fed3..7ff937f 100644 --- a/pkg/recovery/recovery.go +++ b/core/middlewares/recovery/recovery.go @@ -12,7 +12,7 @@ import ( "github.com/gotd/td/tgerr" "go.uber.org/zap" - "github.com/iyear/tdl/pkg/logger" + "github.com/iyear/tdl/core/logctx" ) type recovery struct { @@ -29,7 +29,7 @@ func New(ctx context.Context, backoff backoff.BackOff) telegram.Middleware { func (r *recovery) Handle(next tg.Invoker) telegram.InvokeFunc { return func(ctx context.Context, input bin.Encoder, output bin.Decoder) error { - log := logger.From(ctx) + log := logctx.From(ctx) return backoff.RetryNotify(func() error { if err := next.Invoke(ctx, input, output); err != nil { diff --git a/pkg/retry/retry.go b/core/middlewares/retry/retry.go similarity index 92% rename from pkg/retry/retry.go rename to core/middlewares/retry/retry.go index 71a433a..4ee9fda 100644 --- a/pkg/retry/retry.go +++ b/core/middlewares/retry/retry.go @@ -11,7 +11,7 @@ import ( "github.com/gotd/td/tgerr" "go.uber.org/zap" - "github.com/iyear/tdl/pkg/logger" + "github.com/iyear/tdl/core/logctx" ) var internalErrors = []string{ @@ -35,7 +35,7 @@ func (r retry) Handle(next tg.Invoker) telegram.InvokeFunc { for retries < r.max { if err := next.Invoke(ctx, input, output); err != nil { if tgerr.Is(err, r.errors...) { - logger.From(ctx).Debug("retry middleware", zap.Int("retries", retries), zap.Error(err)) + logctx.From(ctx).Debug("retry middleware", zap.Int("retries", retries), zap.Error(err)) retries++ continue } diff --git a/pkg/takeout/middleware.go b/core/middlewares/takeout/middleware.go similarity index 100% rename from pkg/takeout/middleware.go rename to core/middlewares/takeout/middleware.go diff --git a/pkg/takeout/takeout.go b/core/middlewares/takeout/takeout.go similarity index 91% rename from pkg/takeout/takeout.go rename to core/middlewares/takeout/takeout.go index 9a10064..f2d7425 100644 --- a/pkg/takeout/takeout.go +++ b/core/middlewares/takeout/takeout.go @@ -4,8 +4,6 @@ import ( "context" "github.com/gotd/td/tg" - - "github.com/iyear/tdl/pkg/consts" ) func Takeout(ctx context.Context, invoker tg.Invoker) (int64, error) { @@ -16,7 +14,7 @@ func Takeout(ctx context.Context, invoker tg.Invoker) (int64, error) { MessageMegagroups: true, MessageChannels: true, Files: true, - FileMaxSize: consts.FileMaxSize, + FileMaxSize: 4000 * 1024 * 1024, } req.SetFlags() diff --git a/pkg/tmedia/convert.go b/core/tmedia/convert.go similarity index 100% rename from pkg/tmedia/convert.go rename to core/tmedia/convert.go diff --git a/pkg/tmedia/document.go b/core/tmedia/document.go similarity index 100% rename from pkg/tmedia/document.go rename to core/tmedia/document.go diff --git a/pkg/tmedia/media.go b/core/tmedia/media.go similarity index 100% rename from pkg/tmedia/media.go rename to core/tmedia/media.go diff --git a/pkg/tmedia/photo.go b/core/tmedia/photo.go similarity index 100% rename from pkg/tmedia/photo.go rename to core/tmedia/photo.go diff --git a/pkg/uploader/iter.go b/core/uploader/iter.go similarity index 100% rename from pkg/uploader/iter.go rename to core/uploader/iter.go diff --git a/pkg/uploader/progress.go b/core/uploader/progress.go similarity index 100% rename from pkg/uploader/progress.go rename to core/uploader/progress.go diff --git a/pkg/uploader/uploader.go b/core/uploader/uploader.go similarity index 89% rename from pkg/uploader/uploader.go rename to core/uploader/uploader.go index d380a22..f513524 100644 --- a/pkg/uploader/uploader.go +++ b/core/uploader/uploader.go @@ -13,7 +13,8 @@ import ( "github.com/gotd/td/tg" "golang.org/x/sync/errgroup" - "github.com/iyear/tdl/pkg/utils" + "github.com/iyear/tdl/core/util/fsutil" + "github.com/iyear/tdl/core/util/mediautil" ) type Uploader struct { @@ -110,27 +111,27 @@ func (u *Uploader) upload(ctx context.Context, elem Elem) error { var media message.MediaOption = doc switch { - case utils.Media.IsImage(mime.String()) && elem.AsPhoto(): + case mediautil.IsImage(mime.String()) && elem.AsPhoto(): // webp should be uploaded as document if mime.String() == "image/webp" { break } // upload as photo media = message.UploadedPhoto(f, caption...) - case utils.Media.IsVideo(mime.String()): + case mediautil.IsVideo(mime.String()): // reset reader if _, err = elem.File().Seek(0, io.SeekStart); err != nil { return errors.Wrap(err, "seek file") } - if dur, w, h, err := utils.Media.GetMP4Info(elem.File()); err == nil { + if dur, w, h, err := mediautil.GetMP4Info(elem.File()); err == nil { // #132. There may be some errors, but we can still upload the file media = doc.Video(). Duration(time.Duration(dur)*time.Second). Resolution(w, h). SupportsStreaming() } - case utils.Media.IsAudio(mime.String()): - media = doc.Audio().Title(utils.FS.GetNameWithoutExt(elem.File().Name())) + case mediautil.IsAudio(mime.String()): + media = doc.Audio().Title(fsutil.GetNameWithoutExt(elem.File().Name())) } _, err = message.NewSender(u.opts.Client). diff --git a/pkg/utils/fs.go b/core/util/fsutil/fsutil.go similarity index 62% rename from pkg/utils/fs.go rename to core/util/fsutil/fsutil.go index 2efc0d4..8bb817c 100644 --- a/pkg/utils/fs.go +++ b/core/util/fsutil/fsutil.go @@ -1,4 +1,4 @@ -package utils +package fsutil import ( "os" @@ -6,21 +6,17 @@ import ( "strings" ) -type fs struct{} - -var FS = fs{} - -func (f fs) GetNameWithoutExt(path string) string { +func GetNameWithoutExt(path string) string { return strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)) } -func (f fs) PathExists(path string) bool { +func PathExists(path string) bool { _, err := os.Stat(path) return err == nil || os.IsExist(err) } // AddPrefixDot add prefix dot if extension don't have -func (f fs) AddPrefixDot(ext string) string { +func AddPrefixDot(ext string) string { if !strings.HasPrefix(ext, ".") { return "." + ext } diff --git a/pkg/utils/media.go b/core/util/mediautil/mediautil.go similarity index 62% rename from pkg/utils/media.go rename to core/util/mediautil/mediautil.go index e465655..48efc92 100644 --- a/pkg/utils/media.go +++ b/core/util/mediautil/mediautil.go @@ -1,4 +1,4 @@ -package utils +package mediautil import ( "fmt" @@ -8,11 +8,7 @@ import ( "github.com/yapingcat/gomedia/go-mp4" ) -type media struct{} - -var Media = media{} - -func (m media) split(mime string) (primary string, sub string, ok bool) { +func split(mime string) (primary string, sub string, ok bool) { types := strings.Split(mime, "/") if len(types) != 2 { @@ -22,26 +18,26 @@ func (m media) split(mime string) (primary string, sub string, ok bool) { return types[0], types[1], true } -func (m media) IsVideo(mime string) bool { - primary, _, ok := m.split(mime) +func IsVideo(mime string) bool { + primary, _, ok := split(mime) return primary == "video" && ok } -func (m media) IsAudio(mime string) bool { - primary, _, ok := m.split(mime) +func IsAudio(mime string) bool { + primary, _, ok := split(mime) return primary == "audio" && ok } -func (m media) IsImage(mime string) bool { - primary, _, ok := m.split(mime) +func IsImage(mime string) bool { + primary, _, ok := split(mime) return primary == "image" && ok } // GetMP4Info returns duration, width, height, error -func (m media) GetMP4Info(r io.ReadSeeker) (int, int, int, error) { +func GetMP4Info(r io.ReadSeeker) (int, int, int, error) { d := mp4.CreateMp4Demuxer(r) tracks, err := d.ReadHead() @@ -58,3 +54,4 @@ func (m media) GetMP4Info(r io.ReadSeeker) (int, int, int, error) { return 0, 0, 0, fmt.Errorf("no h264 track found") } + diff --git a/core/tutil/tutil.go b/core/util/tutil/tutil.go similarity index 100% rename from core/tutil/tutil.go rename to core/util/tutil/tutil.go diff --git a/go.mod b/go.mod index adde2c5..d66430e 100644 --- a/go.mod +++ b/go.mod @@ -36,9 +36,7 @@ require ( github.com/spf13/viper v1.18.2 github.com/stretchr/testify v1.9.0 github.com/tidwall/gjson v1.17.1 - github.com/yapingcat/gomedia v0.0.0-20230727105416-c491e66c9d2a go.etcd.io/bbolt v1.3.10 - go.uber.org/atomic v1.11.0 go.uber.org/multierr v1.11.0 go.uber.org/zap v1.27.0 golang.org/x/net v0.25.0 @@ -87,9 +85,11 @@ require ( github.com/tidwall/pretty v1.2.1 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/numcpus v0.6.1 // indirect + github.com/yapingcat/gomedia v0.0.0-20240601043430-920523f8e5c7 // indirect github.com/yusufpapurcu/wmi v1.2.4 // indirect go.opentelemetry.io/otel v1.26.0 // indirect go.opentelemetry.io/otel/trace v1.26.0 // indirect + go.uber.org/atomic v1.11.0 // indirect golang.org/x/crypto v0.23.0 // indirect golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/sys v0.20.0 // indirect diff --git a/go.sum b/go.sum index 4bce663..e38582d 100644 --- a/go.sum +++ b/go.sum @@ -179,8 +179,8 @@ github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFA github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI= github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk= github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY= -github.com/yapingcat/gomedia v0.0.0-20230727105416-c491e66c9d2a h1:x60q0A7QmoUTzixNz7zVTdEA9JC0oYqm8S51PdbTWgs= -github.com/yapingcat/gomedia v0.0.0-20230727105416-c491e66c9d2a/go.mod h1:WSZ59bidJOO40JSJmLqlkBJrjZCtjbKKkygEMfzY/kc= +github.com/yapingcat/gomedia v0.0.0-20240601043430-920523f8e5c7 h1:e9n2WNcfvs20aLgpDhKoaJgrU/EeAvuNnWLBm31Q5Fw= +github.com/yapingcat/gomedia v0.0.0-20240601043430-920523f8e5c7/go.mod h1:WSZ59bidJOO40JSJmLqlkBJrjZCtjbKKkygEMfzY/kc= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= diff --git a/pkg/consts/telegram.go b/pkg/consts/telegram.go deleted file mode 100644 index 670b788..0000000 --- a/pkg/consts/telegram.go +++ /dev/null @@ -1,3 +0,0 @@ -package consts - -const FileMaxSize = 4000 * 1024 * 1024 diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index ea1634b..d1f0b60 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -1,27 +1,11 @@ package logger import ( - "context" - "go.uber.org/zap" "go.uber.org/zap/zapcore" "gopkg.in/natefinch/lumberjack.v2" ) -type ctxKey struct{} - -func From(ctx context.Context) *zap.Logger { - return ctx.Value(ctxKey{}).(*zap.Logger) -} - -func With(ctx context.Context, logger *zap.Logger) context.Context { - return context.WithValue(ctx, ctxKey{}, logger) -} - -func Named(ctx context.Context, name string) context.Context { - return With(ctx, From(ctx).Named(name)) -} - func New(level zapcore.LevelEnabler, path string) *zap.Logger { rotate := &lumberjack.Logger{ Filename: path, diff --git a/pkg/tclient/tclient.go b/pkg/tclient/tclient.go index e2aa35d..83a8eb4 100644 --- a/pkg/tclient/tclient.go +++ b/pkg/tclient/tclient.go @@ -15,12 +15,12 @@ import ( "golang.org/x/net/proxy" "golang.org/x/time/rate" + "github.com/iyear/tdl/core/logctx" + "github.com/iyear/tdl/core/middlewares/recovery" + "github.com/iyear/tdl/core/middlewares/retry" "github.com/iyear/tdl/pkg/clock" "github.com/iyear/tdl/pkg/key" "github.com/iyear/tdl/pkg/kv" - "github.com/iyear/tdl/pkg/logger" - "github.com/iyear/tdl/pkg/recovery" - "github.com/iyear/tdl/pkg/retry" "github.com/iyear/tdl/pkg/storage" "github.com/iyear/tdl/pkg/utils" ) @@ -79,7 +79,7 @@ func New(ctx context.Context, o Options, login bool, middlewares ...telegram.Mid DialTimeout: 10 * time.Second, Middlewares: append(NewDefaultMiddlewares(ctx, o.ReconnectTimeout), middlewares...), Clock: _clock, - Logger: logger.From(ctx).Named("td"), + Logger: logctx.From(ctx).Named("td"), } // test mode, hook options diff --git a/pkg/texpr/env.go b/pkg/texpr/env.go index 5d82bde..4deb235 100644 --- a/pkg/texpr/env.go +++ b/pkg/texpr/env.go @@ -3,8 +3,8 @@ package texpr import ( "github.com/gotd/td/tg" - "github.com/iyear/tdl/core/tutil" - "github.com/iyear/tdl/pkg/tmedia" + "github.com/iyear/tdl/core/tmedia" + "github.com/iyear/tdl/core/util/tutil" ) type EnvMessage struct { diff --git a/pkg/tmessage/files.go b/pkg/tmessage/files.go index 2d80b6e..d8bef22 100644 --- a/pkg/tmessage/files.go +++ b/pkg/tmessage/files.go @@ -13,10 +13,10 @@ import ( "github.com/mitchellh/mapstructure" "go.uber.org/zap" - "github.com/iyear/tdl/core/tutil" - "github.com/iyear/tdl/pkg/dcpool" + "github.com/iyear/tdl/core/dcpool" + "github.com/iyear/tdl/core/logctx" + "github.com/iyear/tdl/core/util/tutil" "github.com/iyear/tdl/pkg/kv" - "github.com/iyear/tdl/pkg/logger" "github.com/iyear/tdl/pkg/storage" ) @@ -46,7 +46,7 @@ func FromFile(ctx context.Context, pool dcpool.Pool, kvd kv.KV, files []string, return nil, err } - logger.From(ctx).Debug("Parse file", + logctx.From(ctx).Debug("Parse file", zap.String("file", file), zap.Int("num", len(d.Messages))) dialogs = append(dialogs, d) @@ -69,7 +69,7 @@ func parseFile(ctx context.Context, client *tg.Client, kvd kv.KV, file string, o if err != nil { return nil, err } - logger.From(ctx).Debug("Got peer info", + logctx.From(ctx).Debug("Got peer info", zap.Int64("id", peer.ID()), zap.String("name", peer.VisibleName())) diff --git a/pkg/tmessage/urls.go b/pkg/tmessage/urls.go index bb7bf19..1fc5877 100644 --- a/pkg/tmessage/urls.go +++ b/pkg/tmessage/urls.go @@ -6,10 +6,10 @@ import ( "github.com/gotd/td/telegram/peers" "go.uber.org/zap" - "github.com/iyear/tdl/core/tutil" - "github.com/iyear/tdl/pkg/dcpool" + "github.com/iyear/tdl/core/dcpool" + "github.com/iyear/tdl/core/logctx" + "github.com/iyear/tdl/core/util/tutil" "github.com/iyear/tdl/pkg/kv" - "github.com/iyear/tdl/pkg/logger" "github.com/iyear/tdl/pkg/storage" ) @@ -24,7 +24,7 @@ func FromURL(ctx context.Context, pool dcpool.Pool, kvd kv.KV, urls []string) Pa if err != nil { return nil, err } - logger.From(ctx).Debug("Parse URL", + logctx.From(ctx).Debug("Parse URL", zap.String("url", u), zap.Int64("peer_id", ch.ID()), zap.String("peer_name", ch.VisibleName()), diff --git a/pkg/tpath/tpath_linux.go b/pkg/tpath/tpath_linux.go index 93e64c6..a260421 100644 --- a/pkg/tpath/tpath_linux.go +++ b/pkg/tpath/tpath_linux.go @@ -5,7 +5,7 @@ package tpath import ( "path/filepath" - "github.com/iyear/tdl/pkg/utils" + "github.com/iyear/tdl/core/util/fsutil" ) // https://github.com/telegramdesktop/tdesktop/blob/dev/Telegram/SourceFiles/platform/linux/specific_linux.cpp#L669-L684 @@ -13,7 +13,7 @@ func desktopAppData(homedir string) []string { oldPath := filepath.Join(homedir, ".TelegramDesktop") suffixes := []string{"0", "1", "s"} for _, s := range suffixes { - if utils.FS.PathExists(filepath.Join(oldPath, "tdata", "settings"+s)) { + if fsutil.PathExists(filepath.Join(oldPath, "tdata", "settings"+s)) { return []string{oldPath} } }