From b8d6e0efda9b6ba55aa30a36801ed2f46d047304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Mon, 9 Nov 2026 16:19:33 +0300 Subject: [PATCH] feat(imaging): lab5 add webp optimization and strip metadata --- backend/go.mod | 6 ++++- backend/go.sum | 4 +++ backend/internal/imaging/webp.go | 42 ++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 backend/internal/imaging/webp.go diff --git a/backend/go.mod b/backend/go.mod index f4b5465..1fa34b4 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -13,7 +13,10 @@ require ( golang.org/x/crypto v0.57.0 ) -require golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect +require ( + github.com/ebitengine/purego v0.10.1 // indirect + golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect +) require ( github.com/bytedance/gopkg v0.1.3 // indirect @@ -24,6 +27,7 @@ require ( github.com/disintegration/imaging v1.6.2 github.com/dustin/go-humanize v1.0.1 // indirect github.com/gabriel-vasile/mimetype v1.4.12 // indirect + github.com/gen2brain/webp v0.6.4 github.com/gin-contrib/sse v1.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect diff --git a/backend/go.sum b/backend/go.sum index 6a155a8..73ba6e9 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -19,8 +19,12 @@ github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1 github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/ebitengine/purego v0.10.1 h1:dewVBCBT2GaMu1SrNTYxQhgQBethzfhiwvZiLGP/qyY= +github.com/ebitengine/purego v0.10.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= +github.com/gen2brain/webp v0.6.4 h1:SUDdmxADOAiPQ+5ylNmuHhuYf2dOi0KgKZHL5vpVCNU= +github.com/gen2brain/webp v0.6.4/go.mod h1:iGWMaCSw7t3I/Cv9llzEKmpnR36S8lS8VL/ZVjxU0JE= github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8= diff --git a/backend/internal/imaging/webp.go b/backend/internal/imaging/webp.go new file mode 100644 index 0000000..724743d --- /dev/null +++ b/backend/internal/imaging/webp.go @@ -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 +}