MCPcopy Index your code
hub / github.com/NdoleStudio/httpsms / GetAttachment

Method GetAttachment

api/pkg/handlers/attachment_handler.go:52–85  ·  view source on GitHub ↗

GetAttachment Downloads an attachment @Summary Download a message attachment @Description Download an MMS attachment by its path components @Tags Attachments @Produce application/octet-stream @Param userID path string true "User ID" @Param messageID

(c fiber.Ctx)

Source from the content-addressed store, hash-verified

50// @Failure 500 {object} responses.InternalServerError
51// @Router /v1/attachments/{userID}/{messageID}/{attachmentIndex}/{filename} [get]
52func (h *AttachmentHandler) GetAttachment(c fiber.Ctx) error {
53 ctx, span := h.tracer.StartFromFiberCtx(c)
54 defer span.End()
55
56 ctxLogger := h.tracer.CtxLogger(h.logger, span)
57
58 userID := c.Params("userID")
59 messageID := c.Params("messageID")
60 attachmentIndex := c.Params("attachmentIndex")
61 filename := c.Params("filename")
62
63 path := fmt.Sprintf("attachments/%s/%s/%s/%s", userID, messageID, attachmentIndex, filename)
64
65 ctxLogger.Info(fmt.Sprintf("downloading attachment from path [%s]", path))
66
67 data, err := h.storage.Download(ctx, path)
68 if err != nil {
69 msg := fmt.Sprintf("cannot download attachment from path [%s]", path)
70 ctxLogger.Warn(stacktrace.Propagate(err, msg))
71 if stacktrace.GetCode(err) == repositories.ErrCodeNotFound {
72 return h.responseNotFound(c, "attachment not found")
73 }
74 return h.responseInternalServerError(c)
75 }
76
77 ext := filepath.Ext(filename)
78 contentType := repositories.ContentTypeFromExtension(ext)
79
80 c.Set("Content-Type", contentType)
81 c.Set("Content-Disposition", "attachment")
82 c.Set("X-Content-Type-Options", "nosniff")
83
84 return c.Send(data)
85}

Callers

nothing calls this directly

Calls 9

responseNotFoundMethod · 0.80
StartFromFiberCtxMethod · 0.65
CtxLoggerMethod · 0.65
InfoMethod · 0.65
DownloadMethod · 0.65
WarnMethod · 0.65
SetMethod · 0.65
SendMethod · 0.65

Tested by

no test coverage detected