feat(admin): full session material management in the session editor

See the attached file (clean name), Open (signed-URL preview), Download
(authenticated blob under the original filename), Delete (confirm, storage
object + path, errors surface), and Upload/Replace with previous-object
cleanup and upload rollback on DB failure. Admins already passed every
storage RLS gate for material writes, so no migration; this also documents
and smoke-verifies insert/read/update/delete as an impersonated admin.
Also fixes the stale e2e expectation that the speaker-portal download is an
<a href> (it became an authenticated blob button with the private bucket).
This commit is contained in:
Lago committed 2026-10-21 21:17:02 +02:00
1 parent 246be7d787
commit ca88c0e564
5 files changed
+150 -43

No files matched your search

+46 -6
View File
@@ -518,13 +518,34 @@ export async function setSessionSpeakers(sessionId: string, emails: string[]): P
}
}
/** Upload a session material file to the `materials` bucket and store the path. */
export async function uploadSessionMaterial(sessionId: string, file: File): Promise<string> {
/** Human filename of a material object path (`sessions/<id>/<ts>-<name>`). */
export function sessionMaterialName(path: string): string {
const base = path.slice(path.lastIndexOf('/') + 1);
return base.replace(/^\d+-/, '');
}
/** Upload a session material file to the `materials` bucket and store the
* path. Admin passes the storage write gate (`can_write_session_material`
* allows `is_admin()`), so uploads on behalf of speakers work. When replacing
* an existing file, the old object is removed best-effort after the new one
* is in place; a failed DB update rolls the fresh upload back so the session
* never points at a nonexistent path. */
export async function uploadSessionMaterial(
sessionId: string,
file: File,
previousPath?: string | null,
): Promise<string> {
const path = `sessions/${sessionId}/${Date.now()}-${file.name}`;
const { error } = await supabase.storage.from('materials').upload(path, file, { upsert: true });
if (error) throw new ApiError(error.message);
const { error: upErr } = await supabase.from('sessions').update({ material_path: path }).eq('id', sessionId);
if (upErr) throw new ApiError(upErr.message);
if (upErr) {
await supabase.storage.from('materials').remove([path]);
throw new ApiError(upErr.message);
}
if (previousPath && previousPath !== path) {
await supabase.storage.from('materials').remove([previousPath]);
}
return path;
}
@@ -532,20 +553,39 @@ export async function removeSessionMaterial(sessionId: string): Promise<void> {
const { data } = await supabase.from('sessions').select('material_path').eq('id', sessionId).maybeSingle();
const path = (data as Session | null)?.material_path;
if (path) {
await supabase.storage.from('materials').remove([path]);
// Surface storage failures instead of nulling the path and orphaning the file.
const { error: rmErr } = await supabase.storage.from('materials').remove([path]);
if (rmErr) throw new ApiError(rmErr.message);
}
const { error } = await supabase.from('sessions').update({ material_path: null }).eq('id', sessionId);
if (error) throw new ApiError(error.message);
}
/** Open a session material in a new tab via a short-lived signed URL. The
* materials bucket is private; storage RLS always allows admins. */
/** Open a session material in a new tab via a short-lived signed URL (previews
* inline for browser-renderable types like PDF). The materials bucket is
* private; storage RLS always allows admins, bypassing the attendee time-lock. */
export async function openSessionMaterial(path: string): Promise<void> {
const { data, error } = await supabase.storage.from('materials').createSignedUrl(path, 600);
if (error) throw new ApiError(error.message);
window.open(data.signedUrl, '_blank');
}
/** Save a session material to disk through authenticated storage download —
* same mechanism the attendee app uses after a session ends, always allowed
* for admins. Saves under the clean original filename. */
export async function downloadSessionMaterial(path: string): Promise<void> {
const { data, error } = await supabase.storage.from('materials').download(path);
if (error) throw new ApiError(error.message);
const url = URL.createObjectURL(data);
const a = document.createElement('a');
a.href = url;
a.download = sessionMaterialName(path);
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
}
async function feedbackSidecarFetch<T>(path: string, init?: RequestInit): Promise<T> {
const { data, error } = await supabase.auth.getSession();
if (error || !data.session) throw new ApiError('Sign in as an administrator');