feat(imaging): lab5 add optimized webp generation and update enums
This commit is contained in:
@@ -17,5 +17,5 @@ type AttachmentVariant struct {
|
|||||||
const (
|
const (
|
||||||
VariantThumbnail = "thumbnail"
|
VariantThumbnail = "thumbnail"
|
||||||
VariantCrop = "crop"
|
VariantCrop = "crop"
|
||||||
VariantWebP = "webp"
|
VariantOptimized = "optimized"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -11,17 +11,20 @@ import (
|
|||||||
|
|
||||||
"high-loud-apps-labs/internal/domain"
|
"high-loud-apps-labs/internal/domain"
|
||||||
|
|
||||||
|
"github.com/disintegration/imaging"
|
||||||
"github.com/gen2brain/webp"
|
"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))
|
img, _, err := image.Decode(bytes.NewReader(imgData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("декодирование изображения: %w", err)
|
return nil, nil, fmt.Errorf("декодирование изображения: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
optimizedImg := imaging.Fit(img, 1280, 1280, imaging.Lanczos)
|
||||||
|
|
||||||
var buf bytes.Buffer
|
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)
|
return nil, nil, fmt.Errorf("кодирование webp: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,11 +33,11 @@ func WebP(attachmentID string, imgData []byte) (*domain.AttachmentVariant, []byt
|
|||||||
|
|
||||||
v := &domain.AttachmentVariant{
|
v := &domain.AttachmentVariant{
|
||||||
AttachmentID: attachmentID,
|
AttachmentID: attachmentID,
|
||||||
Name: domain.VariantWebP,
|
Name: domain.VariantOptimized,
|
||||||
ContentType: "image/webp",
|
ContentType: "image/webp",
|
||||||
SizeBytes: int64(len(data)),
|
SizeBytes: int64(len(data)),
|
||||||
Width: img.Bounds().Dx(),
|
Width: optimizedImg.Bounds().Dx(),
|
||||||
Height: img.Bounds().Dy(),
|
Height: optimizedImg.Bounds().Dy(),
|
||||||
SHA256: sha[:],
|
SHA256: sha[:],
|
||||||
CreatedAt: time.Now().UTC(),
|
CreatedAt: time.Now().UTC(),
|
||||||
}
|
}
|
||||||
@@ -13,7 +13,7 @@ CREATE TABLE attachment_variants (
|
|||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
|
|
||||||
CONSTRAINT attachment_variants_pkey PRIMARY KEY (attachment_id, name),
|
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_size_chk CHECK (size_bytes > 0),
|
||||||
CONSTRAINT attachment_variants_sha256_len CHECK (octet_length(sha256) = 32)
|
CONSTRAINT attachment_variants_sha256_len CHECK (octet_length(sha256) = 32)
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user