Skip to content

Commit

Permalink
Add library with greater support for detection of mime types (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
Robi9 authored Apr 8, 2022
1 parent 128692e commit a35b7bc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/structs v1.0.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.0 // indirect
github.com/golang/protobuf v1.3.2 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/evalphobia/logrus_sentry v0.4.6 h1:825MLGu+SW5H8hMXGeBI7TwX7vgJLd9hz0
github.com/evalphobia/logrus_sentry v0.4.6/go.mod h1:pKcp+vriitUqu9KiWj/VRFbRfFNUwz95/UkgG8a6MNc=
github.com/fatih/structs v1.0.0 h1:BrX964Rv5uQ3wwS+KRUAJCBBw5PQmgJfJ6v4yly5QwU=
github.com/fatih/structs v1.0.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/gabriel-vasile/mimetype v1.4.0 h1:Cn9dkdYsMIu56tGho+fqzh7XmvY2YyGU0FnbhiOsEro=
github.com/gabriel-vasile/mimetype v1.4.0/go.mod h1:fA8fi6KUiG7MgQQ+mEWotXoEOvmxRtOJlERCzSmRvr8=
github.com/getsentry/raven-go v0.0.0-20180517221441-ed7bcb39ff10 h1:YO10pIIBftO/kkTFdWhctH96grJ7qiy7bMdiZcIvPKs=
github.com/getsentry/raven-go v0.0.0-20180517221441-ed7bcb39ff10/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec=
Expand Down Expand Up @@ -114,6 +116,7 @@ golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
26 changes: 17 additions & 9 deletions handlers/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/buger/jsonparser"
"github.com/gabriel-vasile/mimetype"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/backends/rapidpro"
"github.com/nyaruka/courier/handlers"
Expand Down Expand Up @@ -403,10 +404,10 @@ var waIgnoreStatuses = map[string]bool{
// }

type mtTextPayload struct {
To string `json:"to" validate:"required"`
Type string `json:"type" validate:"required"`
PreviewURL bool `json:"preview_url,omitempty"`
Text struct {
To string `json:"to" validate:"required"`
Type string `json:"type" validate:"required"`
PreviewURL bool `json:"preview_url,omitempty"`
Text struct {
Body string `json:"body" validate:"required"`
} `json:"text"`
}
Expand Down Expand Up @@ -795,14 +796,14 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
var payload mtTextPayload
if strings.Contains(part, "https://") || strings.Contains(part, "http://") {
payload = mtTextPayload{
To: msg.URN().Path(),
Type: "text",
To: msg.URN().Path(),
Type: "text",
PreviewURL: true,
}
} else {
payload = mtTextPayload{
To: msg.URN().Path(),
Type: "text",
To: msg.URN().Path(),
Type: "text",
}
}
payload.Text.Body = part
Expand Down Expand Up @@ -934,7 +935,14 @@ func (h *handler) fetchMediaID(msg courier.Msg, mimeType, mediaURL string) (stri
return "", logs, errors.Wrapf(err, "error building request to media endpoint")
}
setWhatsAppAuthHeader(&req.Header, msg.Channel())
req.Header.Add("Content-Type", http.DetectContentType(rr.Body))
mtype := http.DetectContentType(rr.Body)

if mtype != mimeType || mtype == "application/octet-stream" || mtype == "application/zip" {
mimeT := mimetype.Detect(rr.Body)
req.Header.Add("Content-Type", mimeT.String())
} else {
req.Header.Add("Content-Type", mtype)
}
rr, err = utils.MakeHTTPRequest(req)
log = courier.NewChannelLogFromRR("Uploading media to WhatsApp", msg.Channel(), msg.ID(), rr).WithError("Error uploading media to WhatsApp", err)
logs = append(logs, log)
Expand Down

0 comments on commit a35b7bc

Please sign in to comment.