]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Helper/DownloadImages.php
Add instance url to the downloaded images
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / DownloadImages.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Helper;
4
5 use Psr\Log\LoggerInterface;
6 use Symfony\Component\DomCrawler\Crawler;
7 use GuzzleHttp\Client;
8 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
9
10 class DownloadImages
11 {
12 const REGENERATE_PICTURES_QUALITY = 80;
13
14 private $client;
15 private $baseFolder;
16 private $logger;
17 private $mimeGuesser;
18 private $wallabagUrl;
19
20 public function __construct(Client $client, $baseFolder, LoggerInterface $logger)
21 {
22 $this->client = $client;
23 $this->baseFolder = $baseFolder;
24 $this->logger = $logger;
25 $this->mimeGuesser = new MimeTypeExtensionGuesser();
26
27 $this->setFolder();
28 }
29
30 /**
31 * Since we can't inject CraueConfig service because it'll generate a circular reference when injected in the subscriber
32 * we use a different way to inject the current wallabag url.
33 *
34 * @param string $url Usually from `$config->get('wallabag_url')`
35 */
36 public function setWallabagUrl($url)
37 {
38 $this->wallabagUrl = rtrim($url, '/');
39 }
40
41 /**
42 * Setup base folder where all images are going to be saved.
43 */
44 private function setFolder()
45 {
46 // if folder doesn't exist, attempt to create one and store the folder name in property $folder
47 if (!file_exists($this->baseFolder)) {
48 mkdir($this->baseFolder, 0777, true);
49 }
50 }
51
52 /**
53 * Process the html and extract image from it, save them to local and return the updated html.
54 *
55 * @param string $html
56 * @param string $url Used as a base path for relative image and folder
57 *
58 * @return string
59 */
60 public function processHtml($html, $url)
61 {
62 $crawler = new Crawler($html);
63 $result = $crawler
64 ->filterXpath('//img')
65 ->extract(array('src'));
66
67 $relativePath = $this->getRelativePath($url);
68
69 // download and save the image to the folder
70 foreach ($result as $image) {
71 $imagePath = $this->processSingleImage($image, $url, $relativePath);
72
73 if (false === $imagePath) {
74 continue;
75 }
76
77 $html = str_replace($image, $imagePath, $html);
78 }
79
80 return $html;
81 }
82
83 /**
84 * Process a single image:
85 * - retrieve it
86 * - re-saved it (for security reason)
87 * - return the new local path.
88 *
89 * @param string $imagePath Path to the image to retrieve
90 * @param string $url Url from where the image were found
91 * @param string $relativePath Relative local path to saved the image
92 *
93 * @return string Relative url to access the image from the web
94 */
95 public function processSingleImage($imagePath, $url, $relativePath = null)
96 {
97 if (null == $relativePath) {
98 $relativePath = $this->getRelativePath($url);
99 }
100
101 $folderPath = $this->baseFolder.'/'.$relativePath;
102
103 // build image path
104 $absolutePath = $this->getAbsoluteLink($url, $imagePath);
105 if (false === $absolutePath) {
106 $this->logger->log('error', 'Can not determine the absolute path for that image, skipping.');
107
108 return false;
109 }
110
111 try {
112 $res = $this->client->get($absolutePath);
113 } catch (\Exception $e) {
114 $this->logger->log('error', 'Can not retrieve image, skipping.', ['exception' => $e]);
115
116 return false;
117 }
118
119 $ext = $this->mimeGuesser->guess($res->getHeader('content-type'));
120 $this->logger->log('debug', 'Checking extension', ['ext' => $ext, 'header' => $res->getHeader('content-type')]);
121 if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'], true)) {
122 $this->logger->log('error', 'Processed image with not allowed extension. Skipping '.$imagePath);
123
124 return false;
125 }
126 $hashImage = hash('crc32', $absolutePath);
127 $localPath = $folderPath.'/'.$hashImage.'.'.$ext;
128
129 try {
130 $im = imagecreatefromstring($res->getBody());
131 } catch (\Exception $e) {
132 $im = false;
133 }
134
135 if (false === $im) {
136 $this->logger->log('error', 'Error while regenerating image', ['path' => $localPath]);
137
138 return false;
139 }
140
141 switch ($ext) {
142 case 'gif':
143 $result = imagegif($im, $localPath);
144 $this->logger->log('debug', 'Re-creating gif');
145 break;
146 case 'jpeg':
147 case 'jpg':
148 $result = imagejpeg($im, $localPath, self::REGENERATE_PICTURES_QUALITY);
149 $this->logger->log('debug', 'Re-creating jpg');
150 break;
151 case 'png':
152 $result = imagepng($im, $localPath, ceil(self::REGENERATE_PICTURES_QUALITY / 100 * 9));
153 $this->logger->log('debug', 'Re-creating png');
154 }
155
156 imagedestroy($im);
157
158 return $this->wallabagUrl.'/assets/images/'.$relativePath.'/'.$hashImage.'.'.$ext;
159 }
160
161 /**
162 * Generate the folder where we are going to save images based on the entry url.
163 *
164 * @param string $url
165 *
166 * @return string
167 */
168 private function getRelativePath($url)
169 {
170 $hashUrl = hash('crc32', $url);
171 $relativePath = $hashUrl[0].'/'.$hashUrl[1].'/'.$hashUrl;
172 $folderPath = $this->baseFolder.'/'.$relativePath;
173
174 if (!file_exists($folderPath)) {
175 mkdir($folderPath, 0777, true);
176 }
177
178 $this->logger->log('debug', 'Folder used for that url', ['folder' => $folderPath, 'url' => $url]);
179
180 return $relativePath;
181 }
182
183 /**
184 * Make an $url absolute based on the $base.
185 *
186 * @see Graby->makeAbsoluteStr
187 *
188 * @param string $base Base url
189 * @param string $url Url to make it absolute
190 *
191 * @return false|string
192 */
193 private function getAbsoluteLink($base, $url)
194 {
195 if (preg_match('!^https?://!i', $url)) {
196 // already absolute
197 return $url;
198 }
199
200 $base = new \SimplePie_IRI($base);
201
202 // remove '//' in URL path (causes URLs not to resolve properly)
203 if (isset($base->ipath)) {
204 $base->ipath = preg_replace('!//+!', '/', $base->ipath);
205 }
206
207 if ($absolute = \SimplePie_IRI::absolutize($base, $url)) {
208 return $absolute->get_uri();
209 }
210
211 $this->logger->log('error', 'Can not make an absolute link', ['base' => $base, 'url' => $url]);
212
213 return false;
214 }
215 }