forked from mtusi2026/mtusi2026lab5
feat(worker): add optimized WebP variant
This commit is contained in:
+37
-18
@@ -16,6 +16,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/gen2brain/webp"
|
||||
|
||||
"mtusi2026-messenger/internal/db"
|
||||
)
|
||||
@@ -25,6 +26,7 @@ var errInvalidSource = errors.New("invalid source image")
|
||||
type imageVariant struct {
|
||||
name db.AttachmentVariantName
|
||||
data []byte
|
||||
contentType string
|
||||
width, height int
|
||||
}
|
||||
|
||||
@@ -97,7 +99,7 @@ func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAtt
|
||||
variants := make([]db.CreateAttachmentVariantParams, 0, len(images))
|
||||
for _, img := range images {
|
||||
key := "variants/" + row.ID.String() + "/v" + strconv.Itoa(row.PipelineVersion) + "/" + string(img.name)
|
||||
if err := w.s3.Put(ctx, key, bytes.NewReader(img.data), int64(len(img.data)), "image/jpeg"); err != nil {
|
||||
if err := w.s3.Put(ctx, key, bytes.NewReader(img.data), int64(len(img.data)), img.contentType); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -107,7 +109,7 @@ func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAtt
|
||||
PipelineVersion: row.PipelineVersion,
|
||||
Variant: img.name,
|
||||
S3Key: key,
|
||||
ContentType: "image/jpeg",
|
||||
ContentType: img.contentType,
|
||||
SizeBytes: len(img.data),
|
||||
Width: img.width,
|
||||
Height: img.height,
|
||||
@@ -137,41 +139,49 @@ func imageVariants(r io.ReadSeeker, maxPixels int) ([]imageVariant, error) {
|
||||
return nil, errInvalidSource
|
||||
}
|
||||
|
||||
thumb := thumbnail(src)
|
||||
crop := imaging.Fill(src, 128, 128, imaging.Center, imaging.Lanczos)
|
||||
variants := make([]imageVariant, 0, 2)
|
||||
variants := make([]imageVariant, 0, 3)
|
||||
for _, item := range []struct {
|
||||
name db.AttachmentVariantName
|
||||
image image.Image
|
||||
}{
|
||||
{db.AttachmentVariantNameThumbnail, thumb},
|
||||
{db.AttachmentVariantNameCrop, crop},
|
||||
{db.AttachmentVariantNameThumbnail, fitImage(src, 256)},
|
||||
{db.AttachmentVariantNameCrop, imaging.Fill(src, 128, 128, imaging.Center, imaging.Lanczos)},
|
||||
{db.AttachmentVariantNameOptimized, fitImage(src, 1280)},
|
||||
} {
|
||||
data, err := encodeJPEG(item.image)
|
||||
var data []byte
|
||||
contentType := "image/jpeg"
|
||||
if item.name == db.AttachmentVariantNameOptimized {
|
||||
contentType = "image/webp"
|
||||
data, err = encodeWebP(item.image)
|
||||
} else {
|
||||
data, err = encodeJPEG(item.image)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
variants = append(variants, imageVariant{
|
||||
name: item.name,
|
||||
data: data,
|
||||
width: item.image.Bounds().Dx(),
|
||||
height: item.image.Bounds().Dy(),
|
||||
name: item.name,
|
||||
contentType: contentType,
|
||||
data: data,
|
||||
width: item.image.Bounds().Dx(),
|
||||
height: item.image.Bounds().Dy(),
|
||||
})
|
||||
}
|
||||
|
||||
return variants, nil
|
||||
}
|
||||
|
||||
func thumbnail(src image.Image) image.Image {
|
||||
func fitImage(src image.Image, maxSize int) image.Image {
|
||||
width, height := src.Bounds().Dx(), src.Bounds().Dy()
|
||||
if width > 256 || height > 256 {
|
||||
if width > maxSize || height > maxSize {
|
||||
if width >= height {
|
||||
height = max(1, height*256/width)
|
||||
width = 256
|
||||
height = max(1, height*maxSize/width)
|
||||
width = maxSize
|
||||
} else {
|
||||
width = max(1, width*256/height)
|
||||
height = 256
|
||||
width = max(1, width*maxSize/height)
|
||||
height = maxSize
|
||||
}
|
||||
|
||||
return imaging.Resize(src, width, height, imaging.Lanczos)
|
||||
@@ -192,3 +202,12 @@ func encodeJPEG(src image.Image) ([]byte, error) {
|
||||
|
||||
return out.Bytes(), nil
|
||||
}
|
||||
|
||||
func encodeWebP(src image.Image) ([]byte, error) {
|
||||
var out bytes.Buffer
|
||||
if err := webp.Encode(&out, src, webp.Options{Quality: 80, Method: 4}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return out.Bytes(), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user