forked from mtusi2026/mtusi2026lab6
97 lines
3.2 KiB
Go
97 lines
3.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"backend/internal/adapter/imagefile"
|
|
"backend/internal/domain/model"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
func toAttachment(a model.Attachment) Attachment {
|
|
variants := make([]Variant, 0, len(a.Variants))
|
|
for _, v := range a.Variants {
|
|
variants = append(variants, Variant{Name: VariantName(v.Name), ContentType: VariantContentType(v.ContentType), SizeBytes: int(v.SizeBytes), Sha256: v.SHA256, Width: v.Width, Height: v.Height})
|
|
}
|
|
var code *string
|
|
if a.ErrorCode != "" {
|
|
code = &a.ErrorCode
|
|
}
|
|
return Attachment{Id: a.ID, ChatId: a.ChatID, OwnerId: a.OwnerID, Filename: a.Filename, ContentType: AttachmentContentType(a.ContentType), SizeBytes: int(a.SizeBytes), Sha256: a.SHA256, Status: AttachmentStatus(a.Status), Variants: variants, ErrorCode: code, CreatedAt: Timestamp(a.CreatedAt), UpdatedAt: Timestamp(a.UpdatedAt)}
|
|
}
|
|
func (h *API) UploadAttachment(ctx context.Context, req UploadAttachmentRequestObject) (UploadAttachmentResponseObject, error) {
|
|
caller := currentUser(ctx).ID
|
|
if _, err := h.app.GetChat(ctx, caller, req.ChatId); err != nil {
|
|
return nil, err
|
|
}
|
|
select {
|
|
case h.uploadGate <- struct{}{}:
|
|
defer func() { <-h.uploadGate }()
|
|
case <-ctx.Done():
|
|
return nil, errOverloaded
|
|
}
|
|
r := ctx.Value(sessionRequestKey{}).(*http.Request)
|
|
if r.ContentLength > model.MaxImageBytes {
|
|
return nil, errPayloadTooLarge
|
|
}
|
|
f, err := imagefile.Inspect(r.Body, req.Params.Filename, r.Header.Get("Content-Type"))
|
|
if errors.Is(err, imagefile.ErrTooLarge) {
|
|
return nil, errPayloadTooLarge
|
|
}
|
|
if errors.Is(err, imagefile.ErrUnsupported) {
|
|
return nil, errUnsupportedImage
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Dispose()
|
|
a, err := h.attachments.Upload(ctx, caller, req.ChatId, req.Params.Filename, f.ContentType, f.SHA256, f.Size, f.File)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return UploadAttachment202JSONResponse{Body: toAttachment(a)}, nil
|
|
}
|
|
func (h *API) GetAttachment(ctx context.Context, req GetAttachmentRequestObject) (GetAttachmentResponseObject, error) {
|
|
a, e := h.attachments.Get(ctx, currentUser(ctx).ID, req.AttachmentId)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
return GetAttachment200JSONResponse{Body: toAttachment(a)}, nil
|
|
}
|
|
|
|
// Closing in the response visitor keeps the S3 stream alive until serialization completes.
|
|
type originalResponse struct {
|
|
body io.ReadCloser
|
|
size int64
|
|
mime string
|
|
}
|
|
|
|
func (r originalResponse) VisitDownloadAttachmentResponse(w http.ResponseWriter) error {
|
|
defer r.body.Close()
|
|
w.Header().Set("Content-Type", r.mime)
|
|
w.Header().Set("Content-Length", strconv.FormatInt(r.size, 10))
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
w.WriteHeader(200)
|
|
_, err := io.CopyN(w, r.body, r.size)
|
|
if err != nil {
|
|
panic(http.ErrAbortHandler)
|
|
}
|
|
return nil
|
|
}
|
|
func (h *API) DownloadAttachment(ctx context.Context, req DownloadAttachmentRequestObject) (DownloadAttachmentResponseObject, error) {
|
|
if _, e := h.attachments.Get(ctx, currentUser(ctx).ID, req.AttachmentId); e != nil {
|
|
return nil, e
|
|
}
|
|
variant := "original"
|
|
if req.Params.Variant != nil {
|
|
variant = string(*req.Params.Variant)
|
|
}
|
|
a, r, e := h.attachments.Content(ctx, currentUser(ctx).ID, req.AttachmentId, variant)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
return originalResponse{r, a.SizeBytes, a.ContentType}, nil
|
|
}
|