50 lines
2.5 KiB
PL/PgSQL
50 lines
2.5 KiB
PL/PgSQL
-- ═══════════════════════════════════════════════════════════════
|
|
-- Screenshots: Supabase Storage bucket + RLS
|
|
-- ═══════════════════════════════════════════════════════════════
|
|
-- Firecrawl이 반환하는 GCS URL은 7일 후 만료됨.
|
|
-- collect-channel-data가 스크린샷 캡처 시 base64를 이 버킷에 영구 저장.
|
|
-- 경로 규칙: screenshots/{reportId}/{screenshotId}.png
|
|
|
|
-- Storage 버킷 생성 (이미 있으면 무시)
|
|
INSERT INTO storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
|
|
VALUES (
|
|
'screenshots',
|
|
'screenshots',
|
|
true, -- public bucket: 인증 없이 URL만으로 열람 가능
|
|
5242880, -- 5MB per file limit
|
|
ARRAY['image/png', 'image/jpeg', 'image/webp']
|
|
)
|
|
ON CONFLICT (id) DO NOTHING;
|
|
|
|
-- ─── RLS Policies ───────────────────────────────────────────────
|
|
|
|
-- 1. 누구나 읽기 가능 (리포트 공유 URL에서 이미지 표시)
|
|
CREATE POLICY "public_read_screenshots"
|
|
ON storage.objects FOR SELECT
|
|
USING (bucket_id = 'screenshots');
|
|
|
|
-- 2. 서비스 역할만 업로드/삭제 (Edge Function이 service_role_key 사용)
|
|
CREATE POLICY "service_upload_screenshots"
|
|
ON storage.objects FOR INSERT
|
|
WITH CHECK (bucket_id = 'screenshots' AND auth.role() = 'service_role');
|
|
|
|
CREATE POLICY "service_delete_screenshots"
|
|
ON storage.objects FOR DELETE
|
|
USING (bucket_id = 'screenshots' AND auth.role() = 'service_role');
|
|
|
|
-- ─── 정리 함수 ─────────────────────────────────────────────────
|
|
-- 90일 이상 된 스크린샷 자동 삭제 (선택적 — pg_cron 스케줄로 실행)
|
|
-- 병원 리포트는 보통 3개월 후 재분석하므로 이전 스크린샷은 불필요.
|
|
CREATE OR REPLACE FUNCTION delete_old_screenshots()
|
|
RETURNS void
|
|
LANGUAGE sql
|
|
AS $$
|
|
DELETE FROM storage.objects
|
|
WHERE bucket_id = 'screenshots'
|
|
AND created_at < now() - interval '90 days';
|
|
$$;
|
|
|
|
-- 코멘트
|
|
COMMENT ON FUNCTION delete_old_screenshots() IS
|
|
'Deletes screenshots older than 90 days. Schedule with pg_cron: SELECT cron.schedule(''weekly-screenshot-cleanup'', ''0 3 * * 0'', ''SELECT delete_old_screenshots()'');';
|