From 758752684c68efd179071cd77c92f78879e68f6d Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 30 Nov 2018 07:20:13 +0300 Subject: [PATCH] cache_starred_articles: limit maximum amount of download attempts per-article, consider cache operation a success even if all images were too small (prevents repeated requests) --- plugins/cache_starred_images/init.php | 32 ++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/plugins/cache_starred_images/init.php b/plugins/cache_starred_images/init.php index 8a3464e0..ae0eaf56 100755 --- a/plugins/cache_starred_images/init.php +++ b/plugins/cache_starred_images/init.php @@ -4,6 +4,7 @@ class Cache_Starred_Images extends Plugin implements IHandler { /* @var PluginHost $host */ private $host; private $cache_dir; + private $max_cache_attempts = 5; // per-article function about() { return array(1.0, @@ -83,7 +84,7 @@ class Cache_Starred_Images extends Plugin implements IHandler { * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ function hook_house_keeping() { - $files = glob($this->cache_dir . "/*.{png,mp4}", GLOB_BRACE); + $files = glob($this->cache_dir . "/*.{png,mp4,status}", GLOB_BRACE); $last_article_id = 0; $article_exists = 1; @@ -167,6 +168,28 @@ class Cache_Starred_Images extends Plugin implements IHandler { function cache_article_images($content, $site_url, $owner_uid, $article_id) { libxml_use_internal_errors(true); + $status_filename = $this->cache_dir . $article_id . "-" . sha1($site_url) . ".status"; + + //_debug("status: $status_filename"); return; + + if (file_exists($status_filename)) + $status = json_decode(file_get_contents($status_filename), true); + else + $status = []; + + $status["attempt"] += 1; + + // only allow several download attempts for article + if ($status["attempt"] > $this->max_cache_attempts) { + _debug("too many attempts for $site_url"); + return; + } + + if (!file_put_contents($status_filename, json_encode($status))) { + user_error("unable to write status file: $status_filename", E_USER_WARNING); + return; + } + $charset_hack = ' '; @@ -196,8 +219,11 @@ class Cache_Starred_Images extends Plugin implements IHandler { if (!file_exists($local_filename)) { $file_content = fetch_file_contents(["url" => $src, "max_size" => MAX_CACHE_FILE_SIZE]); - if ($file_content && strlen($file_content) > MIN_CACHE_FILE_SIZE) { - file_put_contents($local_filename, $file_content); + if ($file_content) { + if (strlen($file_content) > MIN_CACHE_FILE_SIZE) { + file_put_contents($local_filename, $file_content); + } + $success = true; } } else { -- 2.39.2