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,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\Contracts;
|
||||
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskImage;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\StoredTaskImage;
|
||||
|
||||
/**
|
||||
* Keeps the files of task images: a downscaled, upright JPEG without
|
||||
* metadata (no location) and its thumbnail.
|
||||
*/
|
||||
interface TaskImageFiles
|
||||
{
|
||||
/**
|
||||
* Stores the image read from the uploaded file.
|
||||
*
|
||||
* @throws InvalidTaskImage
|
||||
*/
|
||||
public function store(string $uploadedFilePath, int $taskId): StoredTaskImage;
|
||||
|
||||
/**
|
||||
* Removes the files; missing files are ignored.
|
||||
*
|
||||
* @param list<string> $paths
|
||||
*/
|
||||
public function delete(array $paths): void;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\Contracts;
|
||||
|
||||
use App\Modules\Jobs\Application\DTOs\TaskImageFile;
|
||||
use App\Modules\Jobs\Application\DTOs\TaskImageSummary;
|
||||
|
||||
interface TaskImageQueries
|
||||
{
|
||||
/**
|
||||
* The task's images, oldest first; empty when the task belongs to
|
||||
* another job.
|
||||
*
|
||||
* @return list<TaskImageSummary>
|
||||
*/
|
||||
public function imagesOf(int $jobId, int $taskId): array;
|
||||
|
||||
/**
|
||||
* Where the full image or its thumbnail is kept, for showing it after
|
||||
* the job's visibility is checked; null when the image does not belong
|
||||
* to the task and job.
|
||||
*/
|
||||
public function fileOf(int $jobId, int $taskId, int $imageId, bool $thumbnail): ?TaskImageFile;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\DTOs;
|
||||
|
||||
/**
|
||||
* The stored file of a task image and the name it is downloaded with.
|
||||
*/
|
||||
final readonly class TaskImageFile
|
||||
{
|
||||
public function __construct(
|
||||
public string $path,
|
||||
public string $downloadName,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A task image with its uploader's name, without file paths. The time is
|
||||
* in the business time zone as "Y-m-d H:i".
|
||||
*/
|
||||
final readonly class TaskImageSummary
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public ?int $uploaderId,
|
||||
public string $uploaderName,
|
||||
public ?string $caption,
|
||||
public string $originalName,
|
||||
public string $uploadedAt,
|
||||
public int $width,
|
||||
public int $height,
|
||||
) {}
|
||||
}
|
||||
@@ -28,5 +28,6 @@ final readonly class TaskSummary
|
||||
public ?string $plannedFrom = null,
|
||||
public ?string $plannedTo = null,
|
||||
public int $commentCount = 0,
|
||||
public int $imageCount = 0,
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\Services;
|
||||
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskImageRepository;
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
||||
use App\Modules\Jobs\Domain\Entities\TaskImage;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskImageNotFound;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
|
||||
/**
|
||||
* Finds an image only through its task and the task's job, so a page of
|
||||
* one job never changes another job's images.
|
||||
*/
|
||||
final readonly class TaskImageFinder
|
||||
{
|
||||
public function __construct(
|
||||
private TaskRepository $tasks,
|
||||
private TaskImageRepository $images,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws TaskNotFound
|
||||
* @throws TaskImageNotFound
|
||||
*/
|
||||
public function find(int $jobId, int $taskId, int $imageId): TaskImage
|
||||
{
|
||||
$this->ensureTask($jobId, $taskId);
|
||||
|
||||
return $this->images->findForTask($taskId, $imageId) ?? throw new TaskImageNotFound($taskId, $imageId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TaskNotFound
|
||||
*/
|
||||
public function ensureTask(int $jobId, int $taskId): void
|
||||
{
|
||||
if ($this->tasks->findForJob($jobId, $taskId) === null) {
|
||||
throw new TaskNotFound($jobId, $taskId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\UseCases;
|
||||
|
||||
use App\Modules\Jobs\Application\Contracts\TaskImageFiles;
|
||||
use App\Modules\Jobs\Application\Services\TaskImageFinder;
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskImageRepository;
|
||||
use App\Modules\Jobs\Domain\Entities\TaskImage;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskImage;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskImageCaption;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\TaskImageCaption;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
use App\Modules\Users\Application\Contracts\Clock;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Sends an image to a task: its files are stored first and removed again
|
||||
* if the image cannot be saved.
|
||||
*/
|
||||
final class AddTaskImage
|
||||
{
|
||||
public function __construct(
|
||||
private TaskImageFinder $finder,
|
||||
private TaskImageRepository $images,
|
||||
private TaskImageFiles $files,
|
||||
private TransactionManager $transactions,
|
||||
private Clock $clock,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return int The new image's id.
|
||||
*
|
||||
* @throws TaskNotFound
|
||||
* @throws InvalidTaskImage
|
||||
* @throws InvalidTaskImageCaption
|
||||
*/
|
||||
public function execute(int $jobId, int $taskId, int $userId, string $uploadedFilePath, string $originalName, ?string $caption): int
|
||||
{
|
||||
$caption = TaskImageCaption::fromNullable($caption);
|
||||
$this->finder->ensureTask($jobId, $taskId);
|
||||
|
||||
$stored = $this->files->store($uploadedFilePath, $taskId);
|
||||
|
||||
try {
|
||||
return $this->transactions->run(function () use ($jobId, $taskId, $userId, $stored, $originalName, $caption): int {
|
||||
$this->finder->ensureTask($jobId, $taskId);
|
||||
|
||||
return (int) $this->images->save(TaskImage::create($taskId, $userId, $stored, $originalName, $caption, $this->clock->now()))->id();
|
||||
});
|
||||
} catch (Throwable $exception) {
|
||||
$this->files->delete($stored->paths());
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\UseCases;
|
||||
|
||||
use App\Modules\Jobs\Application\Services\TaskImageFinder;
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskImageRepository;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskImageCaption;
|
||||
use App\Modules\Jobs\Domain\Exceptions\NotTaskImageUploader;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskImageNotFound;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\TaskImageCaption;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Changes or clears the caption of the user's own image.
|
||||
*/
|
||||
final class ChangeTaskImageCaption
|
||||
{
|
||||
public function __construct(
|
||||
private TaskImageFinder $finder,
|
||||
private TaskImageRepository $images,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws TaskNotFound
|
||||
* @throws TaskImageNotFound
|
||||
* @throws NotTaskImageUploader
|
||||
* @throws InvalidTaskImageCaption
|
||||
*/
|
||||
public function execute(int $jobId, int $taskId, int $imageId, int $userId, ?string $caption): void
|
||||
{
|
||||
$caption = TaskImageCaption::fromNullable($caption);
|
||||
|
||||
$this->transactions->run(function () use ($jobId, $taskId, $imageId, $userId, $caption): void {
|
||||
$image = $this->finder->find($jobId, $taskId, $imageId);
|
||||
$image->changeCaption($userId, $caption);
|
||||
|
||||
$this->images->save($image);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -4,14 +4,16 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\UseCases;
|
||||
|
||||
use App\Modules\Jobs\Application\Contracts\TaskImageFiles;
|
||||
use App\Modules\Jobs\Application\Contracts\TaskUsage;
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskImageRepository;
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskHasTimeEntries;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Deletes a task that has no time entries.
|
||||
* Deletes a task that has no time entries, and then its images' files.
|
||||
*/
|
||||
final class RemoveTask
|
||||
{
|
||||
@@ -19,6 +21,8 @@ final class RemoveTask
|
||||
private TaskRepository $tasks,
|
||||
private TaskUsage $usage,
|
||||
private TransactionManager $transactions,
|
||||
private TaskImageRepository $images,
|
||||
private TaskImageFiles $imageFiles,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -27,14 +31,19 @@ final class RemoveTask
|
||||
*/
|
||||
public function execute(int $jobId, int $taskId): void
|
||||
{
|
||||
$this->transactions->run(function () use ($jobId, $taskId): void {
|
||||
$imagePaths = $this->transactions->run(function () use ($jobId, $taskId): array {
|
||||
$task = $this->tasks->findForJob($jobId, $taskId) ?? throw new TaskNotFound($jobId, $taskId);
|
||||
|
||||
if ($this->usage->hasTimeEntries($taskId)) {
|
||||
throw new TaskHasTimeEntries;
|
||||
}
|
||||
|
||||
$imagePaths = $this->images->filePathsOfTask($taskId);
|
||||
$this->tasks->delete($task);
|
||||
|
||||
return $imagePaths;
|
||||
});
|
||||
|
||||
$this->imageFiles->delete($imagePaths);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\UseCases;
|
||||
|
||||
use App\Modules\Jobs\Application\Contracts\TaskImageFiles;
|
||||
use App\Modules\Jobs\Application\Services\TaskImageFinder;
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskImageRepository;
|
||||
use App\Modules\Jobs\Domain\Exceptions\NotTaskImageUploader;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskImageNotFound;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Removes the user's own image, or anyone's when the user may remove
|
||||
* others' images (admin and management), and then its files.
|
||||
*/
|
||||
final class RemoveTaskImage
|
||||
{
|
||||
public function __construct(
|
||||
private TaskImageFinder $finder,
|
||||
private TaskImageRepository $images,
|
||||
private TaskImageFiles $files,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws TaskNotFound
|
||||
* @throws TaskImageNotFound
|
||||
* @throws NotTaskImageUploader
|
||||
*/
|
||||
public function execute(int $jobId, int $taskId, int $imageId, int $userId, bool $mayRemoveOthers): void
|
||||
{
|
||||
$paths = $this->transactions->run(function () use ($jobId, $taskId, $imageId, $userId, $mayRemoveOthers): array {
|
||||
$image = $this->finder->find($jobId, $taskId, $imageId);
|
||||
$image->ensureRemovableBy($userId, $mayRemoveOthers);
|
||||
|
||||
$this->images->delete($image);
|
||||
|
||||
return $image->file()->paths();
|
||||
});
|
||||
|
||||
$this->files->delete($paths);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user