Переведите приём и чтение вложений на асинхронный контракт
This commit is contained in:
@@ -26,22 +26,37 @@ func (h *Handler) Register(protected, binaryProtected gin.IRouter) {
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Status string `json:"status"`
|
||||
Variants []VariantResponse `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
type VariantResponse struct {
|
||||
Name string `json:"name"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Status string `json:"status"`
|
||||
// Lab4's shared response contract requires variants=[], even without processing.
|
||||
Variants []any `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
func toResponse(a Attachment) Response {
|
||||
variants := make([]VariantResponse, 0, len(a.Variants))
|
||||
for _, v := range a.Variants {
|
||||
variants = append(variants, VariantResponse{
|
||||
Name: v.Name, ContentType: v.MIMEType, SizeBytes: v.SizeBytes,
|
||||
Width: v.Width, Height: v.Height, SHA256: hex.EncodeToString(v.Hash),
|
||||
})
|
||||
}
|
||||
return Response{
|
||||
ID: a.ID.String(),
|
||||
ChatID: a.ChatID.String(),
|
||||
@@ -51,9 +66,10 @@ func toResponse(a Attachment) Response {
|
||||
SizeBytes: a.SizeBytes,
|
||||
SHA256: hex.EncodeToString(a.FileHash),
|
||||
Status: string(a.Status),
|
||||
Variants: []any{},
|
||||
Variants: variants,
|
||||
ErrorCode: a.ErrorCode,
|
||||
CreatedAt: httpx.Timestamp(a.CreatedAt),
|
||||
UpdatedAt: httpx.Timestamp(a.CreatedAt),
|
||||
UpdatedAt: httpx.Timestamp(a.UpdatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +105,7 @@ func (h *Handler) upload(c *gin.Context, actor uuid.UUID) {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusCreated, toResponse(a))
|
||||
httpx.JSON(c, http.StatusAccepted, toResponse(a))
|
||||
}
|
||||
func attachmentID(c *gin.Context) (uuid.UUID, error) {
|
||||
id, err := ident.Parse(c.Param("attachment_id"))
|
||||
@@ -117,12 +133,21 @@ func (h *Handler) content(c *gin.Context, actor uuid.UUID) {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
variant, present := c.GetQuery("variant")
|
||||
if present && variant != "original" {
|
||||
httpx.Error(c, apperr.Invalid("variant must be original"))
|
||||
return
|
||||
variant := "original"
|
||||
if values, ok := c.Request.URL.Query()["variant"]; ok {
|
||||
if len(values) != 1 {
|
||||
httpx.Error(c, apperr.Invalid("one variant is required"))
|
||||
return
|
||||
}
|
||||
variant = values[0]
|
||||
switch variant {
|
||||
case "original", "thumbnail", "crop", "optimized":
|
||||
default:
|
||||
httpx.Error(c, apperr.Invalid("unknown variant"))
|
||||
return
|
||||
}
|
||||
}
|
||||
a, r, err := h.s.Content(c.Request.Context(), actor, id)
|
||||
a, r, err := h.s.Content(c.Request.Context(), actor, id, variant)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user