/* @var PluginHost $host */
private $host;
private $cache_dir;
+ private $max_cache_attempts = 5; // per-article
function about() {
return array(1.0,
* @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;
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>';
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 {