Add countdown audio per presentation
One audio file per poll (MP3, M4A, OGG, WAV) is uploaded in the admin settings and looped in the presenter view while a quiz runs. Format is detected by magic bytes, files are served with a fixed content type, copied with the poll and swept by images_cleanup. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cad09163d9
commit
2f7a401522
16 files changed
+648
-11
No files matched your search
@@ -7,6 +7,8 @@ use App\Mailer\PollMailer;
|
||||
use App\Model\Entity\Slide;
|
||||
use App\Model\Table\ResponsesTable;
|
||||
use App\Model\Table\SlidesTable;
|
||||
use App\Service\AudioService;
|
||||
use App\Service\ImageException;
|
||||
use App\Service\PollStateService;
|
||||
use App\Service\QaService;
|
||||
use App\Service\ReportService;
|
||||
@@ -17,6 +19,7 @@ use Cake\Http\Exception\BadRequestException;
|
||||
use Cake\Http\Response;
|
||||
use Cake\I18n\DateTime;
|
||||
use Cake\Routing\Router;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use chillerlan\QRCode\Output\QRMarkupSVG;
|
||||
use chillerlan\QRCode\QRCode;
|
||||
use chillerlan\QRCode\QROptions;
|
||||
@@ -89,6 +92,53 @@ class AdminController extends AdminAppController
|
||||
return $this->redirect($this->adminUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* Countdown music of the presentation: uploads a new file (replaces the old one) or, with
|
||||
* `remove` = 1, deletes it. Old files are deleted after saving, never before.
|
||||
*
|
||||
* @return \Cake\Http\Response|null
|
||||
*/
|
||||
public function audio(): ?Response
|
||||
{
|
||||
$this->request->allowMethod(['post']);
|
||||
$service = new AudioService();
|
||||
$old = $this->poll->audio;
|
||||
$upload = $this->request->getUploadedFile('audio');
|
||||
$hasUpload = $upload instanceof UploadedFileInterface && $upload->getError() !== UPLOAD_ERR_NO_FILE;
|
||||
$back = $this->adminUrl() + ['#' => 'audio'];
|
||||
|
||||
if ($hasUpload) {
|
||||
try {
|
||||
$new = $service->store($upload, $this->poll->id);
|
||||
} catch (ImageException $e) {
|
||||
$this->Flash->error($e->getMessage());
|
||||
|
||||
return $this->redirect($back);
|
||||
}
|
||||
} elseif (filter_var($this->request->getData('remove'), FILTER_VALIDATE_BOOLEAN)) {
|
||||
$new = null;
|
||||
} else {
|
||||
$this->Flash->error(__('Bitte wähle eine Audiodatei aus.'));
|
||||
|
||||
return $this->redirect($back);
|
||||
}
|
||||
|
||||
$this->poll->set('audio', $new);
|
||||
try {
|
||||
$this->Polls->saveOrFail($this->poll);
|
||||
} catch (Throwable $e) {
|
||||
$service->delete($this->poll->id, $new);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
if ($old !== null && $old !== $new) {
|
||||
$service->delete($this->poll->id, $old);
|
||||
}
|
||||
$this->Flash->success($new === null ? __('Audiodatei entfernt.') : __('Audiodatei gespeichert.'));
|
||||
|
||||
return $this->redirect($back);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidates the current admin link and mails a new one.
|
||||
*
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace App\Controller;
|
||||
use App\Model\Entity\Poll;
|
||||
use App\Model\Entity\Slide;
|
||||
use App\Model\Table\PollsTable;
|
||||
use App\Service\AudioService;
|
||||
use App\Service\ImageService;
|
||||
use App\Service\PollStateService;
|
||||
use App\Service\VoteException;
|
||||
@@ -50,7 +51,7 @@ class ParticipateController extends AppController
|
||||
public function beforeFilter(EventInterface $event): void
|
||||
{
|
||||
parent::beforeFilter($event);
|
||||
if ($this->request->getParam('action') === 'image') {
|
||||
if (in_array($this->request->getParam('action'), ['image', 'audio'], true)) {
|
||||
// Cacheable static content: no participant id, no cookie.
|
||||
return;
|
||||
}
|
||||
@@ -124,6 +125,29 @@ class ParticipateController extends AppController
|
||||
->withHeader('Cache-Control', 'private, max-age=31536000, immutable');
|
||||
}
|
||||
|
||||
/**
|
||||
* Serves the countdown audio of this poll (random file name, cached for good, supports range
|
||||
* requests so browsers can loop and seek). Content type is fixed by the extension, never sniffed.
|
||||
*
|
||||
* @param string $code Join code
|
||||
* @param string $file Stored file name
|
||||
* @return \Cake\Http\Response
|
||||
*/
|
||||
public function audio(string $code, string $file): Response
|
||||
{
|
||||
$poll = $this->loadPoll($code);
|
||||
$path = $poll->audio === $file ? (new AudioService())->path($poll->id, $file) : null;
|
||||
if ($path === null) {
|
||||
throw new NotFoundException(__('Audiodatei nicht gefunden.'));
|
||||
}
|
||||
|
||||
return $this->response
|
||||
->withFile($path)
|
||||
->withHeader('Content-Type', AudioService::contentType($file))
|
||||
->withHeader('X-Content-Type-Options', 'nosniff')
|
||||
->withHeader('Cache-Control', 'private, max-age=31536000, immutable');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $code Join code
|
||||
* @return \Cake\Http\Response
|
||||
|
||||
Reference in new issue
Block a user