feat(imaging): lab5 add optimized webp generation and update enums

This commit is contained in:
Голиков Maximillian Антон
2026-11-09 17:28:59 +03:00
parent b8d6e0efda
commit 9b28579031
3 changed files with 10 additions and 7 deletions
@@ -17,5 +17,5 @@ type AttachmentVariant struct {
const (
VariantThumbnail = "thumbnail"
VariantCrop = "crop"
VariantWebP = "webp"
VariantOptimized = "optimized"
)
@@ -11,17 +11,20 @@ import (
"high-loud-apps-labs/internal/domain"
"github.com/disintegration/imaging"
"github.com/gen2brain/webp"
)
func WebP(attachmentID string, imgData []byte) (*domain.AttachmentVariant, []byte, error) {
func Optimized(attachmentID string, imgData []byte) (*domain.AttachmentVariant, []byte, error) {
img, _, err := image.Decode(bytes.NewReader(imgData))
if err != nil {
return nil, nil, fmt.Errorf("декодирование изображения: %w", err)
}
optimizedImg := imaging.Fit(img, 1280, 1280, imaging.Lanczos)
var buf bytes.Buffer
if err := webp.Encode(&buf, img, webp.Options{Quality: 80}); err != nil {
if err := webp.Encode(&buf, optimizedImg, webp.Options{Quality: 80}); err != nil {
return nil, nil, fmt.Errorf("кодирование webp: %w", err)
}
@@ -30,11 +33,11 @@ func WebP(attachmentID string, imgData []byte) (*domain.AttachmentVariant, []byt
v := &domain.AttachmentVariant{
AttachmentID: attachmentID,
Name: domain.VariantWebP,
Name: domain.VariantOptimized,
ContentType: "image/webp",
SizeBytes: int64(len(data)),
Width: img.Bounds().Dx(),
Height: img.Bounds().Dy(),
Width: optimizedImg.Bounds().Dx(),
Height: optimizedImg.Bounds().Dy(),
SHA256: sha[:],
CreatedAt: time.Now().UTC(),
}
+1 -1
View File
@@ -13,7 +13,7 @@ CREATE TABLE attachment_variants (
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT attachment_variants_pkey PRIMARY KEY (attachment_id, name),
CONSTRAINT attachment_variants_name_chk CHECK (name IN ('thumbnail', 'crop', 'webp')),
CONSTRAINT attachment_variants_name_chk CHECK (name IN ('thumbnail', 'crop', 'optimized')),
CONSTRAINT attachment_variants_size_chk CHECK (size_bytes > 0),
CONSTRAINT attachment_variants_sha256_len CHECK (octet_length(sha256) = 32)
);