fix: guard against division by zero when fetchBytes is 0

processAndStore() computed sizePercent as outputSize/fetchBytes*100
without checking for zero, producing Inf/NaN in logs and metrics.

Also treat empty cached source data the same as missing (re-fetch
from upstream) since zero-byte images can't be processed.

Fixes #5
This commit is contained in:
clawbot
2026-02-08 15:59:51 -08:00
parent be293906bc
commit 79ceed2ee4
2 changed files with 50 additions and 3 deletions

View File

@@ -153,8 +153,8 @@ func (s *Service) processFromSourceOrFetch(
}
}
// Fetch from upstream if we don't have source data
if sourceData == nil {
// Fetch from upstream if we don't have source data or it's empty
if len(sourceData) == 0 {
resp, err := s.fetchAndProcess(ctx, req, cacheKey)
if err != nil {
return nil, err
@@ -264,7 +264,11 @@ func (s *Service) processAndStore(
// Log conversion details
outputSize := int64(len(processedData))
sizePercent := float64(outputSize) / float64(fetchBytes) * 100.0 //nolint:mnd // percentage calculation
var sizePercent float64
if fetchBytes > 0 {
sizePercent = float64(outputSize) / float64(fetchBytes) * 100.0 //nolint:mnd // percentage calculation
}
s.log.Info("image converted",
"host", req.SourceHost,