<?php
/**
 * Liefert Audio-Dateien aus wecker/storage/audio/.
 * Aufruf: /audio_file.php?f=tts_123.wav
 */
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');
}

$path = STORAGE_ROOT . '/audio/' . $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);
