Files
faunosteo/supabase/migrations/20261009100000_sheet_asset_storage.sql

108 lines
4.6 KiB
PL/PgSQL

-- Storage for the SVG assets of anatomical sheets (business architecture
-- §15, ANATOMICAL_SHEET_MODEL.md §3 "assets").
--
-- Bucket anatomical-sheet-assets, private. Objects are content-addressed:
-- key = <sha256>.svg, so two sheets with the same drawing share one object
-- and a copy of a sheet never copies bytes. Reading an object is authorised
-- THROUGH a sheet: an asset row the caller may use or edit AND whose upload
-- was confirmed server-side (uploaded_at), never through a hash a user
-- merely registered; drafts stay visible to editors only, as the sheet rows
-- do. No API role writes objects: uploads are an operator / server task
-- (import tool with the service key) until the editor step adds a signed
-- upload path.
insert into storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
values ('anatomical-sheet-assets', 'anatomical-sheet-assets', false, 20971520, array['image/svg+xml'])
on conflict (id) do update
set public = false, file_size_limit = excluded.file_size_limit,
allowed_mime_types = excluded.allowed_mime_types;
-- ---------------------------------------------------------------------------
-- Upload confirmation. An editor may register any sha256 as metadata, so a
-- hash alone must never open an object: the read predicate requires
-- uploaded_at, which only the operator / server path (service role) sets
-- after the bytes have landed in the bucket for that sheet.
-- ---------------------------------------------------------------------------
alter table public.anatomical_sheet_assets
add column uploaded_at timestamptz;
comment on column public.anatomical_sheet_assets.uploaded_at is
'Set by confirm_sheet_asset_upload (service role) once the object <sha256>.svg exists for this sheet. Null = metadata only, object not readable through this row.';
create index anatomical_sheet_assets_sha256_idx on public.anatomical_sheet_assets (sha256);
create or replace function public.confirm_sheet_asset_upload(asset_id uuid)
returns uuid
language plpgsql
security definer
set search_path = ''
as $$
begin
update public.anatomical_sheet_assets
set uploaded_at = now()
where id = asset_id and uploaded_at is null;
if not found and not exists (select 1 from public.anatomical_sheet_assets where id = asset_id) then
raise exception 'unknown asset %', asset_id using errcode = 'P0002';
end if;
perform private.log_event(
auth.uid(), 'anatomical_sheet_asset.uploaded', 'anatomical_sheet_asset', asset_id
);
return asset_id;
end;
$$;
comment on function public.confirm_sheet_asset_upload(uuid) is
'Operator / server only (service role): marks the asset bytes as present in Storage. Idempotent.';
revoke all on function public.confirm_sheet_asset_upload(uuid) from public, anon, authenticated;
grant execute on function public.confirm_sheet_asset_upload(uuid) to service_role;
-- ---------------------------------------------------------------------------
-- Read predicate: a confirmed asset row of a sheet the caller edits, or of a
-- usable sheet whose non-draft version references it. Sargable on sha256.
-- ---------------------------------------------------------------------------
create or replace function private.can_read_sheet_asset_object(p_user_id uuid, p_object_name text)
returns boolean
language sql
stable
security definer
set search_path = ''
as $$
select exists (
select 1
from public.anatomical_sheet_assets a
where a.sha256 = left(p_object_name, 64)
and p_object_name = a.sha256 || '.svg'
and a.uploaded_at is not null
and (
private.can_edit_sheet(p_user_id, a.sheet_id)
or (
private.can_use_sheet(p_user_id, a.sheet_id)
and exists (
select 1 from public.anatomical_sheet_versions v
where v.sheet_id = a.sheet_id
and v.status <> 'draft'
and a.id::text = any (private.sheet_document_asset_ids(v.content))
)
)
)
);
$$;
comment on function private.can_read_sheet_asset_object(uuid, text) is
'Storage read predicate for bucket anatomical-sheet-assets: object <sha256>.svg whose upload is confirmed, referenced by a published version of a usable sheet, or by any version of a sheet the caller edits.';
revoke all on function private.can_read_sheet_asset_object(uuid, text) from public;
grant execute on function private.can_read_sheet_asset_object(uuid, text) to authenticated;
create policy anatomical_sheet_assets_read
on storage.objects
for select
to authenticated
using (
bucket_id = 'anatomical-sheet-assets'
and private.can_read_sheet_asset_object((select auth.uid()), name)
);
-- No INSERT / UPDATE / DELETE policy for API roles on this bucket.