Исправьте кадрирование и очистку рабочих файлов кодека
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"messenger/internal/imaging"
|
||||||
"messenger/internal/platform/apperr"
|
"messenger/internal/platform/apperr"
|
||||||
"messenger/internal/platform/faultbarrier"
|
"messenger/internal/platform/faultbarrier"
|
||||||
"messenger/internal/platform/ident"
|
"messenger/internal/platform/ident"
|
||||||
@@ -35,10 +36,10 @@ type Config struct {
|
|||||||
// Binary operations report unavailability in that mode.
|
// Binary operations report unavailability in that mode.
|
||||||
func NewService(attachments AttachmentRepository, chats ChatMembership, blobs Blobs, cfg Config) *Service {
|
func NewService(attachments AttachmentRepository, chats ChatMembership, blobs Blobs, cfg Config) *Service {
|
||||||
if cfg.MaxBytes == 0 {
|
if cfg.MaxBytes == 0 {
|
||||||
cfg.MaxBytes = MaxUploadBytes
|
cfg.MaxBytes = imaging.MaxUploadBytes
|
||||||
}
|
}
|
||||||
if cfg.MaxPixels == 0 {
|
if cfg.MaxPixels == 0 {
|
||||||
cfg.MaxPixels = MaxImagePixels
|
cfg.MaxPixels = imaging.MaxImagePixels
|
||||||
}
|
}
|
||||||
return &Service{
|
return &Service{
|
||||||
attachments: attachments,
|
attachments: attachments,
|
||||||
|
|||||||
@@ -10,12 +10,10 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"messenger/internal/imaging"
|
||||||
"messenger/internal/platform/apperr"
|
"messenger/internal/platform/apperr"
|
||||||
)
|
)
|
||||||
|
|
||||||
const MaxUploadBytes int64 = 10 * 1024 * 1024
|
|
||||||
const MaxImagePixels int64 = 40_000_000
|
|
||||||
|
|
||||||
var imageFormats = map[string]string{
|
var imageFormats = map[string]string{
|
||||||
"image/png": "png",
|
"image/png": "png",
|
||||||
"image/jpeg": "jpeg",
|
"image/jpeg": "jpeg",
|
||||||
@@ -36,7 +34,7 @@ func ValidateFilename(name string) error {
|
|||||||
// The small header is replayed into the decoder; pixels are allocated only
|
// The small header is replayed into the decoder; pixels are allocated only
|
||||||
// after checking dimensions. The caller serializes decoding per replica.
|
// after checking dimensions. The caller serializes decoding per replica.
|
||||||
func ValidateImage(r io.Reader, mime string, maxPixels int64) error {
|
func ValidateImage(r io.Reader, mime string, maxPixels int64) error {
|
||||||
limited := io.LimitReader(r, MaxUploadBytes+1)
|
limited := io.LimitReader(r, imaging.MaxUploadBytes+1)
|
||||||
var header bytes.Buffer
|
var header bytes.Buffer
|
||||||
cfg, format, err := image.DecodeConfig(io.TeeReader(limited, &header))
|
cfg, format, err := image.DecodeConfig(io.TeeReader(limited, &header))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -36,6 +36,24 @@ var ErrInvalidImage = errors.New("invalid image or pipeline version")
|
|||||||
// occupy hundreds of MiB. A worker with a 2 GiB cgroup can run two ordinary jobs.
|
// occupy hundreds of MiB. A worker with a 2 GiB cgroup can run two ordinary jobs.
|
||||||
var decoderSlots = semaphore.NewWeighted(2)
|
var decoderSlots = semaphore.NewWeighted(2)
|
||||||
|
|
||||||
|
func scratchRoot() string {
|
||||||
|
return filepath.Join(os.TempDir(), "messenger-worker-imaging")
|
||||||
|
}
|
||||||
|
|
||||||
|
// CleanScratch reclaims pixel caches left by a killed worker. The consumer
|
||||||
|
// calls it before starting any jobs in its container's private filesystem.
|
||||||
|
func CleanScratch() error {
|
||||||
|
return os.RemoveAll(scratchRoot())
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRenderDir() (string, error) {
|
||||||
|
root := scratchRoot()
|
||||||
|
if err := os.MkdirAll(root, 0700); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return os.MkdirTemp(root, "attachment-")
|
||||||
|
}
|
||||||
|
|
||||||
// Render decodes once in a deadline-bound child process. Its pixel cache can
|
// Render decodes once in a deadline-bound child process. Its pixel cache can
|
||||||
// spill to a private disk directory when a high-resolution source exceeds RAM.
|
// spill to a private disk directory when a high-resolution source exceeds RAM.
|
||||||
func Render(ctx context.Context, source []byte, version int) ([]Variant, error) {
|
func Render(ctx context.Context, source []byte, version int) ([]Variant, error) {
|
||||||
@@ -61,7 +79,7 @@ func Render(ctx context.Context, source []byte, version int) ([]Variant, error)
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer decoderSlots.Release(weight)
|
defer decoderSlots.Release(weight)
|
||||||
dir, err := os.MkdirTemp("", "attachment-")
|
dir, err := newRenderDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -93,7 +111,7 @@ func Render(ctx context.Context, source []byte, version int) ([]Variant, error)
|
|||||||
args := []string{
|
args := []string{
|
||||||
"-limit", "thread", "1", "-limit", "memory", "96MiB",
|
"-limit", "thread", "1", "-limit", "memory", "96MiB",
|
||||||
"-limit", "map", "0", "-limit", "disk", "2GiB",
|
"-limit", "map", "0", "-limit", "disk", "2GiB",
|
||||||
input, "-colorspace", "sRGB",
|
input, "+repage", "-colorspace", "sRGB",
|
||||||
}
|
}
|
||||||
for _, v := range output {
|
for _, v := range output {
|
||||||
args = append(args, "(")
|
args = append(args, "(")
|
||||||
|
|||||||
@@ -268,3 +268,47 @@ func TestOnePixelMinorAxis(t *testing.T) {
|
|||||||
v := variants(t, testPicture(t, 1, 4096), 1)
|
v := variants(t, testPicture(t, 1, 4096), 1)
|
||||||
decodedJPEG(t, v["thumbnail"], 1, 256)
|
decodedJPEG(t, v["thumbnail"], 1, 256)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCropUsesRasterCenterOnOffsetPNGCanvas(t *testing.T) {
|
||||||
|
requireCodec(t)
|
||||||
|
src, err := exec.CommandContext(t.Context(), "magick", "-size", "320x200", "xc:red",
|
||||||
|
"-fill", "blue", "-draw", "rectangle 0,0 55,199 rectangle 265,0 319,199",
|
||||||
|
"-repage", "2400x1800+850+30", "png:-").Output()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
crop := decodedJPEG(t, variants(t, src, 1)["crop"], 128, 128)
|
||||||
|
for _, x := range []int{1, 64, 126} {
|
||||||
|
r, g, b, _ := crop.At(x, 64).RGBA()
|
||||||
|
if r < 0xe000 || max(g, b) > 0x2000 {
|
||||||
|
t.Fatalf("crop pixel (%d,64) = %x/%x/%x, want red", x, r, g, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCleanScratchRemovesInterruptedRender(t *testing.T) {
|
||||||
|
temp := t.TempDir()
|
||||||
|
for _, key := range []string{"TMPDIR", "TMP", "TEMP"} {
|
||||||
|
t.Setenv(key, temp)
|
||||||
|
}
|
||||||
|
dir, err := newRenderDir()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, "magick-cache"), []byte("pixels"), 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
unrelated := filepath.Join(temp, "another-process")
|
||||||
|
if err := os.Mkdir(unrelated, 0700); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := CleanScratch(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(dir); !errors.Is(err, os.ErrNotExist) {
|
||||||
|
t.Fatalf("interrupted render directory still accessible: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(unrelated); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"messenger/internal/imaging"
|
||||||
)
|
)
|
||||||
|
|
||||||
type S3 struct {
|
type S3 struct {
|
||||||
@@ -15,7 +17,10 @@ type S3 struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func LoadS3(lookup Lookup) (S3, error) {
|
func LoadS3(lookup Lookup) (S3, error) {
|
||||||
cfg := S3{Endpoint: "http://s3:8333", Region: "us-east-1", Bucket: "messenger-attachments", MaxBytes: 10485760, MaxPixels: 40000000, StagingRetention: 24 * time.Hour}
|
cfg := S3{
|
||||||
|
Endpoint: "http://s3:8333", Region: "us-east-1", Bucket: "messenger-attachments",
|
||||||
|
MaxBytes: imaging.MaxUploadBytes, MaxPixels: imaging.MaxImagePixels, StagingRetention: 24 * time.Hour,
|
||||||
|
}
|
||||||
for key, dest := range map[string]*string{"S3_ENDPOINT": &cfg.Endpoint, "S3_REGION": &cfg.Region, "S3_BUCKET": &cfg.Bucket} {
|
for key, dest := range map[string]*string{"S3_ENDPOINT": &cfg.Endpoint, "S3_REGION": &cfg.Region, "S3_BUCKET": &cfg.Bucket} {
|
||||||
if v, ok := lookup(key); ok {
|
if v, ok := lookup(key); ok {
|
||||||
*dest = v
|
*dest = v
|
||||||
@@ -32,7 +37,7 @@ func LoadS3(lookup Lookup) (S3, error) {
|
|||||||
key string
|
key string
|
||||||
dest *int64
|
dest *int64
|
||||||
max int64
|
max int64
|
||||||
}{{"MAX_UPLOAD_BYTES", &cfg.MaxBytes, 10485760}, {"MAX_IMAGE_PIXELS", &cfg.MaxPixels, 40000000}} {
|
}{{"MAX_UPLOAD_BYTES", &cfg.MaxBytes, imaging.MaxUploadBytes}, {"MAX_IMAGE_PIXELS", &cfg.MaxPixels, imaging.MaxImagePixels}} {
|
||||||
if v, ok := lookup(p.key); ok {
|
if v, ok := lookup(p.key); ok {
|
||||||
n, e := strconv.ParseInt(v, 10, 64)
|
n, e := strconv.ParseInt(v, 10, 64)
|
||||||
if e != nil || n < 1 || n > p.max {
|
if e != nil || n < 1 || n > p.max {
|
||||||
|
|||||||
Reference in New Issue
Block a user