diff options
author | ArthurHoaro <arthur@hoa.ro> | 2018-07-05 20:29:55 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2018-07-05 20:34:22 +0200 |
commit | b302b3c584b84f22f0e6f187b072180ecbacdfab (patch) | |
tree | 297f72d8fcf6d158ebbb8198dee4e35a30ab0d7e /application | |
parent | fcba541e2f12c85ac56c6915ba1319fbdd3e6962 (diff) | |
download | Shaarli-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')
-rw-r--r-- | application/PageBuilder.php | 6 | ||||
-rw-r--r-- | application/Thumbnailer.php | 54 | ||||
-rw-r--r-- | application/Updater.php | 5 |
3 files changed, 60 insertions, 5 deletions
diff --git a/application/PageBuilder.php b/application/PageBuilder.php index 5da70811..b1abe0d0 100644 --- a/application/PageBuilder.php +++ b/application/PageBuilder.php | |||
@@ -1,6 +1,7 @@ | |||
1 | <?php | 1 | <?php |
2 | 2 | ||
3 | use Shaarli\Config\ConfigManager; | 3 | use Shaarli\Config\ConfigManager; |
4 | use Shaarli\Thumbnailer; | ||
4 | 5 | ||
5 | /** | 6 | /** |
6 | * This class is in charge of building the final page. | 7 | * This class is in charge of building the final page. |
@@ -119,7 +120,10 @@ class PageBuilder | |||
119 | $this->tpl->assign('tags', $this->linkDB->linksCountPerTag()); | 120 | $this->tpl->assign('tags', $this->linkDB->linksCountPerTag()); |
120 | } | 121 | } |
121 | 122 | ||
122 | $this->tpl->assign('thumbnails_enabled', $this->conf->get('thumbnails.enabled')); | 123 | $this->tpl->assign( |
124 | 'thumbnails_enabled', | ||
125 | $this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE | ||
126 | ); | ||
123 | $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width')); | 127 | $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width')); |
124 | $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height')); | 128 | $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height')); |
125 | 129 | ||
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 | */ |
15 | class Thumbnailer | 15 | class 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 | /** |
diff --git a/application/Updater.php b/application/Updater.php index f6b9e205..2a4c807c 100644 --- a/application/Updater.php +++ b/application/Updater.php | |||
@@ -2,6 +2,7 @@ | |||
2 | use Shaarli\Config\ConfigJson; | 2 | use Shaarli\Config\ConfigJson; |
3 | use Shaarli\Config\ConfigPhp; | 3 | use Shaarli\Config\ConfigPhp; |
4 | use Shaarli\Config\ConfigManager; | 4 | use Shaarli\Config\ConfigManager; |
5 | use Shaarli\Thumbnailer; | ||
5 | 6 | ||
6 | /** | 7 | /** |
7 | * Class Updater. | 8 | * Class Updater. |
@@ -497,12 +498,12 @@ class Updater | |||
497 | */ | 498 | */ |
498 | public function updateMethodWebThumbnailer() | 499 | public function updateMethodWebThumbnailer() |
499 | { | 500 | { |
500 | if ($this->conf->exists('thumbnails.enabled')) { | 501 | if ($this->conf->exists('thumbnails.mode')) { |
501 | return true; | 502 | return true; |
502 | } | 503 | } |
503 | 504 | ||
504 | $thumbnailsEnabled = $this->conf->get('thumbnail.enable_thumbnails', true); | 505 | $thumbnailsEnabled = $this->conf->get('thumbnail.enable_thumbnails', true); |
505 | $this->conf->set('thumbnails.enabled', $thumbnailsEnabled); | 506 | $this->conf->set('thumbnails.mode', $thumbnailsEnabled ? Thumbnailer::MODE_ALL : Thumbnailer::MODE_NONE); |
506 | $this->conf->set('thumbnails.width', 125); | 507 | $this->conf->set('thumbnails.width', 125); |
507 | $this->conf->set('thumbnails.height', 90); | 508 | $this->conf->set('thumbnails.height', 90); |
508 | $this->conf->remove('thumbnail'); | 509 | $this->conf->remove('thumbnail'); |