Files
Toiminnanohjaus/app/Modules/Jobs/Application/UseCases/AddTaskImage.php
T
petriandClaude Sonnet 5.5 ae8172898d 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]>
2026-11-25 07:46:53 +02:00

60 lines
2.0 KiB
PHP

<?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;
}
}
}