Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RE: feat(upload): support --include flag #650

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions app/dl/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/sha256"
"encoding/binary"
"fmt"
"github.com/iyear/tdl/pkg/filterMap"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) <
Expand Down
3 changes: 2 additions & 1 deletion app/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
20 changes: 12 additions & 8 deletions app/up/walk.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package up

import (
"github.com/iyear/tdl/pkg/filterMap"
"io/fs"
"path/filepath"
"strings"
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
9 changes: 6 additions & 3 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/guide/download.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions docs/content/en/guide/upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs/content/zh/guide/download.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ tdl dl -u https://t.me/tdl/1 --takeout
{{< hint warning >}}
扩展名仅与文件名匹配,而不是 MIME 类型。因此,这可能不会按预期工作。

白名单和黑名单不能同时使用。
白名单和黑名单不能同时使用(如果同时使用则仅有白名单生效)
{{< /hint >}}

白名单:只下载扩展名为 `.jpg` `.png` 的文件
Expand Down
18 changes: 16 additions & 2 deletions docs/content/zh/guide/upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 >}}

## 自动删除
Expand Down
9 changes: 9 additions & 0 deletions pkg/filterMap/filterMap.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading