forked from mtusi2026/mtusi2026lab5
feat(worker): enforce image processing timeout with child process
This commit is contained in:
+23
-23
@@ -21,18 +21,18 @@ import (
|
||||
"mtusi2026-messenger/internal/db"
|
||||
)
|
||||
|
||||
var errInvalidSource = errors.New("invalid source image")
|
||||
var ErrInvalidSource = errors.New("invalid source image")
|
||||
|
||||
type imageVariant struct {
|
||||
name db.AttachmentVariantName
|
||||
data []byte
|
||||
contentType string
|
||||
width, height int
|
||||
Name db.AttachmentVariantName
|
||||
Data []byte
|
||||
ContentType string
|
||||
Width, Height int
|
||||
}
|
||||
|
||||
func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAttachmentVariantParams, error) {
|
||||
if row.StagingKey == nil {
|
||||
return nil, errInvalidSource
|
||||
return nil, ErrInvalidSource
|
||||
}
|
||||
|
||||
source, err := w.s3.Get(ctx, *row.StagingKey)
|
||||
@@ -60,7 +60,7 @@ func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAtt
|
||||
|
||||
if size != int64(row.SizeBytes) ||
|
||||
hex.EncodeToString(hash.Sum(nil)) != row.Sha256 {
|
||||
return nil, errInvalidSource
|
||||
return nil, ErrInvalidSource
|
||||
}
|
||||
|
||||
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||
@@ -78,7 +78,7 @@ func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAtt
|
||||
return nil, err
|
||||
}
|
||||
|
||||
images, err := imageVariants(tmp, w.maxPixels)
|
||||
images, err := runImageProcess(ctx, tmp, w.maxPixels)
|
||||
<-w.decodeSlot
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -98,21 +98,21 @@ 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)), img.contentType); err != nil {
|
||||
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)), img.ContentType); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sum := sha256.Sum256(img.data)
|
||||
sum := sha256.Sum256(img.Data)
|
||||
variants = append(variants, db.CreateAttachmentVariantParams{
|
||||
AttachmentID: row.ID,
|
||||
PipelineVersion: row.PipelineVersion,
|
||||
Variant: img.name,
|
||||
Variant: img.Name,
|
||||
S3Key: key,
|
||||
ContentType: img.contentType,
|
||||
SizeBytes: len(img.data),
|
||||
Width: img.width,
|
||||
Height: img.height,
|
||||
ContentType: img.ContentType,
|
||||
SizeBytes: len(img.Data),
|
||||
Width: img.Width,
|
||||
Height: img.Height,
|
||||
Sha256: hex.EncodeToString(sum[:]),
|
||||
})
|
||||
}
|
||||
@@ -127,7 +127,7 @@ func imageVariants(r io.ReadSeeker, maxPixels int) ([]imageVariant, error) {
|
||||
cfg.Width <= 0 ||
|
||||
cfg.Height <= 0 ||
|
||||
cfg.Width > maxPixels/cfg.Height {
|
||||
return nil, errInvalidSource
|
||||
return nil, ErrInvalidSource
|
||||
}
|
||||
|
||||
if _, err := r.Seek(0, io.SeekStart); err != nil {
|
||||
@@ -136,7 +136,7 @@ func imageVariants(r io.ReadSeeker, maxPixels int) ([]imageVariant, error) {
|
||||
|
||||
src, err := imaging.Decode(r, imaging.AutoOrientation(true))
|
||||
if err != nil {
|
||||
return nil, errInvalidSource
|
||||
return nil, ErrInvalidSource
|
||||
}
|
||||
|
||||
variants := make([]imageVariant, 0, 3)
|
||||
@@ -162,11 +162,11 @@ func imageVariants(r io.ReadSeeker, maxPixels int) ([]imageVariant, error) {
|
||||
}
|
||||
|
||||
variants = append(variants, imageVariant{
|
||||
name: item.name,
|
||||
contentType: contentType,
|
||||
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(),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/gob"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func ProcessImage(maxPixels int) error {
|
||||
variants, err := imageVariants(os.Stdin, maxPixels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return gob.NewEncoder(os.Stdout).Encode(variants)
|
||||
}
|
||||
|
||||
func runImageProcess(ctx context.Context, source *os.File, maxPixels int) ([]imageVariant, error) {
|
||||
executable, err := os.Executable()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(ctx, executable, "-process-image", strconv.Itoa(maxPixels))
|
||||
cmd.Stdin = source
|
||||
data, err := cmd.Output()
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(err, &exitErr) && exitErr.ExitCode() == 2 {
|
||||
return nil, ErrInvalidSource
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var variants []imageVariant
|
||||
if err := gob.NewDecoder(bytes.NewReader(data)).Decode(&variants); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return variants, nil
|
||||
}
|
||||
@@ -31,7 +31,7 @@ func (w *Worker) finishFailure(ctx context.Context, job model.AttachmentJob, cau
|
||||
}
|
||||
|
||||
code := "processing_failed"
|
||||
permanent := errors.Is(cause, errInvalidSource) || errors.Is(cause, s3store.ErrObjectNotFound)
|
||||
permanent := errors.Is(cause, ErrInvalidSource) || errors.Is(cause, s3store.ErrObjectNotFound)
|
||||
if permanent {
|
||||
code = "invalid_source"
|
||||
} else if errors.Is(cause, context.DeadlineExceeded) {
|
||||
|
||||
Reference in New Issue
Block a user