Add optional images to slides
Slides (except YouTube) can carry one image. Uploads are validated, rotated by EXIF orientation, scaled to 1600 px and stored as WebP outside webroot, which also strips camera metadata. Images have an alt text (or are marked decorative) and a layout: top, side or full screen (content slides). Files are removed after the commit when a slide is deleted or its image is replaced, with the poll, and `bin/cake images_cleanup` sweeps leftovers. Duplicated slides get their own file. Docker image gains gd with WebP. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8c7818ee40
commit
a395b50894
30 files changed
+1705
-11
No files matched your search
@@ -5,12 +5,17 @@ namespace App\Controller;
|
||||
|
||||
use App\Model\Entity\Slide;
|
||||
use App\Model\Table\SlidesTable;
|
||||
use App\Service\ImageException;
|
||||
use App\Service\ImageService;
|
||||
use App\Service\QaService;
|
||||
use App\Service\ResetService;
|
||||
use App\Service\ResultsService;
|
||||
use App\Service\YouTubeService;
|
||||
use Cake\Core\Configure;
|
||||
use Cake\Http\Exception\NotFoundException;
|
||||
use Cake\Http\Response;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Slide CRUD below /admin/{token}/slides.
|
||||
@@ -115,6 +120,9 @@ class SlidesController extends AdminAppController
|
||||
$this->Slides->deleteOrFail($slide);
|
||||
$this->Slides->normalizePositions($this->poll->id);
|
||||
});
|
||||
// After the commit (a rolled back deletion keeps its image). Model.afterDeleteCommit does not fire
|
||||
// inside this transaction, so the file is removed here.
|
||||
(new ImageService())->delete($this->poll->id, $slide->image);
|
||||
$this->Flash->success(__('Folie gelöscht.'));
|
||||
|
||||
return $this->redirect($this->adminUrl());
|
||||
@@ -289,6 +297,8 @@ class SlidesController extends AdminAppController
|
||||
$data = (array)$this->request->getData();
|
||||
// The type of an existing slide cannot change (would invalidate responses).
|
||||
$data['type'] = $slide->type;
|
||||
// The upload is handled by prepareImage(); the marshaller must not see the file object.
|
||||
unset($data['image']);
|
||||
$fields = ['type', 'question', 'description', 'settings', 'is_voting_open'];
|
||||
if ($slide->isQuiz() || $slide->isQa()) {
|
||||
// Quiz and Q&A voting is controlled by the presenter only.
|
||||
@@ -299,6 +309,7 @@ class SlidesController extends AdminAppController
|
||||
foreach ($youtube['errors'] as $field => $message) {
|
||||
$slide->setError($field, $message);
|
||||
}
|
||||
$image = $this->prepareImage($slide, $data);
|
||||
|
||||
$labels = SlidesTable::parseOptionLines($optionsText);
|
||||
$correct = [];
|
||||
@@ -326,11 +337,141 @@ class SlidesController extends AdminAppController
|
||||
}
|
||||
}
|
||||
if ($slide->hasErrors()) {
|
||||
$this->Flash->error(__('Bitte prüfe deine Eingaben.'));
|
||||
$this->discardImage($slide, $image);
|
||||
$this->Flash->error(__('Bitte prüfe deine Eingaben.') . ($image['new'] !== null ? ' ' . __('Das Bild musst du danach erneut auswählen.') : ''));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$saved = $this->persistSlide($slide, $labels, $correct, $youtube);
|
||||
} catch (Throwable $e) {
|
||||
$this->discardImage($slide, $image);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
if (!$saved) {
|
||||
$this->discardImage($slide, $image);
|
||||
|
||||
return false;
|
||||
}
|
||||
// Committed: the replaced (or removed) image is no longer referenced.
|
||||
if ($image['old'] !== null && $image['old'] !== $slide->image) {
|
||||
(new ImageService())->delete($this->poll->id, $image['old']);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the image of a posted slide: stores a new upload (scaled to WebP), takes over alternative
|
||||
* text and layout, or removes the image (`remove_image`). The values are set on the entity;
|
||||
* use discardImage() if the slide is not saved after all.
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Slide with the posted data patched
|
||||
* @param array<string, mixed> $data Request data
|
||||
* @return array{new: string|null, old: string|null, original: array<string, mixed>}
|
||||
* `new` = file stored for this request, `old` = file the slide had before
|
||||
*/
|
||||
protected function prepareImage(Slide $slide, array $data): array
|
||||
{
|
||||
$fields = ['image', 'image_alt', 'image_layout', 'image_width', 'image_height'];
|
||||
$original = [];
|
||||
foreach ($fields as $field) {
|
||||
$original[$field] = $slide->get($field);
|
||||
}
|
||||
$state = ['new' => null, 'old' => $slide->image, 'original' => $original];
|
||||
if (!Slide::supportsImage($slide->type)) {
|
||||
return $state;
|
||||
}
|
||||
|
||||
// Collected and set at the end: patching a field resets its errors.
|
||||
$errors = [];
|
||||
$upload = $this->request->getUploadedFile('image');
|
||||
$hasUpload = $upload instanceof UploadedFileInterface && $upload->getError() !== UPLOAD_ERR_NO_FILE;
|
||||
$remove = !$hasUpload && filter_var($data['remove_image'] ?? false, FILTER_VALIDATE_BOOLEAN);
|
||||
|
||||
if ($hasUpload) {
|
||||
if (!$slide->hasImage() && $this->Slides->countImages($this->poll->id) >= (int)Configure::read('Plenario.images.maxPerPoll', 20)) {
|
||||
$errors['image'] = __(
|
||||
'Pro Umfrage sind höchstens {0} Folien mit Bild möglich. Entferne bei einer anderen Folie das Bild.',
|
||||
(int)Configure::read('Plenario.images.maxPerPoll', 20),
|
||||
);
|
||||
} else {
|
||||
try {
|
||||
$stored = (new ImageService())->store($upload, $this->poll->id);
|
||||
$state['new'] = $stored['name'];
|
||||
$slide->patch(['image' => $stored['name'], 'image_width' => $stored['width'], 'image_height' => $stored['height']], ['guard' => false]);
|
||||
} catch (ImageException $e) {
|
||||
$errors['image'] = $e->getMessage();
|
||||
}
|
||||
}
|
||||
} elseif ($remove) {
|
||||
$slide->patch(['image' => null, 'image_alt' => null, 'image_layout' => null, 'image_width' => null, 'image_height' => null], ['guard' => false]);
|
||||
}
|
||||
|
||||
if (!$slide->hasImage()) {
|
||||
$this->setErrors($slide, $errors);
|
||||
|
||||
return $state;
|
||||
}
|
||||
// Without an image to describe, accessibility needs either a text or the explicit "decorative".
|
||||
$alt = mb_substr(trim((string)($data['image_alt'] ?? '')), 0, Slide::IMAGE_ALT_MAX);
|
||||
$decorative = filter_var($data['image_decorative'] ?? false, FILTER_VALIDATE_BOOLEAN);
|
||||
if (!$decorative && $alt === '') {
|
||||
$errors['image_alt'] = __('Bitte beschreibe das Bild kurz (für Menschen mit Screenreader) oder markiere es als reine Dekoration.');
|
||||
}
|
||||
$layout = $data['image_layout'] ?? null;
|
||||
$slide->patch([
|
||||
'image_alt' => $decorative ? '' : $alt,
|
||||
'image_layout' => in_array($layout, Slide::IMAGE_LAYOUTS, true) ? $layout : null,
|
||||
], ['guard' => false]);
|
||||
$this->setErrors($slide, $errors);
|
||||
|
||||
return $state;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Slide $slide Slide
|
||||
* @param array<string, string> $errors Field => message
|
||||
* @return void
|
||||
*/
|
||||
protected function setErrors(Slide $slide, array $errors): void
|
||||
{
|
||||
foreach ($errors as $field => $message) {
|
||||
$slide->setError($field, $message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws away the image stored for this request and restores the previous image values
|
||||
* of the entity (the slide was not saved, the form is shown again).
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Slide
|
||||
* @param array{new: string|null, old: string|null, original: array<string, mixed>} $image State of prepareImage()
|
||||
* @return void
|
||||
*/
|
||||
protected function discardImage(Slide $slide, array $image): void
|
||||
{
|
||||
if ($image['new'] !== null) {
|
||||
(new ImageService())->delete($this->poll->id, $image['new']);
|
||||
}
|
||||
$errors = $slide->getErrors();
|
||||
$slide->patch($image['original'], ['guard' => false]);
|
||||
$slide->setErrors($errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the slide and its options in one transaction.
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Slide
|
||||
* @param list<string> $labels Option labels
|
||||
* @param list<string> $correct Labels of the correct options
|
||||
* @param array{errors: array<string, string>, warning: string|null} $youtube Result of prepareYouTube()
|
||||
* @return bool
|
||||
*/
|
||||
protected function persistSlide(Slide $slide, array $labels, array $correct, array $youtube): bool
|
||||
{
|
||||
return (bool)$this->Slides->getConnection()->transactional(function () use ($slide, $labels, $correct, $youtube): bool {
|
||||
if (!$this->Slides->save($slide)) {
|
||||
return false;
|
||||
|
||||
Reference in new issue
Block a user