From b302b3c584b84f22f0e6f187b072180ecbacdfab Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 5 Jul 2018 20:29:55 +0200 Subject: Thumbnails: add a common mode to only retrieve thumbs from popular media websites --- application/PageBuilder.php | 6 ++++- application/Thumbnailer.php | 54 +++++++++++++++++++++++++++++++++++++++++++-- application/Updater.php | 5 +++-- 3 files changed, 60 insertions(+), 5 deletions(-) (limited to 'application') 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 @@ tpl->assign('tags', $this->linkDB->linksCountPerTag()); } - $this->tpl->assign('thumbnails_enabled', $this->conf->get('thumbnails.enabled')); + $this->tpl->assign( + 'thumbnails_enabled', + $this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE + ); $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width')); $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height')); 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; */ class Thumbnailer { + const COMMON_MEDIA_DOMAINS = [ + 'imgur.com', + 'flickr.com', + 'youtube.com', + 'wikimedia.org', + 'redd.it', + 'gfycat.com', + 'media.giphy.com', + 'twitter.com', + 'twimg.com', + 'instagram.com', + 'pinterest.com', + 'pinterest.fr', + 'tumblr.com', + 'deviantart.com', + ]; + + const MODE_ALL = 'all'; + const MODE_COMMON = 'common'; + const MODE_NONE = 'none'; + /** * @var WebThumbnailer instance. */ @@ -57,13 +78,42 @@ class Thumbnailer */ public function get($url) { + if ($this->conf->get('thumbnails.mode') === self::MODE_COMMON + && ! $this->isCommonMediaOrImage($url) + ) { + return false; + } + try { return $this->wt->thumbnail($url); } catch (WebThumbnailerException $e) { // Exceptions are only thrown in debug mode. - error_log(get_class($e) .': '. $e->getMessage()); - return false; + error_log(get_class($e) . ': ' . $e->getMessage()); } + return false; + } + + /** + * We check weather the given URL is from a common media domain, + * or if the file extension is an image. + * + * @param string $url to check + * + * @return bool true if it's an image or from a common media domain, false otherwise. + */ + public function isCommonMediaOrImage($url) + { + foreach (self::COMMON_MEDIA_DOMAINS as $domain) { + if (strpos($url, $domain) !== false) { + return true; + } + } + + if (endsWith($url, '.jpg') || endsWith($url, '.png') || endsWith($url, '.jpeg')) { + return true; + } + + return false; } /** 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 @@ use Shaarli\Config\ConfigJson; use Shaarli\Config\ConfigPhp; use Shaarli\Config\ConfigManager; +use Shaarli\Thumbnailer; /** * Class Updater. @@ -497,12 +498,12 @@ class Updater */ public function updateMethodWebThumbnailer() { - if ($this->conf->exists('thumbnails.enabled')) { + if ($this->conf->exists('thumbnails.mode')) { return true; } $thumbnailsEnabled = $this->conf->get('thumbnail.enable_thumbnails', true); - $this->conf->set('thumbnails.enabled', $thumbnailsEnabled); + $this->conf->set('thumbnails.mode', $thumbnailsEnabled ? Thumbnailer::MODE_ALL : Thumbnailer::MODE_NONE); $this->conf->set('thumbnails.width', 125); $this->conf->set('thumbnails.height', 90); $this->conf->remove('thumbnail'); -- cgit v1.2.3