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:
2026-11-25 07:46:53 +02:00
co-authored by Claude Sonnet 5.5
parent 4fb8ea1c5f
commit ae8172898d
40 changed files with 2250 additions and 8 deletions
@@ -0,0 +1,61 @@
<?php
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
use App\Modules\Jobs\Application\Contracts\TaskImageQueries;
use App\Modules\Jobs\Application\DTOs\TaskImageFile;
use App\Modules\Jobs\Application\DTOs\TaskImageSummary;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskImageModel;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use Illuminate\Database\Eloquent\Builder;
class EloquentTaskImageQueries implements TaskImageQueries
{
public function __construct(private UserAccountQueries $users) {}
public function imagesOf(int $jobId, int $taskId): array
{
$models = $this->ofTask($jobId, $taskId)->orderBy('id')->get();
$names = $this->users->namesByIds($models->pluck('user_id')->filter()->unique()->values()->all());
return $models
->map(fn (TaskImageModel $model): TaskImageSummary => new TaskImageSummary(
$model->id,
$model->user_id,
$model->user_id === null ? '' : ($names[$model->user_id] ?? ''),
$model->caption,
$model->original_name,
$model->created_at->setTimezone((string) config('app.business_timezone'))->format('Y-m-d H:i'),
$model->width,
$model->height,
))
->values()
->all();
}
public function fileOf(int $jobId, int $taskId, int $imageId, bool $thumbnail): ?TaskImageFile
{
$model = $this->ofTask($jobId, $taskId)->find($imageId);
if ($model === null) {
return null;
}
// The stored file is always a JPEG, whatever the original was.
$name = pathinfo($model->original_name, PATHINFO_FILENAME);
return new TaskImageFile($thumbnail ? $model->thumbnail_path : $model->path, ($name === '' ? 'kuva' : $name).'.jpg');
}
/**
* @return Builder<TaskImageModel>
*/
private function ofTask(int $jobId, int $taskId): Builder
{
return TaskImageModel::query()
->where('task_id', $taskId)
->whereExists(fn ($query) => $query->selectRaw('1')->from('tasks')
->whereColumn('tasks.id', 'task_images.task_id')
->where('tasks.customer_job_id', $jobId));
}
}
@@ -0,0 +1,67 @@
<?php
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
use App\Modules\Jobs\Domain\Contracts\TaskImageRepository;
use App\Modules\Jobs\Domain\Entities\TaskImage;
use App\Modules\Jobs\Domain\ValueObjects\StoredTaskImage;
use App\Modules\Jobs\Domain\ValueObjects\TaskImageCaption;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskImageModel;
use DateTimeImmutable;
class EloquentTaskImageRepository implements TaskImageRepository
{
public function findForTask(int $taskId, int $imageId): ?TaskImage
{
$model = TaskImageModel::query()->where('task_id', $taskId)->find($imageId);
return $model === null ? null : $this->toEntity($model);
}
public function filePathsOfTask(int $taskId): array
{
return TaskImageModel::query()
->where('task_id', $taskId)
->get(['path', 'thumbnail_path'])
->flatMap(fn (TaskImageModel $model): array => [$model->path, $model->thumbnail_path])
->values()
->all();
}
public function save(TaskImage $image): TaskImage
{
$model = $image->id() === null ? new TaskImageModel : TaskImageModel::query()->findOrFail($image->id());
$model->fill([
'task_id' => $image->taskId(),
'user_id' => $image->uploaderId(),
'path' => $image->file()->path,
'thumbnail_path' => $image->file()->thumbnailPath,
'original_name' => $image->originalName(),
'caption' => $image->caption()?->value,
'width' => $image->file()->width,
'height' => $image->file()->height,
'created_at' => $image->uploadedAt(),
])->save();
return $this->toEntity($model->refresh());
}
public function delete(TaskImage $image): void
{
TaskImageModel::query()->whereKey($image->id())->delete();
}
private function toEntity(TaskImageModel $model): TaskImage
{
return TaskImage::reconstitute(
$model->id,
$model->task_id,
$model->user_id,
new StoredTaskImage($model->path, $model->thumbnail_path, $model->width, $model->height),
$model->original_name,
TaskImageCaption::fromNullable($model->caption),
DateTimeImmutable::createFromInterface($model->created_at),
);
}
}
@@ -152,6 +152,11 @@ class EloquentTaskQueries implements TaskQueries
->groupBy('task_id')
->selectRaw('task_id, count(*) as aggregate')
->pluck('aggregate', 'task_id');
$imageCounts = DB::table('task_images')
->whereIn('task_id', $models->pluck('id')->all())
->groupBy('task_id')
->selectRaw('task_id, count(*) as aggregate')
->pluck('aggregate', 'task_id');
$workTypeNames = [];
foreach ($models->pluck('work_type_id')->unique() as $workTypeId) {
@@ -176,6 +181,7 @@ class EloquentTaskQueries implements TaskQueries
plannedFrom: $model->planned_from?->format('Y-m-d'),
plannedTo: $model->planned_to?->format('Y-m-d'),
commentCount: (int) ($commentCounts[$model->id] ?? 0),
imageCount: (int) ($imageCounts[$model->id] ?? 0),
))->values()->all();
}
}