3.32 Contact Avatar Media Pipeline
Every contact in RAIA (Real estate Artificial Intelligence Agent) has a photo. Those photos live in a locked storage room (a private Google Cloud bucket). Because the room is locked, the portal can't just point at a photo directly — it has to ask a doorman (an API route) for a temporary key (a signed URL that expires in 15 minutes). The doorman hands the key to your browser, which fetches the photo. If anything goes wrong, the doorman hands back a grey silhouette instead, so the page never shows a broken image.
Every contact in RAIA (Real estate Artificial Intelligence Agent) has a photo. Those photos live in a locked storage room (a private Google Cloud bucket). Because the room is locked, the portal can't just point at a photo directly — it has to ask a doorman (an API route) for a temporary key (a signed URL that expires in 15 minutes). The doorman hands the key to your browser, which fetches the photo. If anything goes wrong, the doorman hands back a grey silhouette instead, so the page never shows a broken image.
How contact photos are stored in a private GCS bucket, signed on demand, and served to <img> tags — and how a single non-idempotent resolver caused the two-week "all avatars grey" incident. This page is the incident knowledge capture: every claim verified against src/lib/media.ts, src/app/api/persons/[id]/avatar/route.ts, and commit 057875dc.
Section A What Is the Avatar Pipeline?
When a landlord, tenant, or guarantor uploads a photo, RAIA (Real estate Artificial Intelligence Agent) does not put it on the public internet. The photo goes into a locked Google Cloud Storage bucket called raia-gb-private, and the database saves only the path to the file — like a coat-check ticket, not the coat itself. When a staff member opens a page showing that contact, the browser asks the portal's doorman route (/api/persons/<id>/avatar) for the photo. The doorman checks you're allowed to see it, swaps the ticket for a 15-minute temporary key (a V4 signed URL), and redirects your browser to fetch the photo. If the person has no photo, or anything breaks, the doorman hands back a built-in grey silhouette so the page still looks fine.
The Two-Week Grey Circles (Why This Page Exists)
In July 2026 every contact photo in the portal rendered as a grey silhouette for two weeks. Three diagnoses were wrong before the real one landed. The data was fine. The bucket was fine. The signing was fine. The bug was one function that wasn't idempotent: the API resolved the raw database value into a route path, and then a component resolved that route path again, rejected it (it doesn't look like an https URL or a storage path), and fell through to the silhouette. Every claim in the Developer section below is verified against the fix commit 057875dc so the next agent finds the answer in two minutes, not two weeks.
If a value can arrive at your function already resolved, the resolver must pass it through untouched. Resolution must be idempotent. Resolve(resolve(x)) must equal resolve(x).
Section B What You See in the Portal
Contact photos appear next to landlords, tenants, guarantors, and contractors throughout the portal — in list rows, detail headers, and the biography panel. There are exactly three visual states:
| What you see | What it means | Action |
|---|---|---|
| Contact's photo | Photo exists in the private bucket and was signed successfully | None — everything is working |
| Grey silhouette placeholder | The person has no usable photo: never uploaded one, or the record holds a legacy path from the old AppSheet system that no longer exists | Upload a new photo from the contact's detail page (staff only) |
| Person-icon (Lucide) instead of an image | The contact record has no photo value at all — the UI shows an icon, not an image tag | Cosmetic only; upload a photo if you want one |
Uploading Your Photo
-
1Open your profile or a contact you manage
Staff can upload for themselves via
/api/me/avatar/upload-url; contact photos go through the same two-step flow. -
2Request an upload URL, then PUT the file
The portal requests a short-lived (5-minute) signed write URL, uploads the file straight to GCS, then calls
/api/me/avatar/confirmwhich writes the bare object pathpersons/<uuid>/avatar.<ext>intotbl_persons.avatar_url. -
3Photo appears within one redirect
The next page load requests
/api/persons/<id>/avatar, receives a 302 to a 15-minute signed URL, and renders the photo. Redirects are cached privately for 5 minutes.
Why Some Photos Are Grey — And Why That's Expected
A grey silhouette is the designed fallback, not a bug. The route that serves avatars can never return an error to the browser — its only consumer is an <img> tag, which cannot display JSON. So every failure mode (no photo, dead legacy path, signing error, permission denied) resolves to the same grey circle. This is deliberate: the UI degrades to a placeholder instead of a broken-image icon.
If every avatar across the whole portal is grey — including people who definitely have photos — that is a system fault, not missing data. See the Developer debugging table below. The decisive first probe takes 30 seconds.
Section C Storage Model
tbl_persons.avatar_url stores a bare GCS object path, not a URL:
persons/<uuid>/avatar.(jpg|png|webp) — bucket: raia-gb-private (PRIVATE)
The bucket has no public read access, so a storage.googleapis.com URL would 403. That is why a read route exists at all. Three value shapes exist in production data:
| Value shape in avatar_url | Origin | Resolution behavior |
|---|---|---|
persons/<uuid>/avatar.<ext> |
Current upload flow (/api/me/avatar/upload-url + confirm) |
Routed through /api/persons/<id>/avatar → V4 signed → 302 |
https://… (Cloudinary, LinkedIn, etc.) |
Legacy imports | isRenderableAvatar() → passed straight through, no signing needed |
Contact_Images/<uuid>.Thumb.… |
Dead AppSheet paths — never in our bucket | Deliberately rejected by isPrivateAvatarObjectPath() → silhouette. Signing them would produce a URL that 404s. |
isPrivateAvatarObjectPath() only matches paths the upload flow itself writes: /^persons\/[0-9a-f-]{36}\/avatar\.(jpg|png|webp)$/i. Only paths we wrote are worth resolving — everything else falls to the silhouette by design.
Read Path — GET /api/persons/[id]/avatar
-
1Validate the UUID
Non-UUID id → immediate 302 fallback to
AVATAR_DEFAULT. -
2
requireStaffAccessMust be an authenticated staff session; the guard's own response passes through untouched.
-
3BOLA org check
Administrators are cross-org. Everyone else may see their own photo, or a person sharing one of their organisations (
tbl_organisations_personslookup). Failure → silhouette, not 403. -
4Branch on the stored value
Legacy https → 302 straight to it. Private object path →
generateAvatarReadUrl()(V4, 15-min TTL, in-process cache bucketed at TTL/3, cache cleared at 500 entries). Anything else → silhouette. -
5Failure → 302 to silhouette, and LOG
Since PR #285 (
892afe66) a signing failure logs[avatar] signing failed for <path>. Swallowing it silently is what made a brokenGOOGLE_STORAGE_CREDENTIALS_JSONindistinguishable from "nobody has photos" for hours.
302 Location: https://storage.googleapis.com/raia-gb-private/persons/…?X-Goog-Signature=… Cache-Control: private, max-age=300 ← redirect cache < 15-min signature TTL (deliberate)
THE CRITICAL RULE — Resolution Must Be Idempotent
getContactAvatarUrl(personId, rawAvatarUrl) may receive either a raw DB value or an already-resolved API route path. Its pass-through order (commit 057875dc):
export function getContactAvatarUrl(personId, rawAvatarUrl) {
if (!personId) return AVATAR_DEFAULT;
// 1. already resolved by the API → pass through UNTOUCHED
if (rawAvatarUrl?.startsWith("/api/persons/")) return rawAvatarUrl;
// 2. legacy public https URL → use as-is
if (isRenderableAvatar(rawAvatarUrl)) return rawAvatarUrl;
// 3. private object path → point at the signing route
if (isPrivateAvatarObjectPath(rawAvatarUrl)) return `/api/persons/${personId}/avatar`;
// 4. everything else → silhouette
return AVATAR_DEFAULT;
}
The API resolved avatar_url server-side and handed components /api/persons/<uuid>/avatar. A component then called getContactAvatarUrl on that value again. The route path is not https, does not match the object-path regex, and did not have the pass-through branch — so it fell through to AVATAR_DEFAULT. Grey circles for every landlord despite a fully working data chain. The fix is branch 1 above; the commit message on 057875dc is the incident post-mortem.
Debugging Table — The Three Grey Circles
"Grey circle" has three distinct causes with identical visuals. Distinguish them in 60 seconds:
| Cause | Elements panel shows | Network panel shows | Meaning |
|---|---|---|---|
(a) Falsy avatar_url |
No <img> at all — a Lucide person icon SVG |
No avatar request | Component never rendered an image; the record has no value. Cosmetic. |
| (b) Resolution fell through | <img src="/images/avatar-default.svg"> |
No /api/persons/… request |
getContactAvatarUrl rejected the value client-side. This was the incident signature. |
| (c) Route fallback | <img src="/api/persons/<uuid>/avatar"> |
302 → /images/avatar-default.svg |
Server-side: no photo, dead path, BOLA denial, or signing failure (check logs for [avatar] signing failed). |
GET /api/landlords (authenticated) and read one avatar_url value. If the API already returns /api/persons/<uuid>/avatar and the browser still shows a silhouette without requesting that route, the bug is client-side double resolution — branch (b). If the route IS requested and 302s to the SVG, the bug is server-side — branch (c).
Anti-Patterns That Wasted Days
vercel env pull outputThe CLI escapes newlines when writing .env files. A private key full of \n literals in the pulled file is an artifact of the pull, not corruption in Vercel. Days were spent "fixing" credentials that were never broken. Verify credentials by signing a URL, not by inspecting the pulled file.
The avatar route requires staff auth. Curling it without a session tells you nothing except that the guard works — yet "the route 302s to the silhouette for me" was reported as a signing bug. Always probe with an authenticated session before concluding anything server-side.
The repo working directory may lag origin/main. The double-resolution bug was "confirmed present" in a worktree after it had already been fixed on main. First command of any audit: git fetch && git status && git log --oneline -1 origin/main -- <file>. Verify the branch before trusting the code in front of you.
Ops Facts & Upload Flow
| Fact | Value | Source |
|---|---|---|
| Upload endpoints | /api/me/avatar/upload-url (V4 signed write, 5-min) → PUT file → /api/me/avatar/confirm (writes bare path to DB) | src/app/api/me/avatar/ |
| Failure log line | [avatar] signing failed for <path> | PR #285 · 892afe66 |
| Route slug fix | Renamed avatar route slug to [id] — unbroke ALL API routes | PR #284 · 6c93495e |
| Redirect cache | Cache-Control: private, max-age=300 — deliberately below the 15-min signature TTL so a cached redirect never points at an expired signature | route.ts · REDIRECT_CACHE_SECONDS |
| Signed URL cache | In-process Map keyed by path@floor(now / (TTL/3)); cleared at 500 entries to bound long-lived serverless instances | gcs.ts · generateAvatarReadUrl |
| Default placeholder | /images/avatar-default.svg — local static, edge-cached, zero GCS dependency | media.ts · AVATAR_DEFAULT |
§ 3.26 Image Carousel & Deduplication covers property photos (Cloudinary). Person Data Architecture covers the tbl_persons schema. This page covers only the contact-avatar read/write path through the private bucket.