forked from mtusi2026/mtusi2026lab5
Реализация на 5
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
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"
|
||||
@@ -41,7 +43,20 @@ func (h *AttachmentHandler) Upload(w http.ResponseWriter, r *http.Request) {
|
||||
framework.JSONError(r.Context(), w, apperr.New(apperr.CodeBadRequest, "expected one filename"))
|
||||
return
|
||||
}
|
||||
a, e := h.svc.Upload(w, r, user.UserID, id, values[0])
|
||||
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")
|
||||
@@ -49,7 +64,7 @@ func (h *AttachmentHandler) Upload(w http.ResponseWriter, r *http.Request) {
|
||||
framework.JSONError(r.Context(), w, e)
|
||||
return
|
||||
}
|
||||
framework.JSONResponse(w, http.StatusCreated, a)
|
||||
framework.JSONResponse(w, http.StatusAccepted, a)
|
||||
}
|
||||
func (h *AttachmentHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
user, e := framework.ParseToken(r, h.provider)
|
||||
@@ -61,27 +76,32 @@ func (h *AttachmentHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
a, e := h.svc.Get(r.Context(), user.UserID, id)
|
||||
|
||||
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
|
||||
}
|
||||
if !strings.HasSuffix(r.URL.Path, "/content") {
|
||||
framework.JSONResponse(w, 200, a.Public)
|
||||
return
|
||||
}
|
||||
if v := r.URL.Query().Get("variant"); v != "" && v != "original" {
|
||||
framework.JSONError(r.Context(), w, apperr.New(apperr.CodeBadRequest, "invalid variant"))
|
||||
return
|
||||
}
|
||||
obj, e := h.svc.Store.Get(r.Context(), a.Key)
|
||||
if e != nil {
|
||||
framework.JSONError(r.Context(), w, attachment.Unavailable())
|
||||
return
|
||||
}
|
||||
defer obj.Body.Close()
|
||||
w.Header().Set("Content-Type", string(a.Public.ContentType))
|
||||
w.Header().Set("Content-Length", strconv.Itoa(a.Public.SizeBytes))
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user