feat: add durable image processing pipeline

This commit is contained in:
2026-10-22 17:22:28 +03:00
parent 8c2ac690a6
commit 42336ce7c2
39 changed files with 4379 additions and 126 deletions
+37 -8
View File
@@ -6,6 +6,7 @@ import (
"mime"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
@@ -46,6 +47,7 @@ func (s *Server) uploadAttachment(w http.ResponseWriter, r *http.Request) *apiEr
uploaded, err := s.attachments.Upload(r.Context(), attachment.Upload{
ChatID: chatID, OwnerID: user.ID, Filename: filename,
ContentType: r.Header.Get("Content-Type"), ContentLength: r.ContentLength, Body: r.Body,
Traceparent: traceparent(r.Header.Get("traceparent")), Tracestate: tracestate(r.Header.Get("tracestate")),
})
if err != nil {
if errors.Is(err, model.ErrCapacity) {
@@ -58,7 +60,7 @@ func (s *Server) uploadAttachment(w http.ResponseWriter, r *http.Request) *apiEr
}
result = "success"
s.metrics.attachmentBytes.Add(float64(uploaded.SizeBytes))
writeJSON(w, http.StatusCreated, uploaded)
writeJSON(w, http.StatusAccepted, uploaded)
return nil
}
@@ -108,20 +110,21 @@ func (s *Server) attachmentContent(w http.ResponseWriter, r *http.Request) *apiE
return authErr
}
query, err := url.ParseQuery(r.URL.RawQuery)
if err != nil || !originalVariant(query) {
variant, valid := attachmentVariant(query)
if err != nil || !valid {
return invalidRequest()
}
attachmentID, apiErr := attachmentIDFromRequest(r)
if apiErr != nil {
return apiErr
}
content, err := s.attachments.Content(r.Context(), attachmentID, user.ID)
content, err := s.attachments.Content(r.Context(), attachmentID, user.ID, variant)
if err != nil {
return attachmentError(err)
}
defer func() { _ = content.Body.Close() }()
w.Header().Set("Content-Type", content.Attachment.ContentType)
w.Header().Set("Content-Length", strconv.FormatInt(content.Attachment.SizeBytes, 10))
w.Header().Set("Content-Type", content.Variant.ContentType)
w.Header().Set("Content-Length", strconv.FormatInt(content.Variant.SizeBytes, 10))
w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": content.Attachment.Filename}))
w.WriteHeader(http.StatusOK)
if _, err := io.Copy(w, content.Body); err != nil {
@@ -130,11 +133,35 @@ func (s *Server) attachmentContent(w http.ResponseWriter, r *http.Request) *apiE
return nil
}
func originalVariant(query url.Values) bool {
func attachmentVariant(query url.Values) (string, bool) {
if len(query) == 0 {
return true
return "original", true
}
return len(query) == 1 && len(query["variant"]) == 1 && query["variant"][0] == "original"
if len(query) != 1 || len(query["variant"]) != 1 {
return "", false
}
switch query["variant"][0] {
case "original", "thumbnail", "crop", "optimized":
return query["variant"][0], true
default:
return "", false
}
}
var traceparentPattern = regexp.MustCompile(`^00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$`)
func traceparent(value string) string {
if traceparentPattern.MatchString(value) {
return value
}
return ""
}
func tracestate(value string) string {
if len(value) <= 512 {
return value
}
return ""
}
func attachmentIDFromRequest(r *http.Request) (string, *apiError) {
@@ -158,6 +185,8 @@ func attachmentError(err error) *apiError {
return &apiError{http.StatusRequestEntityTooLarge, errorCodePayloadTooLarge, "image is too large"}
case errors.Is(err, model.ErrCapacity):
return &apiError{http.StatusTooManyRequests, errorCodeRateLimited, "image capacity is temporarily exhausted"}
case errors.Is(err, model.ErrConflict):
return &apiError{http.StatusConflict, errorCodeConflict, "attachment is not ready"}
default:
return unavailable()
}
@@ -28,7 +28,7 @@ type MessageRepository interface {
type AttachmentService interface {
Upload(context.Context, attachment.Upload) (model.Attachment, error)
Metadata(context.Context, string, string) (model.Attachment, error)
Content(context.Context, string, string) (attachment.Content, error)
Content(context.Context, string, string, string) (attachment.Content, error)
}
type HealthChecker interface {
+6 -2
View File
@@ -87,8 +87,12 @@ func (s attachmentServiceStub) Upload(context.Context, attachment.Upload) (model
func (s attachmentServiceStub) Metadata(context.Context, string, string) (model.Attachment, error) {
return s.uploaded, s.err
}
func (s attachmentServiceStub) Content(context.Context, string, string) (attachment.Content, error) {
return attachment.Content{Attachment: s.uploaded, Body: io.NopCloser(strings.NewReader("content"))}, s.err
func (s attachmentServiceStub) Content(context.Context, string, string, string) (attachment.Content, error) {
return attachment.Content{
Attachment: s.uploaded,
Variant: model.Variant{ContentType: s.uploaded.ContentType, SizeBytes: s.uploaded.SizeBytes},
Body: io.NopCloser(strings.NewReader("content")),
}, s.err
}
type sessionStub struct {