feat(supabase): sheet drawing register, upload, confirm accessors and save answer

This commit is contained in:
bchanot committed 2026-11-04 11:57:22 +01:00
1 parent cfd49c7b5b
commit f7e7bf4ee1
4 files changed
+334

No files matched your search

+6
View File
@@ -69,6 +69,12 @@ export {
getOfficialSheetAdmin,
listSheetVersions,
downloadSheetDrawing,
registerSheetAsset,
listSheetAssets,
uploadSheetDrawing,
confirmSheetAssetUpload,
parseSaveAnswer,
type SheetAssetRow,
listOwnCatalogGrants,
type CatalogAccount,
type CatalogGrant,
+88
View File
@@ -93,6 +93,7 @@ export const SHEET_FAILURE_KINDS = [
// uploads and working copy
'upload_missing',
'size_mismatch',
'upload.pending_other',
'asset.referenced',
'working_copy.source_invalid',
'working_copy.exists',
@@ -257,6 +258,13 @@ export async function saveSheetDraft(
return data
}
/** The save answer `{ revision }` (M5): a positive integer, else `unavailable`. */
export function parseSaveAnswer(value: unknown): { revision: number } {
const answer = z.object({ revision: z.number().int().positive() }).safeParse(value)
if (!answer.success) throw new SheetFailure('unavailable', answer.error)
return { revision: answer.data.revision }
}
export async function submitSheetReview(
client: FaunosteoClient,
args: { version_id: string; expected_revision: number; note?: string },
@@ -532,6 +540,7 @@ const DRAWING_BUCKET = 'anatomical-sheet-assets'
interface StorageLikeError {
name?: string
status?: number
statusCode?: string
code?: string
}
@@ -563,3 +572,82 @@ export async function downloadSheetDrawing(
if (error || !data) throw new SheetFailure(classifyDrawingError(error), error)
return new Uint8Array(await data.arrayBuffer())
}
// --- Drawing upload (editor, aal2; §9.2-§9.3) --------------------------------
/** Registers a drawing of a sheet; the row of the same hash is returned when it exists. */
export async function registerSheetAsset(
client: FaunosteoClient,
args: { sheet_id: string; sha256: string; byte_size: number; file_name: string },
): Promise<string> {
const { data, error, status } = await client.rpc('register_sheet_asset', args)
if (error) failSheet(error, status)
return data
}
export interface SheetAssetRow {
id: string
sha256: string
byte_size: number
uploaded_at: string | null
}
/** The drawing rows of a sheet the caller can see; `uploaded_at` null is a pending row. */
export async function listSheetAssets(
client: FaunosteoClient,
sheetId: string,
): Promise<SheetAssetRow[]> {
if (!UUID_PATTERN.test(sheetId)) throw new SheetFailure('not_found')
const { data, error, status } = await client
.from('anatomical_sheet_assets')
.select('id, sha256, byte_size, uploaded_at')
.eq('sheet_id', sheetId)
if (error) failSheet(error, status)
return data ?? []
}
/** The Storage code of a refusal: HTTP status, or body `statusCode` behind an HTTP 400 (LRN-113). */
function storageCode(error: StorageLikeError): string {
const bodyCode = error.status === 400 ? error.statusCode : undefined
return bodyCode ?? String(error.status)
}
/**
* Uploads `<sha256>.svg` without upsert. `present`: the object is already
* there (409). A policy refusal (403: a pending row of another account) is
* `upload.pending_other`; the caller then confirms once. The bytes are the
* core's `stored`; no check here.
*/
export async function uploadSheetDrawing(
client: FaunosteoClient,
sha256: string,
bytes: Uint8Array,
): Promise<'uploaded' | 'present'> {
if (!SHA256_PATTERN.test(sha256)) throw new SheetFailure('invalid_input')
const { error } = await client.storage
.from(DRAWING_BUCKET)
.upload(`${sha256}.svg`, bytes, { contentType: 'image/svg+xml', upsert: false })
if (!error) return 'uploaded'
const code = storageCode(error)
if (code === '409') return 'present'
if (code === '403') throw new SheetFailure('upload.pending_other', error)
throw new SheetFailure(classifyUploadError(error), error)
}
function classifyUploadError(error: StorageLikeError): SheetFailureKind {
if (error.name === 'StorageUnknownError') return 'network'
if (error.code === 'InvalidJWT' || error.status === 401) return 'session_expired'
if (error.status === 429) return 'rate_limited'
return 'unavailable'
}
/** Confirms the upload of a registered drawing (`upload_missing`, `size_mismatch`). */
export async function confirmSheetAssetUpload(
client: FaunosteoClient,
assetId: string,
): Promise<string> {
const { data, error, status } = await client.rpc('confirm_sheet_asset_upload', {
asset_id: assetId,
})
if (error) failSheet(error, status)
return data
}