Files
Toiminnanohjaus/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentTaskImageQueries.php
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

62 lines
2.2 KiB
PHP

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