Detect a fetched image's format from its bytes once (closes #319)
check / check (push) Canceled after 0s
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:
+31
-40
@@ -175,7 +175,7 @@ func (c *Cache) Variant(
|
||||
return nil, 0, "", err
|
||||
}
|
||||
|
||||
contentType, err := outputContentType(file)
|
||||
format, err := readFormat(file)
|
||||
if err != nil {
|
||||
_ = file.Close()
|
||||
|
||||
@@ -184,7 +184,7 @@ func (c *Cache) Variant(
|
||||
|
||||
c.touchVariant(cacheKey)
|
||||
|
||||
return file, size, contentType, nil
|
||||
return file, size, format.MIMEType(), nil
|
||||
}
|
||||
|
||||
// PutVariant stores a processed variant by its cache key and records
|
||||
@@ -227,44 +227,46 @@ func (c *Cache) PutVariant(
|
||||
return nil
|
||||
}
|
||||
|
||||
// Source opens the cached source of src and returns a reader and its size
|
||||
// in bytes. A source whose row or file is missing is ErrNotFound, as is any
|
||||
// source of a disabled cache. A source it opens has its access time handed
|
||||
// to the background writer without waiting (see touchSourceContent).
|
||||
// Source opens the cached source of src and returns a reader, its size in
|
||||
// bytes and its format, the one PutSource stored with it. A source whose row
|
||||
// or file is missing is ErrNotFound, as is any source of a disabled cache. A
|
||||
// source it opens has its access time handed to the background writer
|
||||
// without waiting (see touchSourceContent).
|
||||
func (c *Cache) Source(
|
||||
ctx context.Context, src imagerequest.Source,
|
||||
) (io.ReadCloser, int64, error) {
|
||||
) (io.ReadCloser, int64, imagerequest.Format, error) {
|
||||
if c.config.MaxBytes == 0 {
|
||||
return nil, 0, ErrNotFound
|
||||
return nil, 0, "", ErrNotFound
|
||||
}
|
||||
|
||||
key := src.Key()
|
||||
|
||||
var rowExists bool
|
||||
var format imagerequest.Format
|
||||
|
||||
err := c.db.QueryRowContext(ctx, `
|
||||
SELECT EXISTS(SELECT 1 FROM source_content WHERE source_key = ?)
|
||||
`, string(key)).Scan(&rowExists)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("failed to lookup source: %w", err)
|
||||
SELECT format FROM source_content WHERE source_key = ?
|
||||
`, string(key)).Scan(&format)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, 0, "", ErrNotFound
|
||||
}
|
||||
|
||||
if !rowExists {
|
||||
return nil, 0, ErrNotFound
|
||||
if err != nil {
|
||||
return nil, 0, "", fmt.Errorf("failed to lookup source: %w", err)
|
||||
}
|
||||
|
||||
file, size, err := c.srcContent.Open(string(key))
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, "", err
|
||||
}
|
||||
|
||||
c.touchSourceContent(key)
|
||||
|
||||
return file, size, nil
|
||||
return file, size, format, nil
|
||||
}
|
||||
|
||||
// PutSource stores fetched source content: it writes the file named by
|
||||
// the source's key, then the source's row. When the row cannot be written
|
||||
// PutSource stores fetched source content and its format, which magic
|
||||
// detected from its first bytes. It writes the file named by the source's
|
||||
// key, then the source's row with the format. When the row cannot be written
|
||||
// the file is removed and the store fails, as a file with no row is never
|
||||
// served or evicted; if that removal fails too, the next reconciliation
|
||||
// pass removes the file. On a disabled cache it is a no-op returning an
|
||||
@@ -272,6 +274,7 @@ func (c *Cache) Source(
|
||||
func (c *Cache) PutSource(
|
||||
ctx context.Context,
|
||||
src imagerequest.Source,
|
||||
format imagerequest.Format,
|
||||
content io.Reader,
|
||||
) (imagerequest.SourceKey, error) {
|
||||
if c.config.MaxBytes == 0 {
|
||||
@@ -293,12 +296,13 @@ func (c *Cache) PutSource(
|
||||
size := int64(len(data))
|
||||
|
||||
_, err = c.db.ExecContext(ctx, `
|
||||
INSERT INTO source_content (source_key, size_bytes)
|
||||
VALUES (?, ?)
|
||||
INSERT INTO source_content (source_key, size_bytes, format)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT(source_key) DO UPDATE SET
|
||||
size_bytes = excluded.size_bytes,
|
||||
format = excluded.format,
|
||||
last_accessed_at = CURRENT_TIMESTAMP
|
||||
`, string(key), size)
|
||||
`, string(key), size, string(format))
|
||||
if err != nil {
|
||||
deleteErr := c.srcContent.Delete(string(key))
|
||||
if deleteErr != nil {
|
||||
@@ -440,13 +444,10 @@ func (c *Cache) cleanExpiredBatch(ctx context.Context) (int64, error) {
|
||||
return deleted, nil
|
||||
}
|
||||
|
||||
// errNotAnOutputFormat is returned for a variant file whose first bytes are
|
||||
// those of a format pixa reads but never writes, such as SVG.
|
||||
var errNotAnOutputFormat = errors.New("variant file is not in an output format")
|
||||
|
||||
// outputContentType reads the first bytes of content, without moving its
|
||||
// offset, and returns the content type of the output format they show.
|
||||
func outputContentType(content io.ReaderAt) (string, error) {
|
||||
// readFormat reads the first bytes of content, without moving its offset,
|
||||
// and returns the format they show. pixa writes each format it reads, so
|
||||
// that is an output format.
|
||||
func readFormat(content io.ReaderAt) (imagerequest.Format, error) {
|
||||
firstBytes := make([]byte, magic.MinMagicBytes)
|
||||
|
||||
_, err := content.ReadAt(firstBytes, 0)
|
||||
@@ -454,15 +455,5 @@ func outputContentType(content io.ReaderAt) (string, error) {
|
||||
return "", fmt.Errorf("failed to read the first bytes: %w", err)
|
||||
}
|
||||
|
||||
detected, err := magic.DetectFormat(firstBytes)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, isOutputFormat := magic.MIMEToImageFormat(string(detected))
|
||||
if !isOutputFormat {
|
||||
return "", fmt.Errorf("%w: %s", errNotAnOutputFormat, detected)
|
||||
}
|
||||
|
||||
return string(detected), nil
|
||||
return magic.DetectFormat(firstBytes)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user