refactor(prj): separate packages to core module

This commit is contained in:
iyear 2024-06-06 23:00:27 +08:00
parent f0b19035b0
commit 372f922c34
62 changed files with 179 additions and 176 deletions

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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)

View File

@ -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 {

View File

@ -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
}

View File

@ -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
}
}

View File

@ -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
}))

View File

@ -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 {

View File

@ -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"

View File

@ -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"
)

View File

@ -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"
)

View File

@ -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"
)

View File

@ -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
}
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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"
)

View File

@ -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"
)

View File

@ -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
}

View File

@ -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)
},
}

View File

@ -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)
})
},
}

View File

@ -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)
})
},
}

View File

@ -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")
}

View File

@ -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)
},
}

View File

@ -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(),
)
},
}

View File

@ -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)
})
},
}

View File

@ -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)))

View File

@ -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())

View File

@ -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 {

View File

@ -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))

View File

@ -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
)

View File

@ -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=

21
core/logctx/logctx.go Normal file
View File

@ -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))
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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()

View File

@ -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).

View File

@ -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
}

View File

@ -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")
}

4
go.mod
View File

@ -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

4
go.sum
View File

@ -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=

View File

@ -1,3 +0,0 @@
package consts
const FileMaxSize = 4000 * 1024 * 1024

View File

@ -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,

View File

@ -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

View File

@ -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 {

View File

@ -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()))

View File

@ -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()),

View File

@ -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}
}
}