feat(worker): add versioned image processing
This commit is contained in:
@@ -21,7 +21,10 @@ import (
|
||||
"mtusi2026-messenger/internal/db"
|
||||
)
|
||||
|
||||
var ErrInvalidSource = errors.New("invalid source image")
|
||||
var (
|
||||
ErrInvalidSource = errors.New("invalid source image")
|
||||
ErrUnsupportedPipelineVersion = errors.New("unsupported pipeline version")
|
||||
)
|
||||
|
||||
type imageVariant struct {
|
||||
Name db.AttachmentVariantName
|
||||
@@ -78,7 +81,7 @@ func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAtt
|
||||
return nil, err
|
||||
}
|
||||
|
||||
images, err := runImageProcess(ctx, tmp, w.maxPixels)
|
||||
images, err := runImageProcess(ctx, tmp, w.maxPixels, row.PipelineVersion)
|
||||
<-w.decodeSlot
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -120,7 +123,16 @@ func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAtt
|
||||
return variants, nil
|
||||
}
|
||||
|
||||
func imageVariants(r io.ReadSeeker, maxPixels int) ([]imageVariant, error) {
|
||||
func imageVariants(r io.ReadSeeker, maxPixels, pipelineVersion int) ([]imageVariant, error) {
|
||||
quality := 80
|
||||
switch pipelineVersion {
|
||||
case 1:
|
||||
case 2:
|
||||
quality = 75
|
||||
default:
|
||||
return nil, ErrUnsupportedPipelineVersion
|
||||
}
|
||||
|
||||
cfg, format, err := image.DecodeConfig(r)
|
||||
if err != nil ||
|
||||
(format != "png" && format != "jpeg") ||
|
||||
@@ -152,9 +164,9 @@ func imageVariants(r io.ReadSeeker, maxPixels int) ([]imageVariant, error) {
|
||||
contentType := "image/jpeg"
|
||||
if item.name == db.AttachmentVariantNameOptimized {
|
||||
contentType = "image/webp"
|
||||
data, err = encodeWebP(item.image)
|
||||
data, err = encodeWebP(item.image, quality)
|
||||
} else {
|
||||
data, err = encodeJPEG(item.image)
|
||||
data, err = encodeJPEG(item.image, 85)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@@ -190,22 +202,22 @@ func fitImage(src image.Image, maxSize int) image.Image {
|
||||
return src
|
||||
}
|
||||
|
||||
func encodeJPEG(src image.Image) ([]byte, error) {
|
||||
func encodeJPEG(src image.Image, quality int) ([]byte, error) {
|
||||
dst := image.NewRGBA(image.Rect(0, 0, src.Bounds().Dx(), src.Bounds().Dy()))
|
||||
draw.Draw(dst, dst.Bounds(), image.NewUniform(color.White), image.Point{}, draw.Src)
|
||||
draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Over)
|
||||
|
||||
var out bytes.Buffer
|
||||
if err := jpeg.Encode(&out, dst, &jpeg.Options{Quality: 85}); err != nil {
|
||||
if err := jpeg.Encode(&out, dst, &jpeg.Options{Quality: quality}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return out.Bytes(), nil
|
||||
}
|
||||
|
||||
func encodeWebP(src image.Image) ([]byte, error) {
|
||||
func encodeWebP(src image.Image, quality int) ([]byte, error) {
|
||||
var out bytes.Buffer
|
||||
if err := webp.Encode(&out, src, webp.Options{Quality: 80, Method: 4}); err != nil {
|
||||
if err := webp.Encode(&out, src, webp.Options{Quality: quality, Method: 4}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func ProcessImage(maxPixels int) error {
|
||||
variants, err := imageVariants(os.Stdin, maxPixels)
|
||||
func ProcessImage(maxPixels, pipelineVersion int) error {
|
||||
variants, err := imageVariants(os.Stdin, maxPixels, pipelineVersion)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -19,13 +19,16 @@ func ProcessImage(maxPixels int) error {
|
||||
return gob.NewEncoder(os.Stdout).Encode(variants)
|
||||
}
|
||||
|
||||
func runImageProcess(ctx context.Context, source *os.File, maxPixels int) ([]imageVariant, error) {
|
||||
func runImageProcess(ctx context.Context, source *os.File, maxPixels, pipelineVersion int) ([]imageVariant, error) {
|
||||
executable, err := os.Executable()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(ctx, executable, "-process-image", strconv.Itoa(maxPixels))
|
||||
cmd := exec.CommandContext(ctx, executable,
|
||||
"-process-image", strconv.Itoa(maxPixels),
|
||||
"-pipeline-version", strconv.Itoa(pipelineVersion),
|
||||
)
|
||||
cmd.Stdin = source
|
||||
data, err := cmd.Output()
|
||||
if ctx.Err() != nil {
|
||||
|
||||
Reference in New Issue
Block a user