100 lines
3.5 KiB
TypeScript
100 lines
3.5 KiB
TypeScript
/**
|
|
* Uploads the SVG assets of a generated sheet seed into the Storage bucket
|
|
* anatomical-sheet-assets, content-addressed (<sha256>.svg).
|
|
*
|
|
* Operator tool: needs the service key, which never ships to the browser.
|
|
* Reads SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY from the environment
|
|
* (local stack: `pnpm supabase status` prints them). Idempotent: an object
|
|
* whose sha256 already exists is kept, a local file whose bytes differ from
|
|
* the manifest is refused, and each asset row is confirmed server-side
|
|
* (confirm_sheet_asset_upload) so the read policy can trust it.
|
|
*
|
|
* pnpm assets:upload supabase/seed/anatomical-sheets/equine/draft
|
|
*/
|
|
import { createHash } from 'node:crypto'
|
|
import { readFile } from 'node:fs/promises'
|
|
import { basename, join, resolve } from 'node:path'
|
|
import { createClient } from '@supabase/supabase-js'
|
|
|
|
const BUCKET = 'anatomical-sheet-assets'
|
|
|
|
interface AssetEntry {
|
|
file: string
|
|
sha256: string
|
|
byte_size: number
|
|
svg_asset_id: string
|
|
}
|
|
|
|
const SHA256_PATTERN = /^[0-9a-f]{64}$/
|
|
const UUID_PATTERN = /^[0-9a-f-]{36}$/
|
|
|
|
/** The manifest is committed data, still: no path separators, hex hash, uuid. */
|
|
function assertEntry(entry: AssetEntry): void {
|
|
if (basename(entry.file) !== entry.file || entry.file.startsWith('.')) {
|
|
throw new Error(`${entry.file}: file must be a plain file name`)
|
|
}
|
|
if (!SHA256_PATTERN.test(entry.sha256))
|
|
throw new Error(`${entry.file}: bad sha256`)
|
|
if (!UUID_PATTERN.test(entry.svg_asset_id))
|
|
throw new Error(`${entry.file}: bad asset id`)
|
|
}
|
|
|
|
function requireEnv(name: string): string {
|
|
const value = process.env[name]
|
|
if (!value) throw new Error(`${name} is required`)
|
|
return value
|
|
}
|
|
|
|
async function readManifest(dir: string): Promise<AssetEntry[]> {
|
|
const raw = await readFile(join(dir, 'assets.json'), 'utf8')
|
|
const entries = JSON.parse(raw) as AssetEntry[]
|
|
entries.forEach(assertEntry)
|
|
return entries
|
|
}
|
|
|
|
/** The bytes on disk must still match what the seed recorded. */
|
|
async function readVerified(dir: string, entry: AssetEntry): Promise<Buffer> {
|
|
const bytes = await readFile(join(dir, 'assets', entry.file))
|
|
const sha256 = createHash('sha256').update(bytes).digest('hex')
|
|
if (sha256 !== entry.sha256 || bytes.length !== entry.byte_size) {
|
|
throw new Error(`${entry.file}: bytes differ from assets.json`)
|
|
}
|
|
return bytes
|
|
}
|
|
|
|
async function main(): Promise<void> {
|
|
// pnpm runs package scripts inside the package: resolve from the caller's directory.
|
|
const dir = resolve(
|
|
process.env.INIT_CWD ?? process.cwd(),
|
|
process.argv[2] ?? '.',
|
|
)
|
|
const client = createClient(
|
|
requireEnv('SUPABASE_URL'),
|
|
requireEnv('SUPABASE_SERVICE_ROLE_KEY'),
|
|
{ auth: { persistSession: false } },
|
|
)
|
|
for (const entry of await readManifest(dir)) {
|
|
const key = `${entry.sha256}.svg`
|
|
const bytes = await readVerified(dir, entry)
|
|
const { error } = await client.storage
|
|
.from(BUCKET)
|
|
.upload(key, bytes, { contentType: 'image/svg+xml', upsert: false })
|
|
if (error && !/already exists/i.test(error.message)) {
|
|
throw new Error(`${key}: ${error.message}`)
|
|
}
|
|
// The asset row becomes readable only once the server records the upload.
|
|
const confirmed = await client.rpc('confirm_sheet_asset_upload', {
|
|
asset_id: entry.svg_asset_id,
|
|
})
|
|
if (confirmed.error) throw new Error(`${key}: ${confirmed.error.message}`)
|
|
console.log(
|
|
`${error ? 'kept' : 'uploaded'} ${key} (${entry.file}), confirmed`,
|
|
)
|
|
}
|
|
}
|
|
|
|
main().catch((error: unknown) => {
|
|
console.error(error instanceof Error ? error.message : error)
|
|
process.exit(1)
|
|
})
|