Skip to content

Commit c4f7e18

Browse files
Centralised the content type check into message entities file
1 parent 182d860 commit c4f7e18

3 files changed

Lines changed: 19 additions & 25 deletions

File tree

api/pkg/entities/message.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package entities
22

33
import (
4+
"strings"
45
"time"
56

67
"github.com/google/uuid"
@@ -233,3 +234,19 @@ func (message *Message) updateOrderTimestamp(timestamp time.Time) {
233234
message.OrderTimestamp = timestamp
234235
}
235236
}
237+
238+
func GetAttachmentContentType(url string) string {
239+
// Since there's no easy way to set a type in the CSV, defaulting to octet-stream and then just checking the file extension in the URL
240+
contentType := "application/octet-stream"
241+
lowerURL := strings.ToLower(url)
242+
if strings.HasSuffix(lowerURL, ".jpg") || strings.HasSuffix(lowerURL, ".jpeg") {
243+
contentType = "image/jpeg"
244+
} else if strings.HasSuffix(lowerURL, ".png") {
245+
contentType = "image/png"
246+
} else if strings.HasSuffix(lowerURL, ".gif") {
247+
contentType = "image/gif"
248+
} else if strings.HasSuffix(lowerURL, ".mp4") {
249+
contentType = "video/mp4"
250+
}
251+
return contentType
252+
}

api/pkg/handlers/discord_handler.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -303,19 +303,7 @@ func (h *DiscordHandler) createRequest(payload map[string]any) requests.MessageS
303303
continue
304304
}
305305

306-
// Same as with bulk CSV attachments, can't easily ask for the MIME type so
307-
// just inferring based on the file extension
308-
contentType := "application/octet-stream"
309-
lowerURL := strings.ToLower(cleanURL)
310-
if strings.HasSuffix(lowerURL, ".jpg") || strings.HasSuffix(lowerURL, ".jpeg") {
311-
contentType = "image/jpeg"
312-
} else if strings.HasSuffix(lowerURL, ".png") {
313-
contentType = "image/png"
314-
} else if strings.HasSuffix(lowerURL, ".gif") {
315-
contentType = "image/gif"
316-
} else if strings.HasSuffix(lowerURL, ".mp4") {
317-
contentType = "video/mp4"
318-
}
306+
contentType := entities.GetAttachmentContentType(cleanURL)
319307

320308
attachments = append(attachments, entities.MessageAttachment{
321309
ContentType: contentType,

api/pkg/requests/bulk_message_request.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,7 @@ func (input *BulkMessage) ToMessageSendParams(userID entities.UserID, requestID
4343
continue
4444
}
4545

46-
// Since there's no easy way to set a type in the CSV, defaulting to octet-stream and then just checking the file extension in the URL
47-
contentType := "application/octet-stream"
48-
lowerURL := strings.ToLower(cleanURL)
49-
if strings.HasSuffix(lowerURL, ".jpg") || strings.HasSuffix(lowerURL, ".jpeg") {
50-
contentType = "image/jpeg"
51-
} else if strings.HasSuffix(lowerURL, ".png") {
52-
contentType = "image/png"
53-
} else if strings.HasSuffix(lowerURL, ".gif") {
54-
contentType = "image/gif"
55-
} else if strings.HasSuffix(lowerURL, ".mp4") {
56-
contentType = "video/mp4"
57-
}
46+
contentType := entities.GetAttachmentContentType(cleanURL)
5847

5948
attachments = append(attachments, entities.MessageAttachment{
6049
ContentType: contentType,

0 commit comments

Comments
 (0)