forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attachments.go
167 lines (137 loc) · 4.67 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package courier
import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"mime"
"net/http"
"net/url"
"path/filepath"
"strings"
"github.com/h2non/filetype"
"github.com/nyaruka/courier/utils"
"github.com/nyaruka/courier/utils/clogs"
"github.com/nyaruka/gocommon/httpx"
)
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 clogs.LogUUID `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, fmt.Errorf("error reading request body: %w", err)
}
fa := &fetchAttachmentRequest{}
if err := json.Unmarshal(body, fa); err != nil {
return nil, fmt.Errorf("error unmarshalling request: %w", err)
}
if err := utils.Validate(fa); err != nil {
return nil, err
}
ch, err := b.GetChannel(ctx, fa.ChannelType, fa.ChannelUUID)
if err != nil {
return nil, fmt.Errorf("error getting channel: %w", err)
}
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, fmt.Errorf("error fetching attachment for msg #%d: %w", fa.MsgID, 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, fmt.Errorf("unable to parse attachment url '%s': %w", attURL, 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, fmt.Errorf("unable to create attachment request: %w", err)
}
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 := getAttachmentType(trace)
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
}
func getAttachmentType(t *httpx.Trace) (string, string) {
var typ string
// use extension from url path if it exists
ext := filepath.Ext(t.Request.URL.Path)
// prioritize to use the response content type header if provided
contentTypeHeader := t.Response.Header.Get("Content-Type")
if contentTypeHeader != "" {
typ, _, _ = mime.ParseMediaType(contentTypeHeader)
}
// if we didn't get a meaningful content type from the header, try to guess it from the body
if typ == "" || typ == "*/*" || typ == "application/octet-stream" {
fileType, _ := filetype.Match(t.ResponseBody[:300])
if fileType != filetype.Unknown {
typ = fileType.MIME.Value
if ext == "" {
ext = fileType.Extension
}
}
}
// if we still don't have a type but the path has an extension, try to use that
if typ == "" && ext != "" {
fileType := filetype.GetType(ext)
if fileType != filetype.Unknown {
typ = fileType.MIME.Value
}
}
// if we have a type but no extension, try to get one from the type
if ext == "" {
extensions, err := mime.ExtensionsByType(typ)
if len(extensions) > 0 && err == nil {
ext = extensions[0][1:]
}
}
// got to default to something...
if typ == "" {
typ = "application/octet-stream"
}
return typ, strings.TrimPrefix(ext, ".")
}