aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php19
-rw-r--r--tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php44
2 files changed, 24 insertions, 39 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index ac27e50a..59465ad1 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -54,7 +54,11 @@ class ContentProxy
54 54
55 if ((empty($content) || false === $this->validateContent($content)) && false === $disableContentUpdate) { 55 if ((empty($content) || false === $this->validateContent($content)) && false === $disableContentUpdate) {
56 $fetchedContent = $this->graby->fetchContent($url); 56 $fetchedContent = $this->graby->fetchContent($url);
57 $fetchedContent['title'] = $this->sanitizeContentTitle($fetchedContent['title'], $fetchedContent['content_type']); 57
58 $fetchedContent['title'] = $this->sanitizeContentTitle(
59 $fetchedContent['title'],
60 isset($fetchedContent['headers']['content-type']) ? $fetchedContent['headers']['content-type'] : ''
61 );
58 62
59 // when content is imported, we have information in $content 63 // when content is imported, we have information in $content
60 // in case fetching content goes bad, we'll keep the imported information instead of overriding them 64 // in case fetching content goes bad, we'll keep the imported information instead of overriding them
@@ -188,8 +192,8 @@ class ContentProxy
188 /** 192 /**
189 * Try to sanitize the title of the fetched content from wrong character encodings and invalid UTF-8 character. 193 * Try to sanitize the title of the fetched content from wrong character encodings and invalid UTF-8 character.
190 * 194 *
191 * @param $title 195 * @param string $title
192 * @param $contentType 196 * @param string $contentType
193 * 197 *
194 * @return string 198 * @return string
195 */ 199 */
@@ -293,12 +297,15 @@ class ContentProxy
293 } 297 }
294 298
295 // if content is an image, define it as a preview too 299 // if content is an image, define it as a preview too
296 if (!empty($content['headers']['content_type']) && \in_array($this->mimeGuesser->guess($content['headers']['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { 300 if (!empty($content['headers']['content-type']) && \in_array($this->mimeGuesser->guess($content['headers']['content-type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
297 $previewPictureUrl = $content['url']; 301 $previewPictureUrl = $content['url'];
302
303 $entry->setMimetype($content['headers']['content-type']);
298 } elseif (empty($previewPictureUrl)) { 304 } elseif (empty($previewPictureUrl)) {
299 $this->logger->debug('Extracting images from content to provide a default preview picture'); 305 $this->logger->debug('Extracting images from content to provide a default preview picture');
300 $imagesUrls = DownloadImages::extractImagesUrlsFromHtml($content['html']); 306 $imagesUrls = DownloadImages::extractImagesUrlsFromHtml($content['html']);
301 $this->logger->debug(\count($imagesUrls) . ' pictures found'); 307 $this->logger->debug(\count($imagesUrls) . ' pictures found');
308
302 if (!empty($imagesUrls)) { 309 if (!empty($imagesUrls)) {
303 $previewPictureUrl = $imagesUrls[0]; 310 $previewPictureUrl = $imagesUrls[0];
304 } 311 }
@@ -308,10 +315,6 @@ class ContentProxy
308 $this->updatePreviewPicture($entry, $previewPictureUrl); 315 $this->updatePreviewPicture($entry, $previewPictureUrl);
309 } 316 }
310 317
311 if (!empty($content['headers']['content-type'])) {
312 $entry->setMimetype($content['headers']['content-type']);
313 }
314
315 try { 318 try {
316 $this->tagger->tag($entry); 319 $this->tagger->tag($entry);
317 } catch (\Exception $e) { 320 } catch (\Exception $e) {
diff --git a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php
index 0199b3e4..3c720425 100644
--- a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php
+++ b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php
@@ -2,10 +2,7 @@
2 2
3namespace Tests\Wallabag\CoreBundle\Helper; 3namespace Tests\Wallabag\CoreBundle\Helper;
4 4
5use GuzzleHttp\Client;
6use GuzzleHttp\Psr7\Response; 5use GuzzleHttp\Psr7\Response;
7use GuzzleHttp\Stream\Stream;
8use GuzzleHttp\Subscriber\Mock;
9use Http\Mock\Client as HttpMockClient; 6use Http\Mock\Client as HttpMockClient;
10use Monolog\Handler\TestHandler; 7use Monolog\Handler\TestHandler;
11use Monolog\Logger; 8use Monolog\Logger;
@@ -153,20 +150,15 @@ class DownloadImagesTest extends TestCase
153 150
154 public function testProcessImageWithSrcset() 151 public function testProcessImageWithSrcset()
155 { 152 {
156 $client = new Client(); 153 $httpMockClient = new HttpMockClient();
157 154 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')));
158 $mock = new Mock([ 155 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')));
159 new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))), 156 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')));
160 new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))),
161 new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))),
162 ]);
163
164 $client->getEmitter()->attach($mock);
165 157
166 $logHandler = new TestHandler(); 158 $logHandler = new TestHandler();
167 $logger = new Logger('test', [$logHandler]); 159 $logger = new Logger('test', [$logHandler]);
168 160
169 $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); 161 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
170 $res = $download->processHtml(123, '<p><img class="alignnone wp-image-1153" src="http://piketty.blog.lemonde.fr/files/2017/10/F1FR-530x375.jpg" alt="" width="628" height="444" srcset="http://piketty.blog.lemonde.fr/files/2017/10/F1FR-530x375.jpg 530w, http://piketty.blog.lemonde.fr/files/2017/10/F1FR-768x543.jpg 768w, http://piketty.blog.lemonde.fr/files/2017/10/F1FR-900x636.jpg 900w" sizes="(max-width: 628px) 100vw, 628px" /></p>', 'http://piketty.blog.lemonde.fr/2017/10/12/budget-2018-la-jeunesse-sacrifiee/'); 162 $res = $download->processHtml(123, '<p><img class="alignnone wp-image-1153" src="http://piketty.blog.lemonde.fr/files/2017/10/F1FR-530x375.jpg" alt="" width="628" height="444" srcset="http://piketty.blog.lemonde.fr/files/2017/10/F1FR-530x375.jpg 530w, http://piketty.blog.lemonde.fr/files/2017/10/F1FR-768x543.jpg 768w, http://piketty.blog.lemonde.fr/files/2017/10/F1FR-900x636.jpg 900w" sizes="(max-width: 628px) 100vw, 628px" /></p>', 'http://piketty.blog.lemonde.fr/2017/10/12/budget-2018-la-jeunesse-sacrifiee/');
171 163
172 $this->assertNotContains('http://piketty.blog.lemonde.fr/', $res, 'Image srcset attribute were not replaced'); 164 $this->assertNotContains('http://piketty.blog.lemonde.fr/', $res, 'Image srcset attribute were not replaced');
@@ -174,20 +166,15 @@ class DownloadImagesTest extends TestCase
174 166
175 public function testProcessImageWithTrickySrcset() 167 public function testProcessImageWithTrickySrcset()
176 { 168 {
177 $client = new Client(); 169 $httpMockClient = new HttpMockClient();
178 170 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')));
179 $mock = new Mock([ 171 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')));
180 new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))), 172 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')));
181 new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))),
182 new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))),
183 ]);
184
185 $client->getEmitter()->attach($mock);
186 173
187 $logHandler = new TestHandler(); 174 $logHandler = new TestHandler();
188 $logger = new Logger('test', [$logHandler]); 175 $logger = new Logger('test', [$logHandler]);
189 176
190 $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); 177 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
191 $res = $download->processHtml(123, '<figure id="post-257260" class="align-none media-257260"><img src="https://cdn.css-tricks.com/wp-content/uploads/2017/08/the-critical-request.png" srcset="https://res.cloudinary.com/css-tricks/image/upload/c_scale,w_1000,f_auto,q_auto/v1501594717/the-critical-request_bqdfaa.png 1000w, https://res.cloudinary.com/css-tricks/image/upload/c_scale,w_200,f_auto,q_auto/v1501594717/the-critical-request_bqdfaa.png 200w" sizes="(min-width: 1850px) calc( (100vw - 555px) / 3 ) 178 $res = $download->processHtml(123, '<figure id="post-257260" class="align-none media-257260"><img src="https://cdn.css-tricks.com/wp-content/uploads/2017/08/the-critical-request.png" srcset="https://res.cloudinary.com/css-tricks/image/upload/c_scale,w_1000,f_auto,q_auto/v1501594717/the-critical-request_bqdfaa.png 1000w, https://res.cloudinary.com/css-tricks/image/upload/c_scale,w_200,f_auto,q_auto/v1501594717/the-critical-request_bqdfaa.png 200w" sizes="(min-width: 1850px) calc( (100vw - 555px) / 3 )
192 (min-width: 1251px) calc( (100vw - 530px) / 2 ) 179 (min-width: 1251px) calc( (100vw - 530px) / 2 )
193 (min-width: 1086px) calc(100vw - 480px) 180 (min-width: 1086px) calc(100vw - 480px)
@@ -199,18 +186,13 @@ class DownloadImagesTest extends TestCase
199 186
200 public function testProcessImageWithNullPath() 187 public function testProcessImageWithNullPath()
201 { 188 {
202 $client = new Client(); 189 $httpMockClient = new HttpMockClient();
203 190 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')));
204 $mock = new Mock([
205 new Response(200, ['content-type' => null], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))),
206 ]);
207
208 $client->getEmitter()->attach($mock);
209 191
210 $logHandler = new TestHandler(); 192 $logHandler = new TestHandler();
211 $logger = new Logger('test', [$logHandler]); 193 $logger = new Logger('test', [$logHandler]);
212 194
213 $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); 195 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
214 196
215 $res = $download->processSingleImage( 197 $res = $download->processSingleImage(
216 123, 198 123,