summaryrefslogtreecommitdiff
path: root/plugins/cache_starred_images
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2018-11-30 07:20:13 +0300
committerAndrew Dolgov <[email protected]>2018-11-30 07:20:13 +0300
commit758752684c68efd179071cd77c92f78879e68f6d (patch)
treea4591a2c511e97e48513d98644e2b8996bc7fd3b /plugins/cache_starred_images
parenteaf7cfdba6af780d6a777dd1fe7d99c69778dbcd (diff)
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)
Diffstat (limited to 'plugins/cache_starred_images')
-rwxr-xr-xplugins/cache_starred_images/init.php32
1 files changed, 29 insertions, 3 deletions
diff --git a/plugins/cache_starred_images/init.php b/plugins/cache_starred_images/init.php
index 8a3464e0d..ae0eaf56d 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 = '<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 {