aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/Thumbnailer.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2018-07-05 20:29:55 +0200
committerArthurHoaro <arthur@hoa.ro>2018-07-05 20:34:22 +0200
commitb302b3c584b84f22f0e6f187b072180ecbacdfab (patch)
tree297f72d8fcf6d158ebbb8198dee4e35a30ab0d7e /application/Thumbnailer.php
parentfcba541e2f12c85ac56c6915ba1319fbdd3e6962 (diff)
downloadShaarli-b302b3c584b84f22f0e6f187b072180ecbacdfab.tar.gz
Shaarli-b302b3c584b84f22f0e6f187b072180ecbacdfab.tar.zst
Shaarli-b302b3c584b84f22f0e6f187b072180ecbacdfab.zip
Thumbnails: add a common mode to only retrieve thumbs from popular media websites
Diffstat (limited to 'application/Thumbnailer.php')
-rw-r--r--application/Thumbnailer.php54
1 files changed, 52 insertions, 2 deletions
diff --git a/application/Thumbnailer.php b/application/Thumbnailer.php
index d2284e79..7d0d9c33 100644
--- a/application/Thumbnailer.php
+++ b/application/Thumbnailer.php
@@ -14,6 +14,27 @@ use WebThumbnailer\Application\ConfigManager as WTConfigManager;
14 */ 14 */
15class Thumbnailer 15class Thumbnailer
16{ 16{
17 const COMMON_MEDIA_DOMAINS = [
18 'imgur.com',
19 'flickr.com',
20 'youtube.com',
21 'wikimedia.org',
22 'redd.it',
23 'gfycat.com',
24 'media.giphy.com',
25 'twitter.com',
26 'twimg.com',
27 'instagram.com',
28 'pinterest.com',
29 'pinterest.fr',
30 'tumblr.com',
31 'deviantart.com',
32 ];
33
34 const MODE_ALL = 'all';
35 const MODE_COMMON = 'common';
36 const MODE_NONE = 'none';
37
17 /** 38 /**
18 * @var WebThumbnailer instance. 39 * @var WebThumbnailer instance.
19 */ 40 */
@@ -57,13 +78,42 @@ class Thumbnailer
57 */ 78 */
58 public function get($url) 79 public function get($url)
59 { 80 {
81 if ($this->conf->get('thumbnails.mode') === self::MODE_COMMON
82 && ! $this->isCommonMediaOrImage($url)
83 ) {
84 return false;
85 }
86
60 try { 87 try {
61 return $this->wt->thumbnail($url); 88 return $this->wt->thumbnail($url);
62 } catch (WebThumbnailerException $e) { 89 } catch (WebThumbnailerException $e) {
63 // Exceptions are only thrown in debug mode. 90 // Exceptions are only thrown in debug mode.
64 error_log(get_class($e) .': '. $e->getMessage()); 91 error_log(get_class($e) . ': ' . $e->getMessage());
65 return false;
66 } 92 }
93 return false;
94 }
95
96 /**
97 * We check weather the given URL is from a common media domain,
98 * or if the file extension is an image.
99 *
100 * @param string $url to check
101 *
102 * @return bool true if it's an image or from a common media domain, false otherwise.
103 */
104 public function isCommonMediaOrImage($url)
105 {
106 foreach (self::COMMON_MEDIA_DOMAINS as $domain) {
107 if (strpos($url, $domain) !== false) {
108 return true;
109 }
110 }
111
112 if (endsWith($url, '.jpg') || endsWith($url, '.png') || endsWith($url, '.jpeg')) {
113 return true;
114 }
115
116 return false;
67 } 117 }
68 118
69 /** 119 /**