Mirror external images into our own storage so they render
Bar items added by barcode stored the Open Food Facts image URL directly. The CSP in next.config.mjs restricts img-src to our own origin, so the browser blocked those and showed a broken image - the picture was fine, we just could not display it. Copy externally-hosted images into MinIO at lookup time and hand back a /minio-images path instead. That fixes the class rather than the instance: no CSP entry is needed per image source, the picture survives the source deleting or reorganising it, and the user's browser never has to talk to a third party to render their own bar. Also fixes a latent bug this uncovered: imageUrl was validated with z.string().url(), which rejects the relative /minio-images/... paths that uploadImage returns, so saving an uploaded drink image would fail validation. That matches production having zero drinks with an image. Both schemas now accept either form. CSP keeps two third-party entries for OAuth avatars, which the provider hosts and we only ever receive as a URL at sign-in. Existing rows were backfilled separately. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import { auth } from "@/lib/auth"
|
||||
import { prisma } from "@/lib/prisma"
|
||||
import { getUserProvider } from "@/lib/ai/provider-factory"
|
||||
import { FEATURE_ROUTING } from "@/lib/ai/routing"
|
||||
import { mirrorExternalImage } from "@/lib/s3"
|
||||
import { rateLimit } from "@/lib/rate-limit"
|
||||
import { z } from "zod"
|
||||
|
||||
@@ -42,8 +43,14 @@ async function lookupOpenFoodFacts(barcode: string) {
|
||||
const name = product.product_name || product.product_name_en || null
|
||||
if (!name) return null
|
||||
|
||||
// Extract product image URL
|
||||
const imageUrl = product.image_url || product.image_front_url || product.image_front_small_url || null
|
||||
// Open Food Facts hosts these on its own domain, which the CSP blocks, so copy
|
||||
// the picture into our storage and hand back a local URL. Null on failure - an
|
||||
// item with no image beats one with an image that cannot render.
|
||||
const remoteImage =
|
||||
product.image_url || product.image_front_url || product.image_front_small_url || null
|
||||
const imageUrl = remoteImage
|
||||
? await mirrorExternalImage(remoteImage, `bar/${barcode}`)
|
||||
: null
|
||||
|
||||
// Extract ABV from alcohol_100g nutrient or nutriments
|
||||
let abv: number | null = null
|
||||
|
||||
Reference in New Issue
Block a user