forked from mtusi2026/mtusi2026lab6
112 lines
3.6 KiB
Go
112 lines
3.6 KiB
Go
package http
|
|
|
|
import (
|
|
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
|
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
|
"gitea.privcloud.tech/mtusi2026/internal/pkg/auth/jwtprovider"
|
|
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/framework"
|
|
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/router"
|
|
"gitea.privcloud.tech/mtusi2026/internal/service/attachment"
|
|
"io"
|
|
"mime"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type AttachmentHandler struct {
|
|
router router.Router
|
|
provider jwtprovider.Provider
|
|
svc *attachment.Service
|
|
}
|
|
|
|
func NewAttachmentHandler(r router.Router, p jwtprovider.Provider, s *attachment.Service) Handler {
|
|
return &AttachmentHandler{r, p, s}
|
|
}
|
|
func (h *AttachmentHandler) RegisterRoutes() {
|
|
h.router.Private("POST", "/api/v1/chats/{chat_id}/attachments", h.Upload)
|
|
h.router.Private("GET", "/api/v1/attachments/{attachment_id}", h.Get)
|
|
h.router.Private("GET", "/api/v1/attachments/{attachment_id}/content", h.Get)
|
|
}
|
|
func (h *AttachmentHandler) Upload(w http.ResponseWriter, r *http.Request) {
|
|
user, e := framework.ParseToken(r, h.provider)
|
|
if e != nil {
|
|
framework.JSONError(r.Context(), w, e)
|
|
return
|
|
}
|
|
id, e := framework.ParseIDFromPathParam(r.Context(), w, r, "chat_id")
|
|
if e != nil {
|
|
return
|
|
}
|
|
values := r.URL.Query()["filename"]
|
|
if len(values) != 1 {
|
|
framework.JSONError(r.Context(), w, apperr.New(apperr.CodeBadRequest, "expected one filename"))
|
|
return
|
|
}
|
|
if !attachment.ValidFilename(values[0]) {
|
|
framework.JSONError(r.Context(), w, apperr.New(apperr.CodeBadRequest, "invalid filename"))
|
|
return
|
|
}
|
|
mt, _, e := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
|
if e != nil || (mt != "image/png" && mt != "image/jpeg") {
|
|
framework.JSONError(r.Context(), w, apperr.New(apperr.CodeUnsupportedMediaType, "expected PNG or JPEG"))
|
|
return
|
|
}
|
|
if r.ContentLength > h.svc.Config.MaxBytes {
|
|
framework.JSONError(r.Context(), w, apperr.New(apperr.CodePayloadTooLarge, "upload too large"))
|
|
return
|
|
}
|
|
a, e := h.svc.Upload(r.Context(), dto.UploadAttachment{UserID: user.UserID, ChatID: id, Filename: values[0], ContentType: mt, Body: io.LimitReader(r.Body, h.svc.Config.MaxBytes+1)})
|
|
if e != nil {
|
|
if apperr.CodeFromError(e) == apperr.CodeTooManyRequests {
|
|
w.Header().Set("Retry-After", "1")
|
|
}
|
|
framework.JSONError(r.Context(), w, e)
|
|
return
|
|
}
|
|
framework.JSONResponse(w, http.StatusAccepted, a)
|
|
}
|
|
func (h *AttachmentHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|
user, e := framework.ParseToken(r, h.provider)
|
|
if e != nil {
|
|
framework.JSONError(r.Context(), w, e)
|
|
return
|
|
}
|
|
id, e := framework.ParseIDFromPathParam(r.Context(), w, r, "attachment_id")
|
|
if e != nil {
|
|
return
|
|
}
|
|
|
|
if !strings.HasSuffix(r.URL.Path, "/content") {
|
|
a, e := h.svc.Get(r.Context(), user.UserID, id)
|
|
if e != nil {
|
|
framework.JSONError(r.Context(), w, e)
|
|
return
|
|
}
|
|
framework.JSONResponse(w, 200, a.Public)
|
|
return
|
|
}
|
|
v := r.URL.Query().Get("variant")
|
|
if v == "" {
|
|
v = "original"
|
|
}
|
|
if v != "original" && v != "thumbnail" && v != "crop" && v != "optimized" {
|
|
framework.JSONError(r.Context(), w, apperr.New(apperr.CodeBadRequest, "invalid variant"))
|
|
return
|
|
}
|
|
obj, e := h.svc.Content(r.Context(), user.UserID, id, v)
|
|
if e != nil {
|
|
framework.JSONError(r.Context(), w, e)
|
|
return
|
|
}
|
|
defer obj.Body.Close()
|
|
w.Header().Set("Content-Type", obj.ContentType)
|
|
w.Header().Set("Content-Length", strconv.FormatInt(obj.Size, 10))
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
// Once streaming has started a transport failure terminates the response; no JSON
|
|
// error can be appended to an image. Content-Length exposes incomplete downloads.
|
|
if _, e = io.Copy(w, obj.Body); e != nil {
|
|
panic(http.ErrAbortHandler)
|
|
}
|
|
}
|