<?php
/**
 * Liefert Benutzer-Audiodateien aus wecker/storage/audio/.
 * Wird vom Alarm-Klingel-Browser-Player und der Musik-Verwaltung genutzt.
 */
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');
}

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

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

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