diff --git a/app/dl/iter.go b/app/dl/iter.go index df524d288..c3b66edf6 100644 --- a/app/dl/iter.go +++ b/app/dl/iter.go @@ -6,6 +6,7 @@ import ( "crypto/sha256" "encoding/binary" "fmt" + "github.com/iyear/tdl/pkg/filterMap" "os" "path/filepath" "sort" @@ -74,8 +75,8 @@ func newIter(pool dcpool.Pool, manager *peers.Manager, dialog [][]*tmessage.Dial } // include and exclude - includeMap := filterMap(opts.Include, fsutil.AddPrefixDot) - excludeMap := filterMap(opts.Exclude, fsutil.AddPrefixDot) + includeMap := filterMap.New(opts.Include, fsutil.AddPrefixDot) + excludeMap := filterMap.New(opts.Exclude, fsutil.AddPrefixDot) // to keep fingerprint stable sortDialogs(dialogs, opts.Desc) @@ -287,14 +288,6 @@ func flatDialogs(dialogs [][]*tmessage.Dialog) []*tmessage.Dialog { return res } -func filterMap(data []string, keyFn func(key string) string) map[string]struct{} { - m := make(map[string]struct{}) - for _, v := range data { - m[keyFn(v)] = struct{}{} - } - return m -} - func sortDialogs(dialogs []*tmessage.Dialog, desc bool) { sort.Slice(dialogs, func(i, j int) bool { return tutil.GetInputPeerID(dialogs[i].Peer) < diff --git a/app/up/up.go b/app/up/up.go index 00f2d46f3..2833ec698 100644 --- a/app/up/up.go +++ b/app/up/up.go @@ -25,13 +25,14 @@ import ( type Options struct { Chat string Paths []string + Includes []string Excludes []string Remove bool Photo bool } func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr error) { - files, err := walk(opts.Paths, opts.Excludes) + files, err := walk(opts.Paths, opts.Includes, opts.Excludes) if err != nil { return err } diff --git a/app/up/walk.go b/app/up/walk.go index e7f8a325d..3ee7954fd 100644 --- a/app/up/walk.go +++ b/app/up/walk.go @@ -1,6 +1,7 @@ package up import ( + "github.com/iyear/tdl/pkg/filterMap" "io/fs" "path/filepath" "strings" @@ -9,15 +10,12 @@ import ( "github.com/iyear/tdl/pkg/consts" ) -func walk(paths, excludes []string) ([]*file, error) { +func walk(paths, includes, excludes []string) ([]*file, error) { files := make([]*file, 0) - excludesMap := map[string]struct{}{ - consts.UploadThumbExt: {}, // ignore thumbnail files - } - for _, exclude := range excludes { - excludesMap[exclude] = struct{}{} - } + includesMap := filterMap.New(includes, fsutil.AddPrefixDot) + excludesMap := filterMap.New(excludes, fsutil.AddPrefixDot) + excludesMap[consts.UploadThumbExt] = struct{}{} // ignore thumbnail files for _, path := range paths { err := filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error { @@ -27,7 +25,13 @@ func walk(paths, excludes []string) ([]*file, error) { if d.IsDir() { return nil } - if _, ok := excludesMap[filepath.Ext(path)]; ok { + + // process include and exclude + ext := filepath.Ext(path) + if _, ok := includesMap[ext]; len(includesMap) > 0 && !ok { + return nil + } + if _, ok := excludesMap[ext]; len(excludesMap) > 0 && ok { return nil } diff --git a/cmd/up.go b/cmd/up.go index 2e3aa9149..6a3b3298d 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -26,12 +26,15 @@ func NewUpload() *cobra.Command { } const ( - _chat = "chat" - path = "path" + _chat = "chat" + path = "path" + include = "include" + exclude = "exclude" ) cmd.Flags().StringVarP(&opts.Chat, _chat, "c", "", "chat id or domain, and empty means 'Saved Messages'") cmd.Flags().StringSliceVarP(&opts.Paths, path, "p", []string{}, "dirs or files") - cmd.Flags().StringSliceVarP(&opts.Excludes, "excludes", "e", []string{}, "exclude the specified file extensions") + cmd.Flags().StringSliceVarP(&opts.Includes, include, "i", []string{}, "include the specified file extensions") + cmd.Flags().StringSliceVarP(&opts.Excludes, exclude, "e", []string{}, "exclude the specified file extensions") cmd.Flags().BoolVar(&opts.Remove, "rm", false, "remove the uploaded files after uploading") cmd.Flags().BoolVar(&opts.Photo, "photo", false, "upload the image as a photo instead of a file") diff --git a/docs/content/en/guide/download.md b/docs/content/en/guide/download.md index 2b44a8073..06814d560 100644 --- a/docs/content/en/guide/download.md +++ b/docs/content/en/guide/download.md @@ -117,7 +117,7 @@ Download files with extension filters: {{< hint warning >}} The extension is only matched with the file name, not the MIME type. So it may not work as expected. -Whitelist and blacklist can not be used at the same time. +Whitelist and blacklist can not be used at the same time(if you use both whitelist and blacklist, only the whitelist will take effect.). {{< /hint >}} Whitelist: Only download files with `.jpg` `.png` extension diff --git a/docs/content/en/guide/upload.md b/docs/content/en/guide/upload.md index b89ee8476..2b36bbad3 100644 --- a/docs/content/en/guide/upload.md +++ b/docs/content/en/guide/upload.md @@ -39,6 +39,28 @@ Upload files except specified extensions: tdl up -p /path/to/file -p /path/to/dir -e .so -e .tmp {{< /command >}} +## Filters + +Upload files with extension filters: + +{{< hint warning >}} +The extension is only matched with the file name, not the MIME type. So it may not work as expected. + +Whitelist and blacklist can not be used at the same time(if you use both whitelist and blacklist, only the whitelist will take effect.). +{{< /hint >}} + +Whitelist: Only upload files with `.jpg` `.png` extension + +{{< command >}} +tdl up -p /path/to/file -p /path/to/dir -i jpg,png +{{< /command >}} + +Blacklist: Upload all files except `.mp4` `.flv` extension + +{{< command >}} +tdl up -p /path/to/file -p /path/to/dir -e mp4 -e flv +{{< /command >}} + ## Delete Local Delete the uploaded file after uploading successfully: diff --git a/docs/content/zh/guide/download.md b/docs/content/zh/guide/download.md index 818aa9e85..f31be63ff 100644 --- a/docs/content/zh/guide/download.md +++ b/docs/content/zh/guide/download.md @@ -112,7 +112,7 @@ tdl dl -u https://t.me/tdl/1 --takeout {{< hint warning >}} 扩展名仅与文件名匹配,而不是 MIME 类型。因此,这可能不会按预期工作。 -白名单和黑名单不能同时使用。 +白名单和黑名单不能同时使用(如果同时使用则仅有白名单生效)。 {{< /hint >}} 白名单:只下载扩展名为 `.jpg` `.png` 的文件 diff --git a/docs/content/zh/guide/upload.md b/docs/content/zh/guide/upload.md index 60a2720bb..c92ee32a3 100644 --- a/docs/content/zh/guide/upload.md +++ b/docs/content/zh/guide/upload.md @@ -33,10 +33,24 @@ tdl up -p /path/to/file -t 8 -s 524288 -l 4 ## 过滤器 -上传除指定扩展名之外的文件: +使用扩展名过滤器上传文件: + +{{< hint warning >}} +扩展名仅与文件名匹配,而不是 MIME 类型。因此,这可能不会按预期工作。 + +白名单和黑名单不能同时使用(如果同时使用则仅有白名单生效)。 +{{< /hint >}} + +白名单:只上传扩展名为 `.jpg` `.png` 的文件 + +{{< command >}} +tdl up -p /path/to/file -p /path/to/dir -i jpg,png +{{< /command >}} + +黑名单:上传除了扩展名为 `.mp4` `.flv` 的所有文件 {{< command >}} -tdl up -p /path/to/file -p /path/to/dir -e .so -e .tmp +tdl up -p /path/to/file -p /path/to/dir -e mp4 -e flv {{< /command >}} ## 自动删除 diff --git a/pkg/filterMap/filterMap.go b/pkg/filterMap/filterMap.go new file mode 100644 index 000000000..5400c9e6f --- /dev/null +++ b/pkg/filterMap/filterMap.go @@ -0,0 +1,9 @@ +package filterMap + +func New(data []string, keyFn func(key string) string) map[string]struct{} { + m := make(map[string]struct{}) + for _, v := range data { + m[keyFn(v)] = struct{}{} + } + return m +}