1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Meema\MediaRecognition\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Aws\Sns\Message; |
6
|
|
|
use Illuminate\Routing\Controller; |
7
|
|
|
use Illuminate\Support\Facades\Http; |
8
|
|
|
use Meema\MediaRecognition\Events\FacialAnalysisCompleted; |
9
|
|
|
use Meema\MediaRecognition\Events\LabelAnalysisCompleted; |
10
|
|
|
use Meema\MediaRecognition\Events\ModerationAnalysisCompleted; |
11
|
|
|
use Meema\MediaRecognition\Events\TextAnalysisCompleted; |
12
|
|
|
use Meema\MediaRecognition\Facades\Recognize; |
13
|
|
|
|
14
|
|
|
class IncomingWebhookController extends Controller |
15
|
|
|
{ |
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->middleware('verify-signature'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @throws \Exception |
23
|
|
|
*/ |
24
|
|
|
public function __invoke() |
25
|
|
|
{ |
26
|
|
|
$message = $this->ensureSubscriptionIsConfirmed(); |
27
|
|
|
|
28
|
|
|
if ($message['Status'] !== 'SUCCEEDED') { |
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$arr = explode('_', $message['JobTag']); |
33
|
|
|
$type = $arr[0]; |
34
|
|
|
$mediaId = (int) $arr[1]; |
35
|
|
|
|
36
|
|
|
try { |
37
|
|
|
$this->fireEventFor($type, $message, $mediaId); |
38
|
|
|
} catch (\Exception $e) { |
39
|
|
|
throw new \Exception($e); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $type |
45
|
|
|
* @param array $message |
46
|
|
|
* @param int|null $mediaId |
47
|
|
|
* @throws \Exception |
48
|
|
|
*/ |
49
|
|
|
public function fireEventFor(string $type, array $message, int $mediaId = null) |
50
|
|
|
{ |
51
|
|
|
switch ($type) { |
52
|
|
|
case 'labels': |
53
|
|
|
Recognize::getLabelsByJobId($message['JobId'], $mediaId); |
54
|
|
|
event(new LabelAnalysisCompleted($message, $mediaId)); |
55
|
|
|
break; |
56
|
|
|
case 'faces': |
57
|
|
|
Recognize::getFacesByJobId($message['JobId'], $mediaId); |
58
|
|
|
event(new FacialAnalysisCompleted($message, $mediaId)); |
59
|
|
|
break; |
60
|
|
|
case 'moderation': |
61
|
|
|
Recognize::getContentModerationByJobId($message['JobId'], $mediaId); |
62
|
|
|
event(new ModerationAnalysisCompleted($message, $mediaId)); |
63
|
|
|
break; |
64
|
|
|
case 'ocr': |
65
|
|
|
Recognize::getTextDetectionByJobId($message['JobId'], $mediaId); |
66
|
|
|
event(new TextAnalysisCompleted($message, $mediaId)); |
67
|
|
|
break; |
68
|
|
|
default: |
69
|
|
|
throw new \Exception(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Because initially |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function ensureSubscriptionIsConfirmed(): array |
79
|
|
|
{ |
80
|
|
|
$message = Message::fromRawPostData()->toArray(); |
81
|
|
|
|
82
|
|
|
if (array_key_exists('SubscribeURL', $message)) { |
83
|
|
|
Http::get($message['SubscribeURL']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $message; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|