-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathattachments.go
149 lines (125 loc) · 4.11 KB
/
attachments.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package courier
import (
"context"
"encoding/json"
"io"
"log/slog"
"mime"
"net/http"
"net/url"
"path/filepath"
"github.com/h2non/filetype"
"github.com/nyaruka/courier/utils"
"github.com/nyaruka/gocommon/httpx"
"github.com/pkg/errors"
)
const (
maxAttBodyReadBytes = 100 * 1024 * 1024
)
type Attachment struct {
ContentType string `json:"content_type"`
URL string `json:"url"`
Size int `json:"size"`
}
type fetchAttachmentRequest struct {
ChannelType ChannelType `json:"channel_type" validate:"required"`
ChannelUUID ChannelUUID `json:"channel_uuid" validate:"required,uuid"`
URL string `json:"url" validate:"required"`
MsgID MsgID `json:"msg_id"`
}
type fetchAttachmentResponse struct {
Attachment *Attachment `json:"attachment"`
LogUUID ChannelLogUUID `json:"log_uuid"`
}
func fetchAttachment(ctx context.Context, b Backend, r *http.Request) (*fetchAttachmentResponse, error) {
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, errors.Wrap(err, "error reading request body")
}
fa := &fetchAttachmentRequest{}
if err := json.Unmarshal(body, fa); err != nil {
return nil, errors.Wrap(err, "error unmarshalling request")
}
if err := utils.Validate(fa); err != nil {
return nil, err
}
ch, err := b.GetChannel(ctx, fa.ChannelType, fa.ChannelUUID)
if err != nil {
return nil, errors.Wrap(err, "error getting channel")
}
clog := NewChannelLogForAttachmentFetch(ch, GetHandler(ch.ChannelType()).RedactValues(ch))
attachment, err := FetchAndStoreAttachment(ctx, b, ch, fa.URL, clog)
// try to write channel log even if we have an error
clog.End()
if err := b.WriteChannelLog(ctx, clog); err != nil {
slog.Error("error writing log", "error", err)
}
if err != nil {
return nil, err
}
return &fetchAttachmentResponse{Attachment: attachment, LogUUID: clog.UUID()}, nil
}
func FetchAndStoreAttachment(ctx context.Context, b Backend, channel Channel, attURL string, clog *ChannelLog) (*Attachment, error) {
parsedURL, err := url.Parse(attURL)
if err != nil {
return nil, err
}
var attRequest *http.Request
handler := GetHandler(channel.ChannelType())
builder, isBuilder := handler.(AttachmentRequestBuilder)
if isBuilder {
attRequest, err = builder.BuildAttachmentRequest(ctx, b, channel, parsedURL.String(), clog)
} else {
attRequest, err = http.NewRequest(http.MethodGet, attURL, nil)
}
if err != nil {
return nil, errors.Wrap(err, "unable to create attachment request")
}
trace, err := httpx.DoTrace(b.HttpClient(true), attRequest, nil, b.HttpAccess(), maxAttBodyReadBytes)
if trace != nil {
clog.HTTP(trace)
// if we got a non-200 response, return the attachment with a pseudo content type which tells the caller
// to continue without the attachment
if trace.Response == nil || trace.Response.StatusCode/100 != 2 || err == httpx.ErrResponseSize || err == httpx.ErrAccessConfig {
return &Attachment{ContentType: "unavailable", URL: attURL}, nil
}
}
if err != nil {
return nil, err
}
mimeType := ""
extension := filepath.Ext(parsedURL.Path)
if extension != "" {
extension = extension[1:]
}
// first try getting our mime type from the first 300 bytes of our body
fileType, _ := filetype.Match(trace.ResponseBody[:300])
if fileType != filetype.Unknown {
mimeType = fileType.MIME.Value
extension = fileType.Extension
} else {
// if that didn't work, try from our extension
fileType = filetype.GetType(extension)
if fileType != filetype.Unknown {
mimeType = fileType.MIME.Value
extension = fileType.Extension
}
}
// we still don't know our mime type, use our content header instead
if mimeType == "" {
mimeType, _, _ = mime.ParseMediaType(trace.Response.Header.Get("Content-Type"))
if extension == "" {
extensions, err := mime.ExtensionsByType(mimeType)
if extensions == nil || err != nil {
extension = ""
} else {
extension = extensions[0][1:]
}
}
}
storageURL, err := b.SaveAttachment(ctx, channel, mimeType, trace.ResponseBody, extension)
if err != nil {
return nil, err
}
return &Attachment{ContentType: mimeType, URL: storageURL, Size: len(trace.ResponseBody)}, nil
}