From 8efbcdce61e5a0ab24b141e1101e5d3b76479140 Mon Sep 17 00:00:00 2001 From: iyear Date: Sun, 23 Nov 2025 18:17:11 +0800 Subject: [PATCH 1/6] fix: prevent index out of range panic in chat ls command - Added length check before accessing topics.Messages slice - Prevents panic when topics.Messages is empty (length 0) - Fixes 'index out of range [-1]' error in fetchTopics function The bug occurred at line 286 where topics.Messages[len(topics.Messages)-1] was accessed without checking if the slice was empty first. When the slice is empty, len-1 equals -1, causing a panic. Fixes #1122 --- extension/go.mod | 4 ++-- extension/go.sum | 8 ++++---- go.mod | 7 ++++--- go.sum | 14 ++++++++------ go.work.sum | 2 ++ 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/extension/go.mod b/extension/go.mod index 7f8bddd0a..200e570bc 100644 --- a/extension/go.mod +++ b/extension/go.mod @@ -5,7 +5,7 @@ go 1.23.10 require ( github.com/go-faster/errors v0.7.1 github.com/gotd/td v0.122.0 - github.com/iyear/tdl/core v0.19.1 + github.com/iyear/tdl/core v0.20.0 go.uber.org/zap v1.27.0 ) @@ -38,7 +38,7 @@ require ( golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 // indirect golang.org/x/mod v0.25.0 // indirect golang.org/x/net v0.41.0 // indirect - golang.org/x/sync v0.15.0 // indirect + golang.org/x/sync v0.16.0 // indirect golang.org/x/sys v0.33.0 // indirect golang.org/x/text v0.26.0 // indirect golang.org/x/tools v0.33.0 // indirect diff --git a/extension/go.sum b/extension/go.sum index 85c749db7..cd84550c9 100644 --- a/extension/go.sum +++ b/extension/go.sum @@ -39,8 +39,8 @@ github.com/gotd/td v0.122.0 h1:xIqoYI02ElZjj+KxOfvoUjA63m7MGWZkemM4m42aqRE= github.com/gotd/td v0.122.0/go.mod h1:vPC2X2rcRQYAGVr9EgmQgswHcj8Ps0Tt66XylR3CxrI= github.com/iyear/connectproxy v0.1.1 h1:JZOF/62vvwRGBWcgSyWRb0BpKD4FSs0++B5/y5pNE4c= github.com/iyear/connectproxy v0.1.1/go.mod h1:yD4zOmSMQCmwHIT4fk8mg4k2M15z8VoMSoeY6NNJdsA= -github.com/iyear/tdl/core v0.19.1 h1:xDBrxzPu9PbgcWHYhHdX7y5g4TK/SGcM1H+3e+XszLw= -github.com/iyear/tdl/core v0.19.1/go.mod h1:hMXFF1FJNqX+jFOLpJdYOWXVrk0Bu8kgziZZAgBPt9g= +github.com/iyear/tdl/core v0.20.0 h1:0BmvEaQSCtDi78a1ZvUbtMuN6ZHPeEEY2JVIh8hipZs= +github.com/iyear/tdl/core v0.20.0/go.mod h1:oIMvODKNqz52VmAk3M2+otHTPai4xo3y1aQ2VZE25eY= github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -87,8 +87,8 @@ golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw= golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8= -golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw= +golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= diff --git a/go.mod b/go.mod index b66a9dc8b..6a63abcfa 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/expr-lang/expr v1.17.5 github.com/fatih/color v1.18.0 github.com/flytam/filenamify v1.2.0 - github.com/gabriel-vasile/mimetype v1.4.8 + github.com/gabriel-vasile/mimetype v1.4.11 github.com/go-faster/errors v0.7.1 github.com/go-faster/jx v1.1.0 github.com/go-playground/validator/v10 v10.27.0 @@ -21,8 +21,8 @@ require ( github.com/gotd/td v0.122.0 github.com/iancoleman/strcase v0.3.0 github.com/ivanpirog/coloredcobra v1.0.1 - github.com/iyear/tdl/core v0.19.2 - github.com/iyear/tdl/extension v0.19.2 + github.com/iyear/tdl/core v0.20.0 + github.com/iyear/tdl/extension v0.20.0 github.com/jedib0t/go-pretty/v6 v6.5.0 github.com/klauspost/compress v1.18.0 github.com/kopoli/go-terminal-size v0.0.0-20170219200355-5c97524c8b54 @@ -82,6 +82,7 @@ require ( github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/sagikazarmark/locafero v0.7.0 // indirect + github.com/samber/lo v1.52.0 // indirect github.com/segmentio/asm v1.2.0 // indirect github.com/shoenig/go-m1cpu v0.1.6 // indirect github.com/sourcegraph/conc v0.3.0 // indirect diff --git a/go.sum b/go.sum index f2338a853..c6f308ffa 100644 --- a/go.sum +++ b/go.sum @@ -36,8 +36,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M= github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= -github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM= -github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8= +github.com/gabriel-vasile/mimetype v1.4.11 h1:AQvxbp830wPhHTqc1u7nzoLT+ZFxGY7emj5DR5DYFik= +github.com/gabriel-vasile/mimetype v1.4.11/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-faster/errors v0.7.1 h1:MkJTnDoEdi9pDabt1dpWf7AA8/BaSYZqibYyhZ20AYg= @@ -100,10 +100,10 @@ github.com/ivanpirog/coloredcobra v1.0.1 h1:aURSdEmlR90/tSiWS0dMjdwOvCVUeYLfltLf github.com/ivanpirog/coloredcobra v1.0.1/go.mod h1:iho4nEKcnwZFiniGSdcgdvRgZNjxm+h20acv8vqmN6Q= github.com/iyear/connectproxy v0.1.1 h1:JZOF/62vvwRGBWcgSyWRb0BpKD4FSs0++B5/y5pNE4c= github.com/iyear/connectproxy v0.1.1/go.mod h1:yD4zOmSMQCmwHIT4fk8mg4k2M15z8VoMSoeY6NNJdsA= -github.com/iyear/tdl/core v0.19.2 h1:y9ct3ZhVI8SL+p3sLfzfYfXqc5/87hNTWFPrDqzmT2Y= -github.com/iyear/tdl/core v0.19.2/go.mod h1:4LlXRlcPZ5ZLhFtGM0js83GqAV+4OdCXXBIPCWav4Tk= -github.com/iyear/tdl/extension v0.19.2 h1:1BA2JKY44oBtXrutCN3CPcIFmIT7KaG2xs2MTGjM3lg= -github.com/iyear/tdl/extension v0.19.2/go.mod h1:gPG3ALxRRWdTOQJf76GIsnHu6q4hkbMD4z0sURhJ9p8= +github.com/iyear/tdl/core v0.20.0 h1:0BmvEaQSCtDi78a1ZvUbtMuN6ZHPeEEY2JVIh8hipZs= +github.com/iyear/tdl/core v0.20.0/go.mod h1:oIMvODKNqz52VmAk3M2+otHTPai4xo3y1aQ2VZE25eY= +github.com/iyear/tdl/extension v0.20.0 h1:R3LsUjJwx8x+aE3XvnYA03bCq/FWfI2XXIEQAwHBTBM= +github.com/iyear/tdl/extension v0.20.0/go.mod h1:gPG3ALxRRWdTOQJf76GIsnHu6q4hkbMD4z0sURhJ9p8= github.com/jedib0t/go-pretty/v6 v6.5.0 h1:FI0L5PktzbafnZKuPae/D3150x3XfYbFe2hxMT+TbpA= github.com/jedib0t/go-pretty/v6 v6.5.0/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= @@ -161,6 +161,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sagikazarmark/locafero v0.7.0 h1:5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsFaodPcyo= github.com/sagikazarmark/locafero v0.7.0/go.mod h1:2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k= +github.com/samber/lo v1.52.0 h1:Rvi+3BFHES3A8meP33VPAxiBZX/Aws5RxrschYGjomw= +github.com/samber/lo v1.52.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0= github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys= github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs= github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI= diff --git a/go.work.sum b/go.work.sum index 514a568f6..903c2825a 100644 --- a/go.work.sum +++ b/go.work.sum @@ -115,6 +115,8 @@ github.com/hashicorp/vault/api v1.12.2 h1:7YkCTE5Ni90TcmYHDBExdt4WGJxhpzaHqR6uGb github.com/hashicorp/vault/api v1.12.2/go.mod h1:LSGf1NGT1BnvFFnKVtnvcaLBM2Lz+gJdpL6HUYed8KE= github.com/ianlancetaylor/demangle v0.0.0-20240312041847-bd984b5ce465 h1:KwWnWVWCNtNq/ewIX7HIKnELmEx2nDP42yskD/pi7QE= github.com/ianlancetaylor/demangle v0.0.0-20240312041847-bd984b5ce465/go.mod h1:gx7rwoVhcfuVKG5uya9Hs3Sxj7EIvldVofAWIUtGouw= +github.com/iyear/tdl/core v0.20.0/go.mod h1:oIMvODKNqz52VmAk3M2+otHTPai4xo3y1aQ2VZE25eY= +github.com/iyear/tdl/extension v0.20.0/go.mod h1:gPG3ALxRRWdTOQJf76GIsnHu6q4hkbMD4z0sURhJ9p8= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/k0kubun/pp/v3 v3.4.1 h1:1WdFZDRRqe8UsR61N/2RoOZ3ziTEqgTPVqKrHeb779Y= From fb122e171d738564c10d1f33705f6f78f9b6ffe6 Mon Sep 17 00:00:00 2001 From: Lenny Date: Sun, 14 Dec 2025 19:05:01 +0100 Subject: [PATCH 2/6] feat(utils): add ParseBinaryBytes utility --- pkg/utils/byte.go | 39 ++++++++++++++++++++++++++++++++++++++- pkg/utils/byte_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 pkg/utils/byte_test.go diff --git a/pkg/utils/byte.go b/pkg/utils/byte.go index d1c14c0ad..48eb86ca1 100644 --- a/pkg/utils/byte.go +++ b/pkg/utils/byte.go @@ -1,6 +1,10 @@ package utils -import "fmt" +import ( + "fmt" + "strconv" + "strings" +) type _byte struct{} @@ -21,3 +25,36 @@ func (b _byte) FormatBinaryBytes(n int64) string { } return fmt.Sprintf("%.2f TB", float64(n)/1024/1024/1024/1024) } + +func (b _byte) ParseBinaryBytes(s string) (int64, error) { + if s == "" { + return 0, nil + } + + s = strings.TrimSpace(s) + s = strings.ToUpper(s) + + var multiplier int64 = 1 + if strings.HasSuffix(s, "TB") { + multiplier = 1024 * 1024 * 1024 * 1024 + s = strings.TrimSuffix(s, "TB") + } else if strings.HasSuffix(s, "GB") { + multiplier = 1024 * 1024 * 1024 + s = strings.TrimSuffix(s, "GB") + } else if strings.HasSuffix(s, "MB") { + multiplier = 1024 * 1024 + s = strings.TrimSuffix(s, "MB") + } else if strings.HasSuffix(s, "KB") { + multiplier = 1024 + s = strings.TrimSuffix(s, "KB") + } else if strings.HasSuffix(s, "B") { + s = strings.TrimSuffix(s, "B") + } + + val, err := strconv.ParseFloat(s, 64) + if err != nil { + return 0, err + } + + return int64(val * float64(multiplier)), nil +} diff --git a/pkg/utils/byte_test.go b/pkg/utils/byte_test.go new file mode 100644 index 000000000..8506a711a --- /dev/null +++ b/pkg/utils/byte_test.go @@ -0,0 +1,38 @@ +package utils + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_byte_ParseBinaryBytes(t *testing.T) { + tests := []struct { + name string + s string + want int64 + wantErr bool + }{ + {name: "bytes", s: "100B", want: 100, wantErr: false}, + {name: "bytes lower", s: "100b", want: 100, wantErr: false}, + {name: "kilobytes", s: "1KB", want: 1024, wantErr: false}, + {name: "kilobytes lower", s: "1kb", want: 1024, wantErr: false}, + {name: "megabytes", s: "10MB", want: 10 * 1024 * 1024, wantErr: false}, + {name: "gigabytes", s: "1.5GB", want: int64(1.5 * 1024 * 1024 * 1024), wantErr: false}, + {name: "raw number", s: "100", want: 100, wantErr: false}, + {name: "invalid unit", s: "100ZB", want: 0, wantErr: true}, + {name: "invalid format", s: "abc", want: 0, wantErr: true}, + {name: "empty", s: "", want: 0, wantErr: false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := Byte.ParseBinaryBytes(tt.s) + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tt.want, got) + } + }) + } +} From e5d01233a7afd943ed2e40dc7b8d8df7425e392e Mon Sep 17 00:00:00 2001 From: Lenny Date: Sun, 14 Dec 2025 19:05:05 +0100 Subject: [PATCH 3/6] feat(dl): implement size filtering logic --- app/dl/dl.go | 4 ++++ app/dl/iter.go | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/app/dl/dl.go b/app/dl/dl.go index e603250ea..b50b8d9ed 100644 --- a/app/dl/dl.go +++ b/app/dl/dl.go @@ -45,6 +45,10 @@ type Options struct { // serve Serve bool Port int + + // size filter + MinSize string + MaxSize string } type parser struct { diff --git a/app/dl/iter.go b/app/dl/iter.go index 44503e248..0060d28b1 100644 --- a/app/dl/iter.go +++ b/app/dl/iter.go @@ -53,6 +53,10 @@ type iter struct { opts Options delay time.Duration + // size filter + minSize int64 + maxSize int64 + mu *sync.Mutex finished map[int]struct{} fingerprint string @@ -90,6 +94,16 @@ func newIter(pool dcpool.Pool, manager *peers.Manager, dialog [][]*tmessage.Dial includeMap := filterMap.New(opts.Include, fsutil.AddPrefixDot) excludeMap := filterMap.New(opts.Exclude, fsutil.AddPrefixDot) + minSize, err := utils.Byte.ParseBinaryBytes(opts.MinSize) + if err != nil { + return nil, errors.Wrap(err, "parse min size") + } + + maxSize, err := utils.Byte.ParseBinaryBytes(opts.MaxSize) + if err != nil { + return nil, errors.Wrap(err, "parse max size") + } + // to keep fingerprint stable sortDialogs(dialogs, opts.Desc) @@ -102,6 +116,8 @@ func newIter(pool dcpool.Pool, manager *peers.Manager, dialog [][]*tmessage.Dial exclude: excludeMap, tpl: tpl, delay: delay, + minSize: minSize, + maxSize: maxSize, mu: &sync.Mutex{}, finished: make(map[int]struct{}), @@ -216,6 +232,14 @@ func (i *iter) processSingle(ctx context.Context, message *tg.Message, from peer return false, true } + if i.minSize > 0 && int64(item.Size) < i.minSize { + return false, true + } + + if i.maxSize > 0 && int64(item.Size) > i.maxSize { + return false, true + } + // process include and exclude ext := filepath.Ext(item.Name) if _, ok = i.include[ext]; len(i.include) > 0 && !ok { From 86184f4b2e879c11151ba2bde032f449cd0dab8e Mon Sep 17 00:00:00 2001 From: Lenny Date: Sun, 14 Dec 2025 19:05:08 +0100 Subject: [PATCH 4/6] feat(cmd): add min-size and max-size flags --- cmd/dl.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/dl.go b/cmd/dl.go index 6540ee8e9..01af9b467 100644 --- a/cmd/dl.go +++ b/cmd/dl.go @@ -61,6 +61,9 @@ func NewDownload() *cobra.Command { cmd.Flags().BoolVar(&opts.Takeout, "takeout", false, "takeout sessions let you export data from your account with lower flood wait limits.") cmd.Flags().BoolVar(&opts.Group, "group", false, "auto detect grouped message and download all of them") + cmd.Flags().StringVar(&opts.MinSize, "min-size", "", "min size of file to download. Example: 10MB, 1GB") + cmd.Flags().StringVar(&opts.MaxSize, "max-size", "", "max size of file to download. Example: 10MB, 1GB") + // resume flags, if both false then ask user cmd.Flags().BoolVar(&opts.Continue, _continue, false, "continue the last download directly") cmd.Flags().BoolVar(&opts.Restart, restart, false, "restart the last download directly") From 0d8c0f938b8739a4233060d03e84b2df951d155e Mon Sep 17 00:00:00 2001 From: Lenny Date: Sun, 14 Dec 2025 19:19:21 +0100 Subject: [PATCH 5/6] fix(dl): remove unnecessary type conversion --- app/dl/iter.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/dl/iter.go b/app/dl/iter.go index 0060d28b1..aac3f8306 100644 --- a/app/dl/iter.go +++ b/app/dl/iter.go @@ -232,11 +232,11 @@ func (i *iter) processSingle(ctx context.Context, message *tg.Message, from peer return false, true } - if i.minSize > 0 && int64(item.Size) < i.minSize { + if i.minSize > 0 && item.Size < i.minSize { return false, true } - if i.maxSize > 0 && int64(item.Size) > i.maxSize { + if i.maxSize > 0 && item.Size > i.maxSize { return false, true } From c93234e5d6f6150271fbc5eb155f07eb1182c8c0 Mon Sep 17 00:00:00 2001 From: Lenny Date: Sun, 14 Dec 2025 19:29:06 +0100 Subject: [PATCH 6/6] refactor(dl): extract size filtering logic to shouldSkip and add tests --- app/dl/iter.go | 27 ++++++++++++++++++++++----- app/dl/iter_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/app/dl/iter.go b/app/dl/iter.go index aac3f8306..6e3933266 100644 --- a/app/dl/iter.go +++ b/app/dl/iter.go @@ -232,11 +232,8 @@ func (i *iter) processSingle(ctx context.Context, message *tg.Message, from peer return false, true } - if i.minSize > 0 && item.Size < i.minSize { - return false, true - } - - if i.maxSize > 0 && item.Size > i.maxSize { + // check size + if i.shouldSkip(ctx, item.Size) { return false, true } @@ -451,3 +448,23 @@ func fingerprint(dialogs []*tmessage.Dialog) string { return fmt.Sprintf("%x", sha256.Sum256(buf.Bytes())) } + +func (i *iter) shouldSkip(ctx context.Context, size int64) bool { + if i.minSize > 0 && size < i.minSize { + logctx.From(ctx).Debug("Skip file due to min-size limit", + zap.Int64("size", size), + zap.Int64("min_size", i.minSize), + ) + return true + } + + if i.maxSize > 0 && size > i.maxSize { + logctx.From(ctx).Debug("Skip file due to max-size limit", + zap.Int64("size", size), + zap.Int64("max_size", i.maxSize), + ) + return true + } + + return false +} diff --git a/app/dl/iter_test.go b/app/dl/iter_test.go index 97deadfe1..d340b6cfd 100644 --- a/app/dl/iter_test.go +++ b/app/dl/iter_test.go @@ -229,3 +229,34 @@ func TestIterContextCancellation(t *testing.T) { } }) } + +func TestIterShouldSkip(t *testing.T) { + tests := []struct { + name string + minSize int64 + maxSize int64 + size int64 + want bool + }{ + {name: "no limit", minSize: 0, maxSize: 0, size: 100, want: false}, + {name: "min limit - pass", minSize: 50, maxSize: 0, size: 100, want: false}, + {name: "min limit - skip", minSize: 150, maxSize: 0, size: 100, want: true}, + {name: "max limit - pass", minSize: 0, maxSize: 150, size: 100, want: false}, + {name: "max limit - skip", minSize: 0, maxSize: 50, size: 100, want: true}, + {name: "both limits - pass", minSize: 50, maxSize: 150, size: 100, want: false}, + {name: "both limits - skip min", minSize: 150, maxSize: 200, size: 100, want: true}, + {name: "both limits - skip max", minSize: 50, maxSize: 80, size: 100, want: true}, + {name: "exact min - pass", minSize: 100, maxSize: 0, size: 100, want: false}, + {name: "exact max - pass", minSize: 0, maxSize: 100, size: 100, want: false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + i := &iter{ + minSize: tt.minSize, + maxSize: tt.maxSize, + } + assert.Equal(t, tt.want, i.shouldSkip(context.Background(), tt.size)) + }) + } +}