Skip to content

Commit

Permalink
Merge pull request #276 from rusq/i270
Browse files Browse the repository at this point in the history
i270
  • Loading branch information
rusq committed Mar 9, 2024
2 parents fdcd778 + 6ef0ecd commit cf084c0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
19 changes: 18 additions & 1 deletion downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (c *Client) saveFile(ctx context.Context, dir string, sf *slack.File) (int6
if c.fs == nil {
return 0, ErrNoFS
}
if mode := sf.Mode; mode == "hidden_by_limit" || mode == "external" || sf.IsExternal {
if !IsValid(sf) {
trace.Logf(ctx, "info", "file %q is not downloadable", sf.Name)
return 0, nil
}
Expand Down Expand Up @@ -337,3 +337,20 @@ func (c *Client) l() logger.Interface {
}
return c.dlog
}

var invalidModes = map[string]struct{}{
"hidden_by_limit": {},
"external": {},
"tombstone": {},
}

// IsValid returns true if the file can be downloaded and is valid.
func IsValid(f *slack.File) bool {
if f == nil {
return false
}
if _, ok := invalidModes[f.Mode]; ok {
return false
}
return !f.IsExternal && f.Name != ""
}
69 changes: 63 additions & 6 deletions downloader/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ package downloader

import (
"context"
"errors"
"os"
"path"
"path/filepath"
"testing"
"time"

"errors"

"github.com/rusq/slackdump/v2/fsadapter"
"github.com/rusq/slackdump/v2/internal/fixtures"
"github.com/rusq/slackdump/v2/internal/mocks/mock_downloader"
"github.com/slack-go/slack"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"golang.org/x/time/rate"

"github.com/rusq/slackdump/v2/fsadapter"
"github.com/rusq/slackdump/v2/internal/fixtures"
"github.com/rusq/slackdump/v2/internal/mocks/mock_downloader"
)

var (
Expand Down Expand Up @@ -466,3 +464,62 @@ func TestClient_DownloadFile(t *testing.T) {
c.Stop()
})
}

func TestIsValid(t *testing.T) {
type args struct {
f *slack.File
}
tests := []struct {
name string
args args
want bool
}{
{
"valid file",
args{&file1},
true,
},
{
"tombstone",
args{&slack.File{Mode: "tombstone", Name: "foo"}},
false,
},
{
"external file",
args{&slack.File{Mode: "external", Name: "foo", IsExternal: true}},
false,
},
{
"hidden by limit",
args{&slack.File{Mode: "hidden_by_limit", Name: "foo"}},
false,
},
{
"tombstone",
args{&slack.File{Mode: "tombstone", Name: "foo"}},
false,
},
{
"external file",
args{&slack.File{Mode: "", Name: "foo", IsExternal: true}},
false,
},
{
"empty name",
args{&slack.File{Mode: "", Name: "", IsExternal: false}},
false,
},
{
"nil file",
args{nil},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsValid(tt.args.f); got != tt.want {
t.Errorf("IsValid() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit cf084c0

Please sign in to comment.