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

feat(upload): support --include flag #566

Open
wants to merge 1 commit 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 @@ -68,8 +69,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.New(opts.Include, utils.FS.AddPrefixDot)
excludeMap := filterMap.New(opts.Exclude, utils.FS.AddPrefixDot)

// to keep fingerprint stable
sortDialogs(dialogs, opts.Desc)
Expand Down Expand Up @@ -274,14 +275,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 utils.Telegram.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 @@ -24,13 +24,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
21 changes: 13 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,13 @@ import (
"github.com/iyear/tdl/pkg/utils"
)

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{}{}
}
// include and exclude
includesMap := filterMap.New(includes, utils.FS.AddPrefixDot)
excludesMap := filterMap.New(excludes, utils.FS.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 +26,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
1 change: 1 addition & 0 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func NewUpload() *cobra.Command {
)
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.Includes, "includes", "i", []string{}, "include the specified file extensions")
cmd.Flags().StringSliceVarP(&opts.Excludes, "excludes", "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
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
}