Remove the code nothing in production calls (closes #312)
check / check (push) Waiting to run

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 is contained in:
2026-10-26 09:40:35 +00:00
parent b49d0068df
commit 5a2aa94691
27 changed files with 187 additions and 1017 deletions
-6
View File
@@ -14,12 +14,6 @@ import (
"sneak.berlin/go/pixa/internal/imagerequest"
)
// Cache errors.
var (
ErrCacheMiss = errors.New("cache miss")
ErrNegativeCache = errors.New("negative cache hit")
)
// maxPendingAccessTimes is how many access times of variants served from
// the cache, and how many of cached sources a miss used, may each wait to be
// written, at about 80 bytes each. An access time handed over while that
+5 -23
View File
@@ -1,13 +1,7 @@
// Package imgcache provides interfaces and types for the image caching proxy.
// Package imgcache provides types for the image caching proxy.
package imgcache
import (
"context"
"io"
"time"
"sneak.berlin/go/pixa/internal/imagerequest"
)
import "io"
// ImageResponse represents a processed image ready to serve
type ImageResponse struct {
@@ -19,9 +13,7 @@ type ImageResponse struct {
ContentType string
// ETag is the entity tag for caching
ETag string
// LastModified is when the content was last modified
LastModified time.Time
// CacheStatus indicates HIT, MISS, or STALE
// CacheStatus indicates HIT or MISS
CacheStatus CacheStatus
// FetchedBytes is the number of bytes fetched from upstream (0 if cache hit)
FetchedBytes int64
@@ -32,16 +24,6 @@ type CacheStatus string
// Cache status values for response headers.
const (
CacheHit CacheStatus = "HIT"
CacheMiss CacheStatus = "MISS"
CacheStale CacheStatus = "STALE"
CacheHit CacheStatus = "HIT"
CacheMiss CacheStatus = "MISS"
)
// ImageCache is the main interface for the image caching proxy
type ImageCache interface {
// Get retrieves a processed image, fetching and processing if necessary
Get(ctx context.Context, req *imagerequest.Request) (*ImageResponse, error)
// Warm pre-fetches and caches an image without returning it
Warm(ctx context.Context, req *imagerequest.Request) error
}
@@ -290,9 +290,9 @@ func TestContentStorage_StoreReplacesFileOfAnotherSize(t *testing.T) {
t.Fatalf("Store() error = %v", err)
}
stored, err := storage.Load(key)
stored, _, err := storage.LoadWithSize(key)
if err != nil {
t.Fatalf("Load() error = %v", err)
t.Fatalf("LoadWithSize() error = %v", err)
}
defer func() { _ = stored.Close() }()
+1 -33
View File
@@ -24,8 +24,7 @@ import (
"sneak.berlin/go/pixa/internal/signature"
)
// Service implements the ImageCache interface, orchestrating cache,
// fetcher, and processor.
// Service orchestrates the cache, fetcher, and processor.
type Service struct {
cache *Cache
fetcher httpfetcher.Fetcher
@@ -189,13 +188,6 @@ func (s *Service) Get(
return response, nil
}
// Warm pre-fetches and caches an image without returning it.
func (s *Service) Warm(ctx context.Context, req *imagerequest.Request) error {
_, err := s.Get(ctx, req)
return err
}
// WaitForProcessing waits until no image is being processed, or until ctx
// ends, and returns how many images were still being processed then.
func (s *Service) WaitForProcessing(ctx context.Context) int {
@@ -229,30 +221,6 @@ func (s *Service) ValidateRequest(req *imagerequest.Request) error {
return s.signer.Verify(req)
}
// GenerateSignedURL generates a signed URL for the given request. The URL
// carries q and fit next to sig and exp, so the image route verifies it for
// the quality and fit it was signed with. An unset quality or fit is first
// set to 85 or cover, the values the route uses when a URL has no q or fit.
func (s *Service) GenerateSignedURL(
baseURL string,
req *imagerequest.Request,
ttl time.Duration,
) (string, error) {
if req.Transform.Quality == 0 {
req.Transform.Quality = 85
}
if req.Transform.FitMode == "" {
req.Transform.FitMode = imagerequest.FitCover
}
// Sets the request's Expires and Signature
path, sig, exp := s.signer.GenerateSignedURL(req, ttl)
return fmt.Sprintf("%s%s?sig=%s&exp=%d&q=%d&fit=%s",
baseURL, path, sig, exp, req.Transform.Quality, req.Transform.FitMode), nil
}
// errPanicked is returned when processing a variant panicked.
var errPanicked = errors.New("panic while processing image")
-32
View File
@@ -62,22 +62,6 @@ func (s *ContentStorage) Store(
return int64(len(data)), nil
}
// Load returns a reader for the content with the given key.
func (s *ContentStorage) Load(key imagerequest.SourceKey) (io.ReadCloser, error) {
path := s.keyToPath(key)
f, err := os.Open(path) //nolint:gosec // path derived from source key
if err != nil {
if os.IsNotExist(err) {
return nil, ErrNotFound
}
return nil, fmt.Errorf("failed to open content: %w", err)
}
return f, nil
}
// LoadWithSize returns a reader and file size for the content with the given key.
func (s *ContentStorage) LoadWithSize(
key imagerequest.SourceKey,
@@ -251,22 +235,6 @@ func (s *VariantStorage) Store(
return int64(len(data)), nil
}
// Load returns a reader for the content at the given key.
func (s *VariantStorage) Load(key imagerequest.VariantKey) (io.ReadCloser, error) {
path := s.keyToPath(key)
f, err := os.Open(path) //nolint:gosec // path derived from cache key
if err != nil {
if os.IsNotExist(err) {
return nil, ErrNotFound
}
return nil, fmt.Errorf("failed to open content: %w", err)
}
return f, nil
}
// LoadWithContentType opens the content at the given key and returns a
// reader, the file size and the content type, which is read from the
// file's first bytes. A missing file is ErrNotFound. A file whose first
+7 -7
View File
@@ -11,7 +11,7 @@ import (
"sneak.berlin/go/pixa/internal/imagerequest"
)
func TestContentStorage_StoreAndLoad(t *testing.T) {
func TestContentStorage_StoreAndLoadWithSize(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
@@ -44,9 +44,9 @@ func TestContentStorage_StoreAndLoad(t *testing.T) {
}
// Load and verify content
r, err := storage.Load(key)
r, _, err := storage.LoadWithSize(key)
if err != nil {
t.Fatalf("Load() error = %v", err)
t.Fatalf("LoadWithSize() error = %v", err)
}
defer func() { _ = r.Close() }()
@@ -57,11 +57,11 @@ func TestContentStorage_StoreAndLoad(t *testing.T) {
}
if !bytes.Equal(loaded, content) {
t.Errorf("Load() content = %q, want %q", loaded, content)
t.Errorf("LoadWithSize() content = %q, want %q", loaded, content)
}
}
func TestContentStorage_LoadNotFound(t *testing.T) {
func TestContentStorage_LoadWithSizeNotFound(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
@@ -71,9 +71,9 @@ func TestContentStorage_LoadNotFound(t *testing.T) {
t.Fatalf("NewContentStorage() error = %v", err)
}
_, err = storage.Load(imagerequest.SourceKey("nonexistent"))
_, _, err = storage.LoadWithSize(imagerequest.SourceKey("nonexistent"))
if !errors.Is(err, ErrNotFound) {
t.Errorf("Load() error = %v, want ErrNotFound", err)
t.Errorf("LoadWithSize() error = %v, want ErrNotFound", err)
}
}