feat(app): profile photo — avatar upload in Mon compte, photo in the sidebar, drawer and top bar

packages/supabase assets module: closed AssetFailure kinds, begin / quarantine
upload (upsert off) / confirm / remove, own-row state read with an explicit
column list, download under the SELECT policy. App: client transport (magic
byte sniff, never SVG, createImageBitmap, canvas 1024 px, JPEG 0.9, 5 MiB
cap), AvatarSection (preview, save, 1 s polling up to 30 s, rejection and
failure messages per closed code, removal with confirmation, in-flight row
resumed at mount), auth store blob URL revoked on change and sign-out,
AvatarCircle in UserBadge and the top bar link to Mon compte (D3), FR/EN
strings, fake client extended, 27 new tests (app 300).
This commit is contained in:
bchanot committed 2026-10-13 19:32:37 +02:00
1 parent 614887470c
commit 926dd7df8c
18 files changed
+1550 -31

No files matched your search

+133
View File
@@ -0,0 +1,133 @@
import { AssetFailure } from '@faunosteo/supabase'
/**
* Browser-side preparation of a profile photo (docs/development/STORAGE.md
* §4): sniff the real type, decode, downscale to a transport size, re-encode
* as JPEG. Transport only: the worker re-decodes and judges the bytes again.
*/
export type SniffedImageType = 'image/jpeg' | 'image/png' | 'image/webp'
/** Largest file accepted at the picker (sanity bound before decoding). */
export const MAX_PICKED_BYTES = 20 * 1024 * 1024
/** Largest blob actually transferred (the bucket cap and the RPC CHECK). */
export const MAX_TRANSPORT_BYTES = 5 * 1024 * 1024
/** Longest side after the canvas downscale. */
export const TRANSPORT_MAX_SIDE = 1024
const JPEG_QUALITY = 0.9
function startsWith(bytes: Uint8Array, prefix: number[], at = 0): boolean {
if (bytes.length < at + prefix.length) return false
return prefix.every((b, i) => bytes[at + i] === b)
}
/** Magic bytes only: SVG, GIF, BMP or any text are null. */
export function sniffImageType(bytes: Uint8Array): SniffedImageType | null {
if (startsWith(bytes, [0xff, 0xd8, 0xff])) return 'image/jpeg'
if (startsWith(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) {
return 'image/png'
}
if (
startsWith(bytes, [0x52, 0x49, 0x46, 0x46]) &&
startsWith(bytes, [0x57, 0x45, 0x42, 0x50], 8)
) {
return 'image/webp'
}
return null
}
/** Canvas size for a bitmap: longest side capped, never upscaled. */
export function transportSize(
width: number,
height: number,
maxSide = TRANSPORT_MAX_SIDE,
): { width: number; height: number } {
const longest = Math.max(width, height)
if (longest <= maxSide) return { width, height }
const scale = maxSide / longest
return {
width: Math.max(1, Math.round(width * scale)),
height: Math.max(1, Math.round(height * scale)),
}
}
export interface BitmapLike {
width: number
height: number
close?: () => void
}
export interface CanvasLike {
width: number
height: number
getContext: (kind: '2d') => { drawImage: (image: unknown, x: number, y: number, w: number, h: number) => void } | null
toBlob: (cb: (blob: Blob | null) => void, type: string, quality: number) => void
}
/** The browser APIs involved, injectable for tests under happy-dom. */
export interface TransportDeps {
createImageBitmap: (file: Blob) => Promise<BitmapLike>
createCanvas: () => CanvasLike
createObjectURL: (blob: Blob) => string
}
function defaultDeps(): TransportDeps {
return {
createImageBitmap: (file) => createImageBitmap(file),
createCanvas: () => document.createElement('canvas') as unknown as CanvasLike,
createObjectURL: (blob) => URL.createObjectURL(blob),
}
}
export interface PreparedAvatar {
blob: Blob
previewUrl: string
}
async function readHead(file: Blob): Promise<Uint8Array> {
return new Uint8Array(await file.slice(0, 16).arrayBuffer())
}
function encodeJpeg(canvas: CanvasLike): Promise<Blob> {
return new Promise((resolve, reject) => {
canvas.toBlob(
(blob) => (blob ? resolve(blob) : reject(new AssetFailure('unsupported_type'))),
'image/jpeg',
JPEG_QUALITY,
)
})
}
/**
* Sniff → decode → downscale → JPEG. Throws AssetFailure('too_large' |
* 'unsupported_type'); never uploads anything.
*/
export async function prepareAvatarUpload(
file: File,
deps: TransportDeps = defaultDeps(),
): Promise<PreparedAvatar> {
if (file.size > MAX_PICKED_BYTES) throw new AssetFailure('too_large')
if (sniffImageType(await readHead(file)) === null) {
throw new AssetFailure('unsupported_type')
}
let bitmap: BitmapLike
try {
bitmap = await deps.createImageBitmap(file)
} catch (cause) {
throw new AssetFailure('unsupported_type', cause)
}
try {
const size = transportSize(bitmap.width, bitmap.height)
const canvas = deps.createCanvas()
canvas.width = size.width
canvas.height = size.height
const context = canvas.getContext('2d')
if (!context) throw new AssetFailure('unsupported_type')
context.drawImage(bitmap, 0, 0, size.width, size.height)
const blob = await encodeJpeg(canvas)
if (blob.size > MAX_TRANSPORT_BYTES) throw new AssetFailure('too_large')
return { blob, previewUrl: deps.createObjectURL(blob) }
} finally {
bitmap.close?.()
}
}