<?php
/**
 * Liefert TTS-Audio-Dateien aus wecker/storage/tts/.
 * storage/audio/ ist ausschließlich für Benutzer-Musik (api_musik).
 */
require __DIR__ . '/../config.php';

$f = isset($_GET['f']) ? basename($_GET['f']) : '';
if ($f === '' || preg_match('/[^a-zA-Z0-9_.-]/', $f)) {
    http_response_code(400);
    exit('Invalid file');
}

$ttsDir = (defined('STORAGE_ROOT') ? STORAGE_ROOT : (dirname(__DIR__) . '/storage')) . '/tts/';
$path = $ttsDir . $f;
if (!is_file($path) || !is_readable($path)) {
    http_response_code(404);
    exit('Not found');
}

$ext = strtolower(pathinfo($f, PATHINFO_EXTENSION));
$mimes = ['wav' => 'audio/wav', 'mp3' => 'audio/mpeg', 'ogg' => 'audio/ogg', 'm4a' => 'audio/mp4'];
$mime = $mimes[$ext] ?? 'application/octet-stream';

header('Content-Type: ' . $mime);
header('Content-Length: ' . filesize($path));
readfile($path);
