|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Afsy\Component\AMQP; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
6
|
|
|
use GuzzleHttp\Message\ResponseInterface; |
|
7
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface; |
|
8
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
|
9
|
|
|
|
|
10
|
|
|
class AfsyDownloadImageConsumer implements ConsumerInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var GuzzleClient |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $client; |
|
16
|
|
|
|
|
17
|
|
|
// Folders |
|
18
|
|
|
protected $createFolderMod = 0755; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Main constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param (GuzzleClient) $client Guzzle Client |
|
24
|
|
|
* @param (array) $options Array of options |
|
25
|
|
|
* |
|
26
|
|
|
* @return (void) |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(GuzzleClient $client, $options = []) |
|
29
|
|
|
{ |
|
30
|
|
|
// Initialize |
|
31
|
|
|
$this->client = $client; |
|
32
|
|
|
|
|
33
|
|
|
// Initialize options |
|
34
|
|
|
$this->createFolderMod = isset($options['createFolderMod']) ? $options['createFolderMod'] : $this->createFolderMod; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Main execute method |
|
39
|
|
|
* Execute actions for a given message. |
|
40
|
|
|
* |
|
41
|
|
|
* @param (AMQPMessage) $msg An instance of `PhpAmqpLib\Message\AMQPMessage` with the $msg->body being the data sent over RabbitMQ |
|
42
|
|
|
* |
|
43
|
|
|
* @return (boolean) Execution status (true if everything's of, false if message should be re-queued) |
|
44
|
|
|
*/ |
|
45
|
|
|
public function execute(AMQPMessage $msg) |
|
46
|
|
|
{ |
|
47
|
|
|
// Initialize |
|
48
|
|
|
$imageToDownload = unserialize($msg->body); |
|
49
|
|
|
|
|
50
|
|
|
// Download image |
|
51
|
|
|
if (!$this->downloadImageTo($imageToDownload['url'], $imageToDownload['savePath'])) { |
|
52
|
|
|
// Image should be downloaded again |
|
53
|
|
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Update saved html file |
|
57
|
|
|
$savedHtmlFileContent = file_get_contents($imageToDownload['savedHtmlFile']); |
|
58
|
|
|
|
|
59
|
|
|
// Update images paths |
|
60
|
|
|
$savedHtmlFileContent = str_replace($imageToDownload['url'], $imageToDownload['savePath'], $savedHtmlFileContent); |
|
61
|
|
|
|
|
62
|
|
|
// Save file |
|
63
|
|
|
return file_put_contents($imageToDownload['savedHtmlFile'], $savedHtmlFileContent) > 0; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Download an image to a given path. |
|
68
|
|
|
* |
|
69
|
|
|
* @param (string) $downloadImagePath Download image path |
|
70
|
|
|
* @param (string) $saveImagePath Save image path |
|
71
|
|
|
* |
|
72
|
|
|
* @return (boolean) Download status (or true if file already exists) |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function downloadImageTo($downloadImagePath, $saveImagePath) |
|
75
|
|
|
{ |
|
76
|
|
|
// Initialize |
|
77
|
|
|
$saveImageFolder = pathinfo($saveImagePath, PATHINFO_DIRNAME); |
|
78
|
|
|
$saveStatus = false; |
|
79
|
|
|
|
|
80
|
|
|
// Check if image already exists |
|
81
|
|
|
if (file_exists($saveImagePath)) { |
|
82
|
|
|
echo 'File "'.$saveImagePath.'" already exists'."\n"; |
|
83
|
|
|
|
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// Check if folder already exists |
|
88
|
|
|
if (!is_dir($saveImageFolder)) { |
|
89
|
|
|
// Initialize |
|
90
|
|
|
$createFolderMod = is_int($this->createFolderMod) ? $this->createFolderMod : (int) ($this->createFolderMod); |
|
91
|
|
|
|
|
92
|
|
|
// Create folder |
|
93
|
|
|
mkdir($saveImageFolder, $createFolderMod, true); |
|
94
|
|
|
echo 'Folder "'.$saveImageFolder.'" has been created.'."\n"; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Download image |
|
98
|
|
|
try { |
|
99
|
|
|
// Log download status |
|
100
|
|
|
echo 'Begin download of "'.$downloadImagePath.'".'."\n"; |
|
101
|
|
|
|
|
102
|
|
|
// Get image content |
|
103
|
|
|
/* @var ResponseInterface $imageContent */ |
|
104
|
|
|
$imageContent = $this->client->get($downloadImagePath); |
|
105
|
|
|
|
|
106
|
|
|
// Check content |
|
107
|
|
|
if (404 === $imageContent->getStatusCode()) { |
|
108
|
|
|
throw new \Exception('Error downloading file "'.$downloadImagePath.'" : returns a 404 page.', 1); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// Save image |
|
112
|
|
|
$saveStatus = file_put_contents($saveImagePath, $imageContent) > 0; |
|
113
|
|
|
|
|
114
|
|
|
// Log info |
|
115
|
|
|
echo 'Image "'.$saveImagePath.'" has been successfully downloaded!'."\n"; |
|
116
|
|
|
} catch (\Exception $e) { |
|
117
|
|
|
// Log error |
|
118
|
|
|
echo '#ERROR# Image "'.$downloadImagePath.'" was not downloaded! '."\n"; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
// Return save status |
|
122
|
|
|
return $saveStatus; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|