]> git.wh0rd.org - tt-rss.git/commitdiff
cache_starred_articles: limit maximum amount of download attempts per-article, consid...
authorAndrew Dolgov <noreply@fakecake.org>
Fri, 30 Nov 2018 04:20:13 +0000 (07:20 +0300)
committerAndrew Dolgov <noreply@fakecake.org>
Fri, 30 Nov 2018 04:20:13 +0000 (07:20 +0300)
plugins/cache_starred_images/init.php

index 8a3464e0d758abb41817d018016d1e67e967cbec..ae0eaf56d679eb3834eab78d50ee3b403192cb74 100755 (executable)
@@ -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 = '<head>
                        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                </head>';
@@ -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 {