fix(storage): purge raw audio when its history entry or meeting is deleted

This commit is contained in:
Yun Chan 2026-09-28 02:16:22 +09:00
parent 4807a5283d
commit ed790e672b
10 changed files with 1073 additions and 39 deletions

View file

@ -2,6 +2,11 @@ import { corsHeaders, handleCorsPreflightRequest } from '../_shared/cors.ts'
import { requireUser, authErrorResponse, type AuthError } from '../_shared/auth.ts'
import { createServiceRoleClient } from '../_shared/quota.ts'
import { hasRecentAuthentication } from './recent-auth.ts'
import {
listStorageFiles,
removeStorageObjects,
supabaseStorageBucket,
} from '../_shared/storage-objects.ts'
interface DeleteAccountRequest {
confirmation: string
@ -17,39 +22,6 @@ function jsonResponse(body: Record<string, unknown>, status = 200): Response {
})
}
async function listStorageFiles(
serviceClient: ReturnType<typeof createServiceRoleClient>,
bucket: string,
prefix: string,
): Promise<string[]> {
const result: string[] = []
let offset = 0
while (true) {
const { data, error } = await serviceClient.storage.from(bucket).list(prefix, {
limit: 100,
offset,
sortBy: { column: 'name', order: 'asc' },
})
if (error) throw new Error(`storage_list_failed:${bucket}`)
if (!data || data.length === 0) break
for (const entry of data) {
const childPath = `${prefix}/${entry.name}`
if (entry.id) {
result.push(childPath)
} else {
result.push(...await listStorageFiles(serviceClient, bucket, childPath))
}
}
if (data.length < 100) break
offset += data.length
}
return result
}
Deno.serve(async (req: Request) => {
const preflight = handleCorsPreflightRequest(req)
if (preflight) return preflight
@ -110,12 +82,8 @@ Deno.serve(async (req: Request) => {
}
for (const bucket of STORAGE_BUCKETS) {
const files = await listStorageFiles(serviceClient, bucket, user.id)
for (let index = 0; index < files.length; index += 100) {
const batch = files.slice(index, index + 100)
const { error } = await serviceClient.storage.from(bucket).remove(batch)
if (error) throw new Error(`storage_delete_failed:${bucket}`)
}
const storage = supabaseStorageBucket(serviceClient, bucket)
await removeStorageObjects(storage, await listStorageFiles(storage, user.id))
}
const { error: deleteError } = await serviceClient.auth.admin.deleteUser(user.id, false)