From c852393504c4d3cabc610fe323f073fd46026946 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 12 Mar 2020 09:01:41 -0400 Subject: [PATCH] Fix download methods --- chat.go | 46 ++++++++++++++++++++++++++++++++++++---------- types.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/chat.go b/chat.go index a5c1971..b5b9edf 100644 --- a/chat.go +++ b/chat.go @@ -583,26 +583,52 @@ func (k *Keybase) UploadToConversation(conv chat1.ConvIDStr, title string, filen return k.SendMessage("attach", opts) } +// Download downloads a file +func (k *Keybase) Download(options DownloadOptions) error { + type res struct { + Error *Error `json:"error"` + } + var r res + + arg := newDownloadArg(options) + + jsonBytes, _ := json.Marshal(arg) + + cmdOut, err := k.Exec("chat", "api", "-m", string(jsonBytes)) + if err != nil { + return err + } + + err = json.Unmarshal(cmdOut, &r) + if err != nil { + return err + } + + if r.Error != nil { + return fmt.Errorf("%v", r.Error.Message) + } + + return nil +} + // DownloadFromChannel downloads a file from a channel -func (k *Keybase) DownloadFromChannel(channel chat1.ChatChannel, msgID chat1.MessageID, filename string) (chat1.SendRes, error) { - opts := SendMessageOptions{ +func (k *Keybase) DownloadFromChannel(channel chat1.ChatChannel, msgID chat1.MessageID, output string) error { + opts := DownloadOptions{ Channel: channel, MessageID: msgID, - Filename: filename, + Output: output, } - - return k.SendMessage("download", opts) + return k.Download(opts) } // DownloadFromConversation downloads a file from a conversation -func (k *Keybase) DownloadFromConversation(conv chat1.ConvIDStr, msgID chat1.MessageID, filename string) (chat1.SendRes, error) { - opts := SendMessageOptions{ +func (k *Keybase) DownloadFromConversation(conv chat1.ConvIDStr, msgID chat1.MessageID, output string) error { + opts := DownloadOptions{ ConversationID: conv, MessageID: msgID, - Filename: filename, + Output: output, } - - return k.SendMessage("download", opts) + return k.Download(opts) } // LoadFlip returns the results of a flip diff --git a/types.go b/types.go index 411683a..840b64c 100644 --- a/types.go +++ b/types.go @@ -149,6 +149,34 @@ func newAdvertiseCommandsArg(options AdvertiseCommandsOptions) advertiseCommands } } +// DownloadOptions holds a set of options to be passed to Download +type DownloadOptions struct { + Channel chat1.ChatChannel + ConversationID chat1.ConvIDStr `json:"conversation_id"` + MessageID chat1.MessageID `json:"message_id"` + Output string + Preview bool + NoStream bool +} + +type downloadParams struct { + Options DownloadOptions +} + +type downloadArg struct { + Method string + Params downloadParams +} + +func newDownloadArg(options DownloadOptions) downloadArg { + return downloadArg{ + Method: "download", + Params: downloadParams{ + Options: options, + }, + } +} + // KVOptions holds a set of options to be passed to the KV methods type KVOptions struct { Team *string `json:"team"`