Skip to content

Commit

Permalink
%image_url% now replaced with direct link to the image on the web ser…
Browse files Browse the repository at this point in the history
…ver instead of the "/img" resource url. Added new variable %image_download_link% for the "/img" resource url.
  • Loading branch information
akmolina28 committed Apr 20, 2021
1 parent 1c5e0e3 commit acb73d2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/app/DetectionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand Down
6 changes: 5 additions & 1 deletion src/app/PayloadHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/database/factories/ImageFileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/tests/Feature/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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%',
Expand Down
6 changes: 4 additions & 2 deletions src/tests/Unit/PayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
}

0 comments on commit acb73d2

Please sign in to comment.