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
@@ -85,7 +85,7 @@ func TestEvictSourceBlob_LeavesSourceWithIdenticalBytesServed(t *testing.T) {
t.Errorf("the evicted source still has %d source_content rows", n)
}
reader, _, err := cache.Source(t.Context(),
reader, _, _, err := cache.Source(t.Context(),
imagerequest.Source{Host: testHostCDN, Path: "/kept.jpg"})
t.Logf("Source() of the kept source: error %v", err)
@@ -122,7 +122,7 @@ func TestPutSource_FailedRowWriteLeavesNoFile(t *testing.T) {
req := &imagerequest.Request{
Source: imagerequest.Source{Host: testHostCDN, Path: testPathCat},
}
key, err := cache.PutSource(t.Context(), req.Source,
key, err := cache.PutSource(t.Context(), req.Source, imagerequest.FormatJPEG,
bytes.NewReader([]byte("source bytes")))
t.Logf("PutSource() = %q, error = %v", key, err)
@@ -135,3 +135,37 @@ func TestPutSource_FailedRowWriteLeavesNoFile(t *testing.T) {
t.Error("the source's file is on disk although its row was not written")
}
}
// TestSource_ReturnsTheFormatPutSourceStored stores a source as PNG, then
// the same source URL again as WebP. After each store, Source must return
// the format that store gave.
func TestSource_ReturnsTheFormatPutSourceStored(t *testing.T) {
t.Parallel()
cache, _ := newEvictionTestCache(t, 1<<30)
src := imagerequest.Source{Host: testHostCDN, Path: testPathCat}
for _, format := range []imagerequest.Format{
imagerequest.FormatPNG, imagerequest.FormatWebP,
} {
_, err := cache.PutSource(t.Context(), src, format,
bytes.NewReader([]byte("source bytes")))
if err != nil {
t.Fatalf("PutSource(%s) error = %v", format, err)
}
reader, _, got, err := cache.Source(t.Context(), src)
if err != nil {
t.Fatalf("Source() after PutSource(%s) error = %v", format, err)
}
_ = reader.Close()
t.Logf("Source() after PutSource(%s): format %s", format, got)
if got != format {
t.Errorf("Source() after PutSource(%s) format = %q, want %q",
format, got, format)
}
}
}