chore(supabase): ceiling comments aligned, prefix patterns built once, cross-account residuals named in STORAGE.md

This commit is contained in:
bchanot committed 2026-10-13 20:20:19 +02:00
1 parent 6a9657ce1e
commit 40ae453f84
2 files changed
+9 -5

No files matched your search

@@ -204,7 +204,7 @@ create trigger assets_enqueue_purge_on_delete
for each row execute function private.enqueue_asset_objects();
-- Global ceiling on the bytes present in the avatars bucket (10 GiB to start),
-- measured on storage.objects, plus the declared size of uploads still expected.
-- measured on storage.objects, plus one 5 MiB cap per upload still expected.
create or replace function private.storage_ceiling()
returns bigint
language sql
@@ -312,6 +312,8 @@ declare
v_used bigint;
v_expected bigint;
v_mine bigint;
v_final_prefix text;
v_quarantine_prefix text;
v_object_key text;
v_quarantine_key text;
begin
@@ -337,10 +339,12 @@ begin
-- Per-account budget on bytes present under the caller's two prefixes
-- (clean photo, quarantine objects within their grace): bounds what one
-- account can hold against the global ceiling.
v_final_prefix := 'profiles/' || v_actor::text || '/%';
v_quarantine_prefix := 'quarantine/' || v_actor::text || '/%';
select coalesce(sum((o.metadata ->> 'size')::bigint), 0) into v_mine
from storage.objects o
where o.bucket_id = 'avatars'
and (o.name like 'profiles/' || v_actor || '/%' or o.name like 'quarantine/' || v_actor || '/%');
and (o.name like v_final_prefix or o.name like v_quarantine_prefix);
if v_mine + 5242880 > private.account_byte_budget() then
raise exception using message = 'rate_limited', errcode = 'P0001';
end if;
@@ -378,7 +382,7 @@ end;
$$;
comment on function public.begin_avatar_upload(text, integer) is
'Reserves an avatar upload: rate (10 per hour), global ceiling, type and size caps, supersedes any in-flight row of the caller, returns {asset_id, bucket_id, quarantine_key}.';
'Reserves an avatar upload: rate (10 per hour), per-account byte budget, global ceiling, type and size caps, supersedes any in-flight row of the caller, returns {asset_id, bucket_id, quarantine_key}.';
create or replace function public.confirm_asset_upload(asset_id uuid)
returns void
@@ -417,7 +421,7 @@ begin
raise exception using message = 'upload_missing', errcode = 'P0001';
end if;
-- The object may not exceed the cap nor what the caller declared (the
-- ceiling counted the declared size).
-- transport blob the browser announced).
if v_size > 5242880 or v_size > v_row.declared_size then
raise exception using message = 'too_large', errcode = 'P0001';
end if;