Skip to content

Commit a0fc868

Browse files
Added attachment file type check to CreateRequest
1 parent 569b56d commit a0fc868

1 file changed

Lines changed: 37 additions & 3 deletions

File tree

api/pkg/handlers/discord_handler.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"encoding/json"
99
"fmt"
1010
"os"
11+
"strings"
1112

1213
"github.com/google/uuid"
1314

15+
"github.com/NdoleStudio/httpsms/pkg/entities"
1416
"github.com/NdoleStudio/httpsms/pkg/repositories"
1517
"github.com/NdoleStudio/httpsms/pkg/requests"
1618
"github.com/NdoleStudio/httpsms/pkg/services"
@@ -290,10 +292,42 @@ func (h *DiscordHandler) createRequest(payload map[string]any) requests.MessageS
290292
}
291293
return ""
292294
}
295+
var attachments []entities.MessageAttachment
296+
attachmentURLsStr := getOption("attachment_urls")
297+
298+
if attachmentURLsStr != "" {
299+
urls := strings.Split(attachmentURLsStr, ",")
300+
for _, u := range urls {
301+
cleanURL := strings.TrimSpace(u)
302+
if cleanURL == "" {
303+
continue
304+
}
305+
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+
}
319+
320+
attachments = append(attachments, entities.MessageAttachment{
321+
ContentType: contentType,
322+
URL: cleanURL,
323+
})
324+
}
325+
}
293326
return requests.MessageSend{
294-
From: getOption("from"),
295-
To: getOption("to"),
296-
Content: getOption("message"),
327+
From: getOption("from"),
328+
To: getOption("to"),
329+
Content: getOption("message"),
330+
Attachments: attachments,
297331
}
298332
}
299333

0 commit comments

Comments
 (0)