Skip to content

Commit

Permalink
Merge pull request #453 from rusq/i452
Browse files Browse the repository at this point in the history
download gifs as gifs
  • Loading branch information
rusq authored Feb 21, 2025
2 parents 59f36dd + 0bb9775 commit a133bc5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/emoji/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var CmdEmoji = &base.Command{
UsageLine: "slackdump emoji [flags]",
Short: "download workspace emoticons ಠ_ಠ",
Long: emojiMD,
FlagMask: cfg.OmitAll &^ cfg.OmitAuthFlags,
FlagMask: cfg.OmitAll &^ cfg.OmitAuthFlags &^ cfg.OmitOutputFlag,
RequireAuth: true,
PrintFlags: true,
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/slackdump/internal/emoji/emojidl/emoedge.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package emojidl provides functions to dump the all slack emojis for a workspace.
// It skips the "alias" emojis, so only original an emoji with an original name
// is present. If you need to find the alias - lookup the index.json. The
// directory structure is the following:
// Package emojidl provides functions to dump the all slack emojis for a
// workspace. It skips the "alias" emojis, so only original emoji with an
// original name is present. If you need to find the alias - look it up in the
// index.json. The directory structure is the following:
//
// .
// +- emojis
Expand All @@ -24,6 +24,7 @@ import (
"sync"

"github.com/rusq/fsadapter"

"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/internal/edge"
)
Expand Down Expand Up @@ -97,7 +98,7 @@ func DlEdgeFS(ctx context.Context, sess EdgeEmojiLister, fsa fsadapter.FS, failF
count = 0
total = <-totalC // if there's a generator error, this will receive 0.
)
var emojis = make(map[string]edge.Emoji, total)
emojis := make(map[string]edge.Emoji, total)
LOOP:
for {
select {
Expand Down
6 changes: 4 additions & 2 deletions cmd/slackdump/internal/emoji/emojidl/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sync"

"github.com/rusq/fsadapter"

"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/internal/edge"
)
Expand Down Expand Up @@ -54,7 +55,7 @@ func DlFS(ctx context.Context, sess EmojiDumper, fsa fsadapter.FS, failFast bool
if err != nil {
return fmt.Errorf("error marshalling emoji index: %w", err)
}
if err := fsa.WriteFile("index.json", bIndex, 0644); err != nil {
if err := fsa.WriteFile("index.json", bIndex, 0o644); err != nil {
return fmt.Errorf("failed writing emoji index: %w", err)
}

Expand Down Expand Up @@ -164,7 +165,8 @@ func fetchEmoji(ctx context.Context, fsa fsadapter.FS, dir string, name, uri str
}
defer resp.Body.Close()

filename := path.Join(dir, name+".png")
emojiExt := path.Ext(uri) // get the extension from the uri.
filename := path.Join(dir, name+emojiExt)
wc, err := fsa.Create(filename)
if err != nil {
return err
Expand Down
25 changes: 18 additions & 7 deletions cmd/slackdump/internal/emoji/emojidl/emoji_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
"reflect"
"sync"
Expand Down Expand Up @@ -40,9 +41,10 @@ func setGlobalFetchFn(fn fetchFunc) {

func Test_fetchEmoji(t *testing.T) {
type args struct {
ctx context.Context
dir string
name string
ctx context.Context
dir string
name string
urlsuffix string
}
type serverOptions struct {
status int
Expand All @@ -58,15 +60,23 @@ func Test_fetchEmoji(t *testing.T) {
}{
{
"ok",
args{context.Background(), "test", "file"},
args{context.Background(), "test", "file", "/somepath/file.png"},
serverOptions{status: http.StatusOK, body: []byte("test data")},
false,
true,
[]byte("test data"),
},
{
"gif",
args{context.Background(), "test", "file", "/somepath/file.gif"},
serverOptions{status: http.StatusOK, body: []byte("test data")},
false,
true,
[]byte("test data"),
},
{
"404",
args{context.Background(), "test", "file"},
args{context.Background(), "test", "file", "/somepath/file.png"},
serverOptions{status: http.StatusNotFound, body: nil},
true,
true,
Expand All @@ -89,11 +99,12 @@ func Test_fetchEmoji(t *testing.T) {
t.Fatalf("failed to create test dir: %s", err)
}

if err := fetchEmoji(tt.args.ctx, fsa, tt.args.dir, tt.args.name, server.URL); (err != nil) != tt.wantErr {
if err := fetchEmoji(tt.args.ctx, fsa, tt.args.dir, tt.args.name, server.URL+tt.args.urlsuffix); (err != nil) != tt.wantErr {
t.Errorf("fetch() error = %v, wantErr %v", err, tt.wantErr)
}

testfile := filepath.Join(dir, tt.args.dir, tt.args.name+".png")
ext := path.Ext(tt.args.urlsuffix)
testfile := filepath.Join(dir, tt.args.dir, tt.args.name+ext)
_, err = os.Stat(testfile)
if notExist := os.IsNotExist(err); notExist != !tt.wantFileExist {
t.Errorf("os.IsNotExist=%v tt.wantFileExist=%v", notExist, tt.wantFileExist)
Expand Down

0 comments on commit a133bc5

Please sign in to comment.