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)
This commit is contained in:
parent
eaf7cfdba6
commit
758752684c
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue