Files
majordomo/imagegen/background.go
T
steve 4a752ffa2a
CI / Tidy (pull_request) Successful in 9m24s
CI / Build & Test (pull_request) Successful in 10m17s
fix: harden upstream path + binary-body validation (gadfly round 1)
- upstreamPath rejects '..' in model ids AND in the rest path — the rest
  can embed SERVER-SUPPLIED components (ACE-Step result file URLs), so
  dot-dot/scheme smuggling toward other proxy endpoints is refused
- singleImageResult requires positive image evidence (sniffed magic OR
  declared image/*): an empty-Content-Type error page can no longer pass
  as 'the image' via sniffImageMIME's PNG-default labelling
- upscale/background responses get a dedicated 256MB cap (the 64MB cap
  is JSON-sized; a 4x PNG legitimately exceeds it)
- mesh JSON-detection widened (512-byte whitespace-tolerant peek + reject
  declared application/json)
- Transcribe now reuses buildMultipart; transcriptionFilename takes
  (filename, mime) so diarize shares it without a fake request struct;
  truncateForError stops shadowing builtin cap; OnlyMask doc de-ambiguated

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 00:50:21 -04:00

77 lines
2.9 KiB
Go

package imagegen
import "context"
// BackgroundRemovalRequest asks a matting/segmentation backend to cut the
// subject out of an image. Zero values mean "backend default" (ADR-0020).
type BackgroundRemovalRequest struct {
// Image is the image to process. Required.
Image Image
// Net selects the remover's internal network where the backend offers
// several (rembg: "u2net", "birefnet-general", "isnet-general-use", ...);
// "" = backend default. This is NOT the provider model id — that is fixed
// when the BackgroundRemover is minted.
Net string
// OnlyMask returns the black/white segmentation mask instead of the
// cutout — WHITE marks the SUBJECT. To inpaint (repaint) the subject,
// use it directly as EditRequest.Mask; to repaint the BACKGROUND,
// invert it first (EditRequest.Mask is white-means-repaint).
OnlyMask bool
}
// BackgroundRemovalOption mutates a BackgroundRemovalRequest before it is sent.
type BackgroundRemovalOption func(*BackgroundRemovalRequest)
// WithBackgroundNet selects the remover's internal network.
func WithBackgroundNet(n string) BackgroundRemovalOption {
return func(r *BackgroundRemovalRequest) { r.Net = n }
}
// WithOnlyMask requests the segmentation mask instead of the cutout.
func WithOnlyMask() BackgroundRemovalOption {
return func(r *BackgroundRemovalRequest) { r.OnlyMask = true }
}
// Apply returns a copy of the request with all options applied.
func (r BackgroundRemovalRequest) Apply(opts ...BackgroundRemovalOption) BackgroundRemovalRequest {
for _, opt := range opts {
opt(&r)
}
return r
}
// BackgroundRemover cuts subjects out of images (RGBA PNG with transparent
// background, or the mask alone with OnlyMask).
type BackgroundRemover interface {
// RemoveBackground returns the cutout (or mask) as a one-image Result.
RemoveBackground(ctx context.Context, req BackgroundRemovalRequest, opts ...BackgroundRemovalOption) (*Result, error)
}
// BackgroundRemovalModelOption configures a BackgroundRemover at construction
// time. Reserved for future per-model settings.
type BackgroundRemovalModelOption func(*BackgroundRemovalModelConfig)
// BackgroundRemovalModelConfig carries per-model construction settings.
type BackgroundRemovalModelConfig struct{}
// ApplyBackgroundRemovalModelOptions folds options into a config.
func ApplyBackgroundRemovalModelOptions(opts []BackgroundRemovalModelOption) BackgroundRemovalModelConfig {
var cfg BackgroundRemovalModelConfig
for _, opt := range opts {
opt(&cfg)
}
return cfg
}
// BackgroundRemovalProvider mints BackgroundRemovers bound to one backend.
type BackgroundRemovalProvider interface {
// Name is the registry identifier for the provider.
Name() string
// BackgroundRemovalModel returns a BackgroundRemover bound to the given
// id (passed through to the backend verbatim; no catalog validation).
BackgroundRemovalModel(id string, opts ...BackgroundRemovalModelOption) (BackgroundRemover, error)
}