Skip to content

Commit

Permalink
Merge pull request rapidpro#356 from Ilhasoft/fix/excel-files-sent-as…
Browse files Browse the repository at this point in the history
…-zip

Add Filename
  • Loading branch information
nicpottier authored May 18, 2021
2 parents 2e2de31 + 9d1414a commit 7ebe378
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
14 changes: 11 additions & 3 deletions handlers/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ type eventPayload struct {
MimeType string `json:"mime_type" validate:"required"`
Sha256 string `json:"sha256" validate:"required"`
Caption string `json:"caption"`
Filename string `json:"filename"`
} `json:"document"`
Image *struct {
File string `json:"file" validate:"required"`
Expand Down Expand Up @@ -342,9 +343,10 @@ type mtTextPayload struct {
}

type mediaObject struct {
ID string `json:"id,omitempty"`
Link string `json:"link,omitempty"`
Caption string `json:"caption,omitempty"`
ID string `json:"id,omitempty"`
Link string `json:"link,omitempty"`
Caption string `json:"caption,omitempty"`
Filename string `json:"filename,omitempty"`
}

type LocalizableParam struct {
Expand Down Expand Up @@ -473,6 +475,12 @@ func (h *handler) SendMsg(ctx context.Context, msg courier.Msg) (courier.MsgStat
if attachmentCount == 0 {
mediaPayload.Caption = msg.Text()
}
mediaPayload.Filename, err = utils.BasePathForURL(mediaURL)

// Logging error
if err != nil {
logrus.WithField("channel_uuid", msg.Channel().UUID().String()).WithError(err).Error("Error while parsing the media URL")
}
payload.Document = mediaPayload
wppID, externalID, logs, err = sendWhatsAppMsg(msg, sendPath, payload)
} else if strings.HasPrefix(mimeType, "image") {
Expand Down
5 changes: 3 additions & 2 deletions handlers/whatsapp/whatsapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ var documentMsg = `{
"link": "https://example.org/v1/media/41",
"mime_type": "text/plain",
"sha256": "the-sha-signature",
"caption": "the caption"
"caption": "the caption",
"filename": "filename.type"
}
}]
}`
Expand Down Expand Up @@ -389,7 +390,7 @@ var defaultSendTestCases = []ChannelSendTestCase{
MockedRequest{
Method: "POST",
Path: "/v1/messages",
Body: `{"to":"250788123123","type":"document","document":{"link":"https://foo.bar/document.pdf","caption":"document caption"}}`,
Body: `{"to":"250788123123","type":"document","document":{"link":"https://foo.bar/document.pdf","caption":"document caption","filename":"document.pdf"}}`,
}: MockedResponse{
Status: 201,
Body: `{ "messages": [{"id": "157b5e14568e8"}] }`,
Expand Down
11 changes: 11 additions & 0 deletions utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net/url"
"path"
"regexp"
"strings"
"unicode/utf8"
Expand Down Expand Up @@ -98,3 +100,12 @@ func CleanString(s string) string {

return cleaned
}

// BasePathForURL, parse static URL, and return filename + format
func BasePathForURL(rawURL string) (string, error) {
parsedURL, err := url.Parse(rawURL)
if err != nil {
return rawURL, err
}
return path.Base(parsedURL.Path), nil
}
10 changes: 10 additions & 0 deletions utils/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ func TestCleanString(t *testing.T) {
text, _ := url.PathUnescape("hi%1C%00%00%00%00%00%07%E0%00")
assert.Equal(t, "hi\x1c\a", CleanString(text))
}

func TestURLGetFile(t *testing.T) {
test1, err := BasePathForURL("https://example.com/test.pdf")
assert.Equal(t, nil, err)
assert.Equal(t, "test.pdf", test1)

test2, err := BasePathForURL("application/pdf:https://some-url.host.service.com/media/999/zz99/9999/da514731-4bed-428c-afb9-860dd94530cc.xlsx")
assert.Equal(t, nil, err)
assert.Equal(t, "da514731-4bed-428c-afb9-860dd94530cc.xlsx", test2)
}

0 comments on commit 7ebe378

Please sign in to comment.