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:
@@ -1,5 +1,19 @@
|
||||
import { z } from "zod"
|
||||
|
||||
/**
|
||||
* An image reference. Uploaded and mirrored images are stored as relative paths
|
||||
* (`/minio-images/...`) served through the Next.js proxy, so a plain `.url()` check
|
||||
* rejects them - which silently broke saving any uploaded image. Absolute http(s)
|
||||
* URLs stay allowed for data restored from older backups.
|
||||
*/
|
||||
export const imageUrlSchema = z
|
||||
.string()
|
||||
.max(2048)
|
||||
.refine(
|
||||
(v) => v.startsWith("/minio-images/") || /^https?:\/\//i.test(v),
|
||||
"Must be an uploaded image path or an http(s) URL"
|
||||
)
|
||||
|
||||
export const drinkCreateSchema = z.object({
|
||||
name: z.string().min(1, "Name is required").max(200),
|
||||
type: z.enum(["BEER", "WINE", "COCKTAIL", "SPIRIT", "OTHER"]),
|
||||
@@ -8,7 +22,7 @@ export const drinkCreateSchema = z.object({
|
||||
region: z.string().max(200).optional(),
|
||||
abv: z.number().min(0).max(100).optional(),
|
||||
description: z.string().max(2000).optional(),
|
||||
imageUrl: z.string().url().optional(),
|
||||
imageUrl: imageUrlSchema.optional(),
|
||||
})
|
||||
|
||||
export const drinkUpdateSchema = drinkCreateSchema.partial()
|
||||
@@ -79,7 +93,7 @@ export const barItemCreateSchema = z.object({
|
||||
quantity: z.enum(["FULL", "HALF", "LOW", "EMPTY"]).default("FULL"),
|
||||
notes: z.string().max(2000).optional(),
|
||||
barcode: z.string().max(50).optional(),
|
||||
imageUrl: z.string().url().optional().or(z.literal("")),
|
||||
imageUrl: imageUrlSchema.optional().or(z.literal("")),
|
||||
})
|
||||
|
||||
export const barItemUpdateSchema = barItemCreateSchema.partial()
|
||||
|
||||
Reference in New Issue
Block a user