Detect a fetched image's format from its bytes once (closes #319)
check / check (push) Canceled after 0s

magic.DetectFormat returns an imagerequest.Format. ValidateMagicBytes
returns the format it checked against the upstream Content-Type. The
cache stores that format in the source's row, so a variant made from a
cached source uses it too. The proxy hands it to Process, which still
refuses input libvips would load with another loader, such as an SVG
passing as AVIF. Format.MIMEType declares each MIME string once, and the
fetcher, the cache's variant read and the Accept matching use it.
magic.MIMEType, magic.ImageFormat, MIMEToImageFormat, magic's SVG
detection, detectFormat, formatFromString, FormatToMIME and
Result.InputFormat are gone.

Model: opus-5-5
This commit is contained in:
2026-10-26 19:46:01 +00:00
parent ac850823f4
commit adfb6ab50a
39 changed files with 430 additions and 594 deletions
+8 -5
View File
@@ -79,8 +79,9 @@ func newEvictionTestCache(t *testing.T, maxBytes int64) (*Cache, string) {
return cache, tmpDir
}
// storeEvictionTestSource stores content as a fetched source for
// host/path and returns the resulting source key.
// storeEvictionTestSource stores content as a fetched JPEG source for
// host/path and returns the resulting source key. The cache does not read
// the content, so it need not be a JPEG.
func storeEvictionTestSource(
t *testing.T, cache *Cache, host, path string, content []byte,
) imagerequest.SourceKey {
@@ -99,7 +100,8 @@ func storeEvictionTestSource(
}
key, err := cache.PutSource(
context.Background(), req.Source, bytes.NewReader(content),
context.Background(), req.Source, imagerequest.FormatJPEG,
bytes.NewReader(content),
)
if err != nil {
t.Fatalf("PutSource(%s%s) failed: %v", host, path, err)
@@ -436,7 +438,8 @@ func assertDisabledCacheWritesAreNoOps(
t.Fatalf("PutVariant on disabled cache must be a no-op, got error: %v", err)
}
key, err := cache.PutSource(ctx, req.Source, bytes.NewReader([]byte("data")))
key, err := cache.PutSource(ctx, req.Source, imagerequest.FormatJPEG,
bytes.NewReader([]byte("data")))
if err != nil {
t.Fatalf("PutSource on disabled cache must be a no-op, got error: %v", err)
}
@@ -455,7 +458,7 @@ func assertDisabledCacheReadsAlwaysMiss(
ctx := t.Context()
reader, _, err := cache.Source(ctx, req.Source)
reader, _, _, err := cache.Source(ctx, req.Source)
if !errors.Is(err, ErrNotFound) {
t.Fatalf("Source on disabled cache error = %v, want %v", err, ErrNotFound)
}