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:
+67
@@ -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),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user