All files / web/src/app/api/admin/audio/voices route.ts

0% Statements 0/46
0% Branches 0/1
0% Functions 0/1
0% Lines 0/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47                                                                                             
import { existsSync, readdirSync, rmSync, statSync } from 'fs'
import { join } from 'path'
import { NextResponse } from 'next/server'
import { db } from '@/db'
import { ttsCollectedClips, ttsCollectedClipSay } from '@/db/schema'
import { withAuth } from '@/lib/auth/withAuth'

const AUDIO_DIR = join(process.cwd(), 'data', 'audio')

/**
 * DELETE /api/admin/audio/voices
 *
 * Nuclear reset: removes all generated audio clips from disk AND all
 * collected clip records from the database.
 *
 * - Deletes every voice directory under data/audio/ (preserves the parent)
 * - Deletes all rows from tts_collected_clip_say then tts_collected_clips
 */
export const DELETE = withAuth(
  async () => {
    try {
      // 1. Remove voice directories from disk
      let removedDirs = 0
      if (existsSync(AUDIO_DIR)) {
        const entries = readdirSync(AUDIO_DIR)
        for (const entry of entries) {
          const entryPath = join(AUDIO_DIR, entry)
          if (statSync(entryPath).isDirectory()) {
            rmSync(entryPath, { recursive: true, force: true })
            removedDirs++
          }
        }
      }

      // 2. Clear collected clips from the database (child table first)
      await db.delete(ttsCollectedClipSay)
      await db.delete(ttsCollectedClips)

      return NextResponse.json({ removedDirs, clearedDb: true })
    } catch (error) {
      console.error('Error nuking all clips:', error)
      return NextResponse.json({ error: 'Failed to remove clips' }, { status: 500 })
    }
  },
  { role: 'admin' }
)