Add images to tasks
Everyone who may comment on a task sends images to it, e.g. photos taken with a phone. The server stores an upright, at most 2560 px JPEG without EXIF data (no location) and a 400 px thumbnail on the private disk, served only to those who see the job. Uploaders change captions (also the alt text) and remove their own images; admin and management may remove anyone's (jobs.remove-any-task-image). Removing a task also removes its image files. Livewire's temporary upload limit is raised to PHP's 40 MB so that the page's own 12 MB limit gives a translated message. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Contracts;
|
||||
|
||||
use App\Modules\Jobs\Domain\Entities\TaskImage;
|
||||
|
||||
interface TaskImageRepository
|
||||
{
|
||||
/**
|
||||
* Null when the image does not exist or belongs to another task.
|
||||
*/
|
||||
public function findForTask(int $taskId, int $imageId): ?TaskImage;
|
||||
|
||||
/**
|
||||
* The files of all the task's images, for removing them with the task.
|
||||
*
|
||||
* @return list<string>
|
||||
*/
|
||||
public function filePathsOfTask(int $taskId): array;
|
||||
|
||||
public function save(TaskImage $image): TaskImage;
|
||||
|
||||
public function delete(TaskImage $image): void;
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Entities;
|
||||
|
||||
use App\Modules\Jobs\Domain\Exceptions\NotTaskImageUploader;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\StoredTaskImage;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\TaskImageCaption;
|
||||
use DateTimeImmutable;
|
||||
|
||||
/**
|
||||
* An image sent to a task. Its uploader may change the caption and remove
|
||||
* it; others may only remove it, and only when allowed to remove anyone's
|
||||
* images.
|
||||
*/
|
||||
final class TaskImage
|
||||
{
|
||||
private function __construct(
|
||||
private ?int $id,
|
||||
private int $taskId,
|
||||
private ?int $uploaderId,
|
||||
private StoredTaskImage $file,
|
||||
private string $originalName,
|
||||
private ?TaskImageCaption $caption,
|
||||
private DateTimeImmutable $uploadedAt,
|
||||
) {}
|
||||
|
||||
public static function create(int $taskId, int $uploaderId, StoredTaskImage $file, string $originalName, ?TaskImageCaption $caption, DateTimeImmutable $at): self
|
||||
{
|
||||
$originalName = trim($originalName);
|
||||
|
||||
return new self(null, $taskId, $uploaderId, $file, mb_substr($originalName === '' ? 'kuva.jpg' : $originalName, 0, 255), $caption, $at);
|
||||
}
|
||||
|
||||
/**
|
||||
* The uploader is null when the user no longer exists.
|
||||
*/
|
||||
public static function reconstitute(int $id, int $taskId, ?int $uploaderId, StoredTaskImage $file, string $originalName, ?TaskImageCaption $caption, DateTimeImmutable $uploadedAt): self
|
||||
{
|
||||
return new self($id, $taskId, $uploaderId, $file, $originalName, $caption, $uploadedAt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotTaskImageUploader
|
||||
*/
|
||||
public function changeCaption(int $userId, ?TaskImageCaption $caption): void
|
||||
{
|
||||
$this->ensureUploader($userId);
|
||||
|
||||
$this->caption = $caption;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotTaskImageUploader
|
||||
*/
|
||||
public function ensureRemovableBy(int $userId, bool $mayRemoveOthers): void
|
||||
{
|
||||
if (! $mayRemoveOthers) {
|
||||
$this->ensureUploader($userId);
|
||||
}
|
||||
}
|
||||
|
||||
public function id(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function taskId(): int
|
||||
{
|
||||
return $this->taskId;
|
||||
}
|
||||
|
||||
public function uploaderId(): ?int
|
||||
{
|
||||
return $this->uploaderId;
|
||||
}
|
||||
|
||||
public function file(): StoredTaskImage
|
||||
{
|
||||
return $this->file;
|
||||
}
|
||||
|
||||
public function originalName(): string
|
||||
{
|
||||
return $this->originalName;
|
||||
}
|
||||
|
||||
public function caption(): ?TaskImageCaption
|
||||
{
|
||||
return $this->caption;
|
||||
}
|
||||
|
||||
public function uploadedAt(): DateTimeImmutable
|
||||
{
|
||||
return $this->uploadedAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotTaskImageUploader
|
||||
*/
|
||||
private function ensureUploader(int $userId): void
|
||||
{
|
||||
if ($this->uploaderId !== $userId) {
|
||||
throw new NotTaskImageUploader((int) $this->id, $userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
/**
|
||||
* The uploaded file could not be read as an image.
|
||||
*/
|
||||
final class InvalidTaskImage extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The uploaded file is not a readable image.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class InvalidTaskImageCaption extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('An image caption may be at most 255 characters.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
/**
|
||||
* Only the uploader changes an image's caption; others than the uploader
|
||||
* may remove it only when they may remove anyone's images.
|
||||
*/
|
||||
final class NotTaskImageUploader extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $imageId, public readonly int $userId)
|
||||
{
|
||||
parent::__construct("User {$userId} did not upload image {$imageId}.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
/**
|
||||
* The image does not exist or belongs to another task or job.
|
||||
*/
|
||||
final class TaskImageNotFound extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $taskId, public readonly int $imageId)
|
||||
{
|
||||
parent::__construct("Image {$imageId} of task {$taskId} was not found.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\ValueObjects;
|
||||
|
||||
/**
|
||||
* Where a task image's files are kept and the size of the full image.
|
||||
*/
|
||||
final readonly class StoredTaskImage
|
||||
{
|
||||
public function __construct(
|
||||
public string $path,
|
||||
public string $thumbnailPath,
|
||||
public int $width,
|
||||
public int $height,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public function paths(): array
|
||||
{
|
||||
return [$this->path, $this->thumbnailPath];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskImageCaption;
|
||||
|
||||
/**
|
||||
* The optional caption of a task image, also its alternative text.
|
||||
*/
|
||||
final readonly class TaskImageCaption
|
||||
{
|
||||
public const int MAX_LENGTH = 255;
|
||||
|
||||
private function __construct(public string $value) {}
|
||||
|
||||
/**
|
||||
* Null for an empty caption.
|
||||
*
|
||||
* @throws InvalidTaskImageCaption
|
||||
*/
|
||||
public static function fromNullable(?string $value): ?self
|
||||
{
|
||||
$value = trim(preg_replace('/\s+/u', ' ', (string) $value) ?? (string) $value);
|
||||
|
||||
if ($value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (mb_strlen($value) > self::MAX_LENGTH) {
|
||||
throw new InvalidTaskImageCaption;
|
||||
}
|
||||
|
||||
return new self($value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user