Skip to content

Commit 3201494

Browse files
AchoArnoldCopilot
andcommitted
fix: address code review findings for MMS attachments
- Use deterministic reverse map for ContentTypeFromExtension (I-2) - Initialize attachmentURLs to []string{} to avoid null in JSON (I-3) - Distinguish 404 vs 500 in download handler with ErrAttachmentNotFound (I-4) - Remove unused stacktrace import from memory storage Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 082eebf commit 3201494

4 files changed

Lines changed: 28 additions & 8 deletions

File tree

api/pkg/handlers/attachment_handler.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package handlers
22

33
import (
4+
"errors"
45
"fmt"
56
"path/filepath"
67

@@ -68,7 +69,10 @@ func (h *AttachmentHandler) GetAttachment(c *fiber.Ctx) error {
6869
if err != nil {
6970
msg := fmt.Sprintf("cannot download attachment from path [%s]", path)
7071
ctxLogger.Warn(stacktrace.Propagate(err, msg))
71-
return h.responseNotFound(c, "attachment not found")
72+
if errors.Is(err, repositories.ErrAttachmentNotFound) {
73+
return h.responseNotFound(c, "attachment not found")
74+
}
75+
return h.responseInternalServerError(c)
7276
}
7377

7478
ext := filepath.Ext(filename)

api/pkg/repositories/attachment_storage.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ var contentTypeExtensions = map[string]string{
3434
"text/x-vcard": ".vcf",
3535
}
3636

37+
// extensionContentTypes is the reverse map from file extensions to canonical MIME types
38+
var extensionContentTypes = map[string]string{
39+
".jpg": "image/jpeg",
40+
".png": "image/png",
41+
".gif": "image/gif",
42+
".webp": "image/webp",
43+
".bmp": "image/bmp",
44+
".mp4": "video/mp4",
45+
".3gp": "video/3gpp",
46+
".mp3": "audio/mpeg",
47+
".ogg": "audio/ogg",
48+
".amr": "audio/amr",
49+
".pdf": "application/pdf",
50+
".vcf": "text/vcard",
51+
}
52+
3753
// AllowedContentTypes returns the set of allowed MIME types for attachments
3854
func AllowedContentTypes() map[string]bool {
3955
allowed := make(map[string]bool, len(contentTypeExtensions))
@@ -55,14 +71,15 @@ func ExtensionFromContentType(contentType string) string {
5571
// ContentTypeFromExtension returns the MIME content type for a file extension.
5672
// Returns "application/octet-stream" if the extension is not recognized.
5773
func ContentTypeFromExtension(ext string) string {
58-
for ct, e := range contentTypeExtensions {
59-
if e == ext {
60-
return ct
61-
}
74+
if ct, ok := extensionContentTypes[ext]; ok {
75+
return ct
6276
}
6377
return "application/octet-stream"
6478
}
6579

80+
// ErrAttachmentNotFound is returned when an attachment is not found in storage
81+
var ErrAttachmentNotFound = fmt.Errorf("attachment not found")
82+
6683
// SanitizeFilename removes path separators and traversal sequences from a filename.
6784
// Returns "attachment-{index}" if the sanitized name is empty.
6885
func SanitizeFilename(name string, index int) string {

api/pkg/repositories/memory_attachment_storage.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"sync"
77

88
"github.com/NdoleStudio/httpsms/pkg/telemetry"
9-
"github.com/palantir/stacktrace"
109
)
1110

1211
// MemoryAttachmentStorage stores attachments in memory
@@ -44,7 +43,7 @@ func (s *MemoryAttachmentStorage) Download(ctx context.Context, path string) ([]
4443

4544
value, ok := s.data.Load(path)
4645
if !ok {
47-
return nil, stacktrace.NewError(fmt.Sprintf("attachment not found at path [%s]", path))
46+
return nil, ErrAttachmentNotFound
4847
}
4948
return value.([]byte), nil
5049
}

api/pkg/services/message_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func (service *MessageService) ReceiveMessage(ctx context.Context, params *Messa
323323
ctxLogger := service.tracer.CtxLogger(service.logger, span)
324324

325325
messageID := uuid.New()
326-
var attachmentURLs []string
326+
attachmentURLs := []string{}
327327

328328
if len(params.Attachments) > 0 {
329329
ctxLogger.Info(fmt.Sprintf("uploading [%d] attachments for message [%s]", len(params.Attachments), messageID))

0 commit comments

Comments
 (0)