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)
| 50 | // @Failure 500 {object} responses.InternalServerError |
| 51 | // @Router /v1/attachments/{userID}/{messageID}/{attachmentIndex}/{filename} [get] |
| 52 | func (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 | } |
nothing calls this directly
no test coverage detected