diff --git a/src/app/DetectionEvent.php b/src/app/DetectionEvent.php index 65b4d2a..9534715 100644 --- a/src/app/DetectionEvent.php +++ b/src/app/DetectionEvent.php @@ -23,6 +23,7 @@ * @property Carbon|null $occurred_at * @property string|null $eventUrl * @property string|null $imageUrl + * @property string|null $imageDownload * @property-read Collection|AiPrediction[] $aiPredictions * @property-read int|null $ai_predictions_count * @property-read Collection|DetectionProfile[] $patternMatchedProfiles @@ -127,6 +128,15 @@ public function getEventUrlAttribute() } public function getImageUrlAttribute() + { + if ($this->imageFile) { + return url($this->imageFile->getStoragePath()); + } + + return null; + } + + public function getImageDownloadAttribute() { if ($this->id) { return url('/api/events/'.$this->id.'/img'); diff --git a/src/app/PayloadHelper.php b/src/app/PayloadHelper.php index d99d3d9..d4d3e74 100644 --- a/src/app/PayloadHelper.php +++ b/src/app/PayloadHelper.php @@ -11,7 +11,8 @@ class PayloadHelper '%profile_name%' => 'The name of the profile which triggered this automation', '%object_classes%' => 'The objects which triggered this automation, e.g. car,person,dog', '%event_url%' => 'Link to the event details page', - '%image_url%' => 'Direct link to the event image', + '%image_url%' => 'Direct link to the event image on the web server', + '%image_download_link%' => 'Resource link for downloading the image file' ]; public static function doReplacement( @@ -43,6 +44,9 @@ public static function doReplacement( if ($replacementString == '%image_url%') { return str_replace('%image_url%', $event->imageUrl, $payload); } + if ($replacementString == '%image_download_link%') { + return str_replace('%image_download_link%', $event->imageDownload, $payload); + } throw new Exception('Unrecognized replacement string '.$replacementString); } diff --git a/src/database/factories/ImageFileFactory.php b/src/database/factories/ImageFileFactory.php index 791dffc..25fc259 100644 --- a/src/database/factories/ImageFileFactory.php +++ b/src/database/factories/ImageFileFactory.php @@ -6,7 +6,7 @@ $factory->define(ImageFile::class, function () { return [ - 'path' => 'events/'.Str::random(40).'jpeg', + 'path' => 'events/'.Str::random(40).'.jpeg', 'file_name' => 'testimage.jpg', 'width' => 640, 'height' => 480, diff --git a/src/tests/Feature/ApiTest.php b/src/tests/Feature/ApiTest.php index dca47eb..986c120 100644 --- a/src/tests/Feature/ApiTest.php +++ b/src/tests/Feature/ApiTest.php @@ -1857,7 +1857,6 @@ public function api_can_get_available_replacements() { $this->json('GET', '/api/automations/replacements') ->assertStatus(200) - ->assertJsonCount(5, 'data') ->assertJsonStructure([ 'data' => [ '%image_file_name%', diff --git a/src/tests/Unit/PayloadTest.php b/src/tests/Unit/PayloadTest.php index 32cd827..f4cd3cc 100644 --- a/src/tests/Unit/PayloadTest.php +++ b/src/tests/Unit/PayloadTest.php @@ -112,7 +112,9 @@ public function automation_payload_can_replace_object_classes() */ public function automation_payload_can_replace_image_url() { - $imageFile = factory(ImageFile::class)->create(); + $imageFile = factory(ImageFile::class)->create([ + 'path' => 'events/g5Aqi4GzEXP7PYhh3Iy74vrGP3lhsnDum8UOGWS4.jpeg' + ]); $event = factory(DetectionEvent::class)->create([ 'image_file_id' => $imageFile, @@ -122,6 +124,6 @@ public function automation_payload_can_replace_image_url() $replaced = PayloadHelper::doReplacements($payload, $event, $profile); - $this->assertEquals('{"link"="http://unit.test:9999/api/events/'.$event->id.'/img"}', $replaced); + $this->assertEquals('{"link"="http://unit.test:9999/storage/events/g5Aqi4GzEXP7PYhh3Iy74vrGP3lhsnDum8UOGWS4.jpeg"}', $replaced); } }