Remove the code nothing in production calls (closes #312)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
Service.Warm, the ImageCache interface, both GenerateSignedURL methods, signature.ParseParams, imagerequest.ParseImagePath, four symbols of internal/magic, the image processor's lists of supported formats, IsEmpty and Count of the allowlist, ssrfSafeDialer, CacheStale, ErrCacheMiss, ErrNegativeCache, ImageResponse.LastModified and the Load methods of ContentStorage and VariantStorage had no caller outside tests. They are gone, with the tests that tested only them. The handlers tests sign their image URLs with generateSignedURL, a test helper that builds the URL as the removed generator did. The SSRF dialer tests call dialSSRFSafe with no operator blocklist, which is all ssrfSafeDialer did. The tests of ParseImagePath and ContentStorage.Load call ParseImageURL and LoadWithSize instead. MIMEToImageFormat, ContentStorage.Store and CleanExpired stay, as pixa calls them. Model: opus-5-5
This commit was merged in pull request #329.
This commit is contained in:
@@ -37,22 +37,13 @@ func TestDetectFormatJPEGXL(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestJPEGXLMIMEType verifies that image/jxl is a supported input type and
|
||||
// maps to and from the format jxl.
|
||||
// TestJPEGXLMIMEType verifies that image/jxl maps to the format jxl.
|
||||
func TestJPEGXLMIMEType(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if !IsSupportedMIMEType(testMIMEJXL) {
|
||||
t.Errorf("IsSupportedMIMEType(%q) = false", testMIMEJXL)
|
||||
}
|
||||
|
||||
format, ok := MIMEToImageFormat(testMIMEJXL)
|
||||
if format != FormatJXL || !ok {
|
||||
t.Errorf("MIMEToImageFormat(%q) = %q, %v, want %q, true",
|
||||
testMIMEJXL, format, ok, FormatJXL)
|
||||
}
|
||||
|
||||
if got := ImageFormatToMIME(FormatJXL); got != testMIMEJXL {
|
||||
t.Errorf("ImageFormatToMIME(%q) = %q, want %q", FormatJXL, got, testMIMEJXL)
|
||||
}
|
||||
}
|
||||
|
||||
+6
-71
@@ -5,7 +5,6 @@ package magic
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -36,22 +35,17 @@ type ImageFormat string
|
||||
|
||||
// Supported image output formats.
|
||||
const (
|
||||
FormatOriginal ImageFormat = "orig"
|
||||
FormatJPEG ImageFormat = "jpeg"
|
||||
FormatPNG ImageFormat = "png"
|
||||
FormatWebP ImageFormat = "webp"
|
||||
FormatAVIF ImageFormat = "avif"
|
||||
FormatJXL ImageFormat = "jxl"
|
||||
FormatGIF ImageFormat = "gif"
|
||||
FormatJPEG ImageFormat = "jpeg"
|
||||
FormatPNG ImageFormat = "png"
|
||||
FormatWebP ImageFormat = "webp"
|
||||
FormatAVIF ImageFormat = "avif"
|
||||
FormatJXL ImageFormat = "jxl"
|
||||
FormatGIF ImageFormat = "gif"
|
||||
)
|
||||
|
||||
// MinMagicBytes is the minimum number of bytes needed to detect format.
|
||||
const MinMagicBytes = 12
|
||||
|
||||
// mimeOctetStream is the fallback MIME type for formats without a
|
||||
// specific MIME type.
|
||||
const mimeOctetStream = "application/octet-stream"
|
||||
|
||||
// Magic byte signatures for supported formats.
|
||||
// These are effectively constants but Go doesn't support const slices.
|
||||
//
|
||||
@@ -190,42 +184,6 @@ func normalizeMIMEType(mimeType string) string {
|
||||
return strings.TrimSpace(strings.ToLower(mimeType))
|
||||
}
|
||||
|
||||
// IsSupportedMIMEType checks if a MIME type is supported for input.
|
||||
func IsSupportedMIMEType(mimeType string) bool {
|
||||
normalized := normalizeMIMEType(mimeType)
|
||||
switch MIMEType(normalized) {
|
||||
case MIMETypeJPEG, MIMETypePNG, MIMETypeWebP, MIMETypeGIF, MIMETypeAVIF,
|
||||
MIMETypeJXL, MIMETypeSVG:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// PeekAndValidate reads the minimum bytes needed for format detection,
|
||||
// validates against the declared type, and returns a reader that includes
|
||||
// those bytes for subsequent reading.
|
||||
func PeekAndValidate(r io.Reader, declaredType string) (io.Reader, error) {
|
||||
// Read minimum bytes for detection
|
||||
buf := make([]byte, MinMagicBytes)
|
||||
|
||||
n, err := io.ReadFull(r, buf)
|
||||
if err != nil && !errors.Is(err, io.ErrUnexpectedEOF) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf = buf[:n]
|
||||
|
||||
// Validate magic bytes
|
||||
err = ValidateMagicBytes(buf, declaredType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Return a reader that includes the peeked bytes
|
||||
return io.MultiReader(bytes.NewReader(buf), r), nil
|
||||
}
|
||||
|
||||
// MIMEToImageFormat converts a MIME type to an ImageFormat.
|
||||
func MIMEToImageFormat(mimeType string) (ImageFormat, bool) {
|
||||
normalized := normalizeMIMEType(mimeType)
|
||||
@@ -249,26 +207,3 @@ func MIMEToImageFormat(mimeType string) (ImageFormat, bool) {
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
// ImageFormatToMIME converts an ImageFormat to a MIME type string.
|
||||
func ImageFormatToMIME(format ImageFormat) string {
|
||||
switch format {
|
||||
case FormatJPEG:
|
||||
return string(MIMETypeJPEG)
|
||||
case FormatPNG:
|
||||
return string(MIMETypePNG)
|
||||
case FormatWebP:
|
||||
return string(MIMETypeWebP)
|
||||
case FormatGIF:
|
||||
return string(MIMETypeGIF)
|
||||
case FormatAVIF:
|
||||
return string(MIMETypeAVIF)
|
||||
case FormatJXL:
|
||||
return string(MIMETypeJXL)
|
||||
case FormatOriginal:
|
||||
// Original format passes content through unchanged.
|
||||
return mimeOctetStream
|
||||
default:
|
||||
return mimeOctetStream
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,6 @@ package magic
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -155,119 +152,6 @@ func TestValidateMagicBytes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsSupportedMIMEType(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
mimeType string
|
||||
want bool
|
||||
}{
|
||||
{testMIMEJPEG, true},
|
||||
{testMIMEPNG, true},
|
||||
{testMIMEWebP, true},
|
||||
{testMIMEGIF, true},
|
||||
{testMIMEAVIF, true},
|
||||
{"image/svg+xml", true},
|
||||
{"IMAGE/JPEG", true},
|
||||
{testMIMEJPEGParams, true},
|
||||
{"image/tiff", false},
|
||||
{"image/bmp", false},
|
||||
{mimeOctetStream, false},
|
||||
{"text/plain", false},
|
||||
{"", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.mimeType, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if got := IsSupportedMIMEType(tt.mimeType); got != tt.want {
|
||||
t.Errorf("IsSupportedMIMEType(%q) = %v, want %v", tt.mimeType, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPeekAndValidate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
jpegMagic := []byte{
|
||||
0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01,
|
||||
}
|
||||
pngMagic := []byte{
|
||||
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D,
|
||||
}
|
||||
jpegData := slices.Concat(jpegMagic, []byte("rest of jpeg data"))
|
||||
pngData := slices.Concat(pngMagic, []byte("rest of png data"))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
data []byte
|
||||
declaredType string
|
||||
wantErr bool
|
||||
wantData []byte
|
||||
}{
|
||||
{
|
||||
name: "valid JPEG",
|
||||
data: jpegData,
|
||||
declaredType: testMIMEJPEG,
|
||||
wantErr: false,
|
||||
wantData: jpegData,
|
||||
},
|
||||
{
|
||||
name: "valid PNG",
|
||||
data: pngData,
|
||||
declaredType: testMIMEPNG,
|
||||
wantErr: false,
|
||||
wantData: pngData,
|
||||
},
|
||||
{
|
||||
name: "mismatched type",
|
||||
data: jpegData,
|
||||
declaredType: testMIMEPNG,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
r := bytes.NewReader(tt.data)
|
||||
|
||||
result, err := PeekAndValidate(r, tt.declaredType)
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Error("PeekAndValidate() expected error, got nil")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("PeekAndValidate() unexpected error = %v", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Read all data from result reader
|
||||
got, err := io.ReadAll(result)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to read result: %v", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if !bytes.Equal(got, tt.wantData) {
|
||||
t.Errorf(
|
||||
"PeekAndValidate() data mismatch: got %d bytes, want %d bytes",
|
||||
len(got), len(tt.wantData),
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMIMEToImageFormat(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -303,35 +187,6 @@ func TestMIMEToImageFormat(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageFormatToMIME(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
format ImageFormat
|
||||
wantMIME string
|
||||
}{
|
||||
{FormatJPEG, testMIMEJPEG},
|
||||
{FormatPNG, testMIMEPNG},
|
||||
{FormatWebP, testMIMEWebP},
|
||||
{FormatGIF, testMIMEGIF},
|
||||
{FormatAVIF, testMIMEAVIF},
|
||||
{FormatOriginal, mimeOctetStream},
|
||||
{"unknown", mimeOctetStream},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(string(tt.format), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := ImageFormatToMIME(tt.format)
|
||||
|
||||
if got != tt.wantMIME {
|
||||
t.Errorf("ImageFormatToMIME(%q) = %v, want %v", tt.format, got, tt.wantMIME)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeMIMEType(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -493,34 +348,3 @@ func TestDetectFormatFtypNotAVIF(t *testing.T) {
|
||||
t.Errorf("DetectFormat(MP4) error = %v, want %v", err, ErrUnknownFormat)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPeekAndValidatePreservesReader(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Ensure that after PeekAndValidate, we can read the complete
|
||||
// original content
|
||||
originalContent := append(
|
||||
[]byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D},
|
||||
[]byte(strings.Repeat("PNG IDAT chunk data here ", 100))...,
|
||||
)
|
||||
|
||||
r := bytes.NewReader(originalContent)
|
||||
|
||||
validated, err := PeekAndValidate(r, testMIMEPNG)
|
||||
if err != nil {
|
||||
t.Fatalf("PeekAndValidate() error = %v", err)
|
||||
}
|
||||
|
||||
// Read everything from the validated reader
|
||||
got, err := io.ReadAll(validated)
|
||||
if err != nil {
|
||||
t.Fatalf("io.ReadAll() error = %v", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(got, originalContent) {
|
||||
t.Errorf(
|
||||
"Content mismatch: got %d bytes, want %d bytes",
|
||||
len(got), len(originalContent),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user