Files
MealQuest/internal/server/media.go
T
dev_agentandClaude Opus 5.5 70f545152a
CI / ci (push) Successful in 1m37s
CI / ci (pull_request) Successful in 1m37s
Validate decoded photo size and require a photos row to serve
decodePhoto checked the GIF logical screen size, but gif.Decode returns
the first frame, which may be smaller; a 400x400 screen with a 1x1 first
frame passed and was scaled. The short-side check now measures the
decoded image's bounds. The pixel cap still comes from the header, before
decoding.

OpenPhoto served any file whose name was a canonical ULID and stored
size, with no photos row required. It now looks the id up with the new
GetPhoto query and reports an id with no row as fs.ErrNotExist, so
GET /media/photos/{id}-{size}.jpg is 404 for it even when a file exists.
OpenPhoto takes the request context for the lookup.

The media test fixture records a photos row for its served photo and
adds an orphan photo whose files exist without a row; both its sizes
must be 404.

Maestro task: 8a432108-d3ba-4013-ae8c-11963981d641

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 14:10:54 -04:00

76 lines
2.2 KiB
Go

package server
import (
"context"
"errors"
"io/fs"
"log/slog"
"net/http"
"os"
"strconv"
"strings"
"github.com/go-chi/chi/v5"
)
// photoCacheControl lets the browser keep a photo for a year without
// revalidating: a photo's files never change once stored, and a new image
// gets a new id.
const photoCacheControl = "private, max-age=31536000, immutable"
// PhotoFiles opens a cached photo's file at one of its stored sizes. An
// unknown photo or size is an error matching fs.ErrNotExist.
type PhotoFiles interface {
OpenPhoto(ctx context.Context, id string, size int) (*os.File, error)
}
// getPhoto answers GET /media/photos/{id}-{size}.jpg with the stored JPEG.
func getPhoto(logger *slog.Logger, photos PhotoFiles) http.HandlerFunc {
notFound := apiErrorHandler(logger, http.StatusNotFound, "not_found", "not found")
return func(w http.ResponseWriter, r *http.Request) {
id, size, ok := parsePhotoName(chi.URLParam(r, "name"))
if !ok {
notFound(w, r)
return
}
f, err := photos.OpenPhoto(r.Context(), id, size)
if errors.Is(err, fs.ErrNotExist) {
notFound(w, r)
return
}
if err != nil {
logger.ErrorContext(r.Context(), "open photo", "id", id, "size", size, "error", err)
writeError(w, r, logger, http.StatusInternalServerError, "internal", "internal error")
return
}
defer f.Close()
info, err := f.Stat()
if err != nil {
logger.ErrorContext(r.Context(), "stat photo", "id", id, "size", size, "error", err)
writeError(w, r, logger, http.StatusInternalServerError, "internal", "internal error")
return
}
w.Header().Set("Content-Type", "image/jpeg")
w.Header().Set("Cache-Control", photoCacheControl)
http.ServeContent(w, r, "", info.ModTime(), f)
}
}
// parsePhotoName splits "{id}-{size}.jpg" into its id and size, refusing
// a size not written in its canonical decimal form.
func parsePhotoName(name string) (id string, size int, ok bool) {
stem, ok := strings.CutSuffix(name, ".jpg")
if !ok {
return "", 0, false
}
i := strings.LastIndexByte(stem, '-')
if i < 0 {
return "", 0, false
}
size, err := strconv.Atoi(stem[i+1:])
if err != nil || strconv.Itoa(size) != stem[i+1:] {
return "", 0, false
}
return stem[:i], size, true
}