|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * Copyright (c) 2017-2018 Tobias Reich |
| 6 | + * Copyright (c) 2018-2026 LycheeOrg. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace App\Jobs; |
| 10 | + |
| 11 | +use App\Enum\JobStatus; |
| 12 | +use App\Metadata\Extractor; |
| 13 | +use App\Metadata\Geodecoder; |
| 14 | +use App\Models\JobHistory; |
| 15 | +use App\Models\Photo; |
| 16 | +use Illuminate\Bus\Queueable; |
| 17 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 18 | +use Illuminate\Foundation\Bus\Dispatchable; |
| 19 | +use Illuminate\Queue\InteractsWithQueue; |
| 20 | +use Illuminate\Queue\Middleware\RateLimited; |
| 21 | +use Illuminate\Queue\SerializesModels; |
| 22 | +use Illuminate\Support\Facades\Log; |
| 23 | +use Illuminate\Support\Str; |
| 24 | + |
| 25 | +/** |
| 26 | + * Asynchronously reverse-geocodes the GPS coordinates of a photo and |
| 27 | + * stores the resolved location string on the photo record. |
| 28 | + */ |
| 29 | +class GeodecodeLocationJob implements ShouldQueue |
| 30 | +{ |
| 31 | + use HasFailedTrait; |
| 32 | + use Dispatchable; |
| 33 | + use InteractsWithQueue; |
| 34 | + use Queueable; |
| 35 | + use SerializesModels; |
| 36 | + |
| 37 | + protected JobHistory $history; |
| 38 | + public string $photo_id; |
| 39 | + |
| 40 | + /** |
| 41 | + * Create a new job instance. |
| 42 | + */ |
| 43 | + public function __construct(Photo $photo) |
| 44 | + { |
| 45 | + $this->photo_id = $photo->id; |
| 46 | + |
| 47 | + $this->history = new JobHistory(); |
| 48 | + $this->history->owner_id = $photo->owner_id; |
| 49 | + $this->history->job = Str::limit(sprintf('Geodecode location for %s [%s].', $photo->title, $this->photo_id), 200); |
| 50 | + $this->history->status = JobStatus::READY; |
| 51 | + |
| 52 | + $this->history->save(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Get the middleware the job should pass through. |
| 57 | + * |
| 58 | + * @return array |
| 59 | + */ |
| 60 | + public function middleware() |
| 61 | + { |
| 62 | + return [new RateLimited('geo-queue')]; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Execute the job. |
| 67 | + */ |
| 68 | + public function handle(): void |
| 69 | + { |
| 70 | + Log::channel('jobs')->info("Starting geodecode location job for photo ID {$this->photo_id}."); |
| 71 | + $this->history->status = JobStatus::STARTED; |
| 72 | + $this->history->save(); |
| 73 | + |
| 74 | + $photo = Photo::findOrFail($this->photo_id); |
| 75 | + |
| 76 | + if ($photo->latitude === null || $photo->longitude === null) { |
| 77 | + $this->history->status = JobStatus::SUCCESS; |
| 78 | + $this->history->save(); |
| 79 | + |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + $cached_provider = Geodecoder::getGeocoderProvider(); |
| 84 | + $location = Geodecoder::decodeLocation_core($photo->latitude, $photo->longitude, $cached_provider); |
| 85 | + |
| 86 | + if ($location !== null) { |
| 87 | + $location = substr($location, 0, Extractor::MAX_LOCATION_STRING_LENGTH); |
| 88 | + } |
| 89 | + |
| 90 | + Photo::query() |
| 91 | + ->where('id', '=', $this->photo_id) |
| 92 | + ->where(function ($query): void { |
| 93 | + $query->whereNull('location')->orWhere('location', '=', ''); |
| 94 | + }) |
| 95 | + ->update(['location' => $location]); |
| 96 | + |
| 97 | + $this->history->status = JobStatus::SUCCESS; |
| 98 | + $this->history->save(); |
| 99 | + } |
| 100 | +} |
0 commit comments