feat(imaging): lab5 add webp optimization and strip metadata
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package imaging
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"time"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
|
||||
"github.com/gen2brain/webp"
|
||||
)
|
||||
|
||||
func WebP(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)
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := webp.Encode(&buf, img, webp.Options{Quality: 80}); err != nil {
|
||||
return nil, nil, fmt.Errorf("кодирование webp: %w", err)
|
||||
}
|
||||
|
||||
data := buf.Bytes()
|
||||
sha := sha256.Sum256(data)
|
||||
|
||||
v := &domain.AttachmentVariant{
|
||||
AttachmentID: attachmentID,
|
||||
Name: domain.VariantWebP,
|
||||
ContentType: "image/webp",
|
||||
SizeBytes: int64(len(data)),
|
||||
Width: img.Bounds().Dx(),
|
||||
Height: img.Bounds().Dy(),
|
||||
SHA256: sha[:],
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
return v, data, nil
|
||||
}
|
||||
Reference in New Issue
Block a user