Fix: convert audio in storage/sounds first to prevent Permission Denied on /var/lib/asterisk/sounds, fix ACLs and systemd ReadWritePaths

This commit is contained in:
sorcier committed 2026-11-02 14:16:56 +01:00
1 parent 8a6f398af5
commit 56052011fc
3 files changed
+34 -19

No files matched your search

+22 -9
View File
@@ -699,43 +699,56 @@ try {
$targetAlaw = "{$customDir}/{$slug}.alaw";
$targetMp3 = "{$storeDir}/{$slug}.mp3";
// Fichiers de travail dans storage/sounds (garanti accessible en écriture par www-data)
$storeWav = "{$storeDir}/{$slug}.wav";
$storeAlaw = "{$storeDir}/{$slug}.alaw";
// 1. Conversion Asterisk WAV (8kHz mono 16-bit PCM)
$cmdWav = sprintf('ffmpeg -y -i %s -ar 8000 -ac 1 -c:a pcm_s16le %s 2>&1', escapeshellarg($tmpPath), escapeshellarg($targetWav));
$cmdWav = sprintf('ffmpeg -y -i %s -ar 8000 -ac 1 -c:a pcm_s16le %s 2>&1', escapeshellarg($tmpPath), escapeshellarg($storeWav));
exec($cmdWav, $outWav, $retWav);
if ($retWav !== 0) {
@unlink($storeWav);
throw new RuntimeException("Erreur de conversion audio WAV Asterisk : " . implode("\n", $outWav));
}
// 2. Conversion Asterisk ALAW (G.711a 8kHz mono)
$cmdAlaw = sprintf('ffmpeg -y -i %s -ar 8000 -ac 1 -f alaw %s 2>&1', escapeshellarg($tmpPath), escapeshellarg($targetAlaw));
$cmdAlaw = sprintf('ffmpeg -y -i %s -ar 8000 -ac 1 -f alaw %s 2>&1', escapeshellarg($tmpPath), escapeshellarg($storeAlaw));
exec($cmdAlaw, $outAlaw, $retAlaw);
// 3. Conversion MP3 Web preview
$cmdMp3 = sprintf('ffmpeg -y -i %s -c:a libmp3lame -b:a 128k %s 2>&1', escapeshellarg($tmpPath), escapeshellarg($targetMp3));
exec($cmdMp3, $outMp3, $retMp3);
// 4. Si musique d'attente (MOH), copier dans le dossier MOH correspondant
// 4. Copie vers les dossiers Asterisk custom
@copy($storeWav, $targetWav);
if (file_exists($storeAlaw)) {
@copy($storeAlaw, $targetAlaw);
}
// 5. Si musique d'attente (MOH), copier dans le dossier MOH correspondant
if ($usageType === 'moh') {
$siteSlug = preg_replace('/[^a-z0-9]+/i', '-', strtolower($siteCategory));
$siteSlug = trim($siteSlug, '-') ?: 'default';
if ($siteSlug === 'global') $siteSlug = 'default';
$siteMohDir = "{$mohBase}/{$siteSlug}";
@mkdir($siteMohDir, 0775, true);
@copy($targetWav, "{$siteMohDir}/{$slug}.wav");
@copy($targetAlaw, "{$siteMohDir}/{$slug}.alaw");
@copy($storeWav, "{$siteMohDir}/{$slug}.wav");
if (file_exists($storeAlaw)) {
@copy($storeAlaw, "{$siteMohDir}/{$slug}.alaw");
}
@chown($siteMohDir, 'www-data');
@chgrp($siteMohDir, 'asterisk');
}
// Calcul de la durée en secondes
$cmdDur = sprintf('ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 %s 2>&1', escapeshellarg($targetWav));
$cmdDur = sprintf('ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 %s 2>&1', escapeshellarg($storeWav));
$durStr = trim((string)shell_exec($cmdDur));
$durationSec = max(1, (int)round((float)$durStr));
// Permissions
@chown($targetWav, 'www-data'); @chgrp($targetWav, 'asterisk');
@chown($targetAlaw, 'www-data'); @chgrp($targetAlaw, 'asterisk');
@chown($targetMp3, 'www-data'); @chgrp($targetMp3, 'asterisk');
@chown($storeWav, 'www-data'); @chgrp($storeWav, 'asterisk'); @chmod($storeWav, 0664);
@chown($targetWav, 'asterisk'); @chgrp($targetWav, 'asterisk'); @chmod($targetWav, 0664);
@chown($targetMp3, 'www-data'); @chgrp($targetMp3, 'asterisk'); @chmod($targetMp3, 0664);
$stmt = $db->prepare('
INSERT INTO sounds (name, slug, site_category, usage_type, description, filename, original_filename, duration_sec, file_size, format)