Files
mtusi2026lab5/internal/dto/attachment.go
T

99 lines
2.6 KiB
Go

package dto
import (
"time"
"unicode"
"unicode/utf8"
"github.com/google/uuid"
"mtusi2026-messenger/internal/model"
)
type CreateAttachmentRequest struct {
Filename string
}
type GetAttachmentContentRequest struct {
Variant string
}
type VariantResponse struct {
Name model.AttachmentVariantName `json:"name"`
ContentType model.ContentType `json:"content_type"`
SizeBytes int `json:"size_bytes"`
Width int `json:"width"`
Height int `json:"height"`
Sha256 string `json:"sha256"`
}
type AttachmentResponse struct {
ID uuid.UUID `json:"id"`
ChatID uuid.UUID `json:"chat_id"`
OwnerID uuid.UUID `json:"owner_id"`
Filename string `json:"filename"`
ContentType model.ContentType `json:"content_type"`
SizeBytes int `json:"size_bytes"`
Sha256 string `json:"sha256"`
Status model.AttachmentStatus `json:"status"`
Variants []VariantResponse `json:"variants"`
ErrorCode *string `json:"error_code"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func (r CreateAttachmentRequest) Validate() bool {
if n := utf8.RuneCountInString(r.Filename); n < 1 || n > 255 {
return false
}
for _, ch := range r.Filename {
if ch == '/' || ch == '\\' || ch == 0 || unicode.IsControl(ch) {
return false
}
}
return true
}
func (r GetAttachmentContentRequest) Validate() bool {
switch model.AttachmentVariantName(r.Variant) {
case "",
model.AttachmentContentOriginal,
model.AttachmentVariantNameThumbnail,
model.AttachmentVariantNameCrop,
model.AttachmentVariantNameOptimized:
return true
default:
return false
}
}
func ToAttachmentResponse(a model.Attachment) AttachmentResponse {
variants := make([]VariantResponse, 0, len(a.Variants))
for _, v := range a.Variants {
variants = append(variants, VariantResponse{
Name: v.Name,
ContentType: v.ContentType,
SizeBytes: v.SizeBytes,
Width: v.Width,
Height: v.Height,
Sha256: v.Sha256,
})
}
return AttachmentResponse{
ID: a.ID,
ChatID: a.ChatID,
OwnerID: a.OwnerID,
Filename: a.Filename,
ContentType: a.ContentType,
SizeBytes: a.SizeBytes,
Sha256: a.Sha256,
Status: a.Status,
Variants: variants,
ErrorCode: a.ErrorCode,
CreatedAt: a.CreatedAt,
UpdatedAt: a.UpdatedAt,
}
}