summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2021-11-15 23:22:21 +0300
committerAndrew Dolgov <[email protected]>2021-11-15 23:22:21 +0300
commitb2952843f50c7b5d2e8aafd62fadb4674acc59b1 (patch)
tree8a9c6fb117c74068669c1572ad305436e402ebfb
parent8cd69fe15c72982ad44d0ce5ba4b1454028408b5 (diff)
* DiskCache: add download() helper
* Af_Comics_Gocomics_FarSide: cache linked images because it seems to be required anyway
-rw-r--r--classes/diskcache.php20
-rw-r--r--plugins/af_comics/filters/af_comics_gocomics_farside.php16
2 files changed, 35 insertions, 1 deletions
diff --git a/classes/diskcache.php b/classes/diskcache.php
index ed334b2d2..0df8d7cd4 100644
--- a/classes/diskcache.php
+++ b/classes/diskcache.php
@@ -253,6 +253,26 @@ class DiskCache {
return touch($this->get_full_path($filename));
}
+ /** Downloads $url to cache as $local_filename if its missing (unless $force-ed)
+ * @param string $url
+ * @param string $local_filename
+ * @param array<string,string|int|false> $options (additional params to UrlHelper::fetch())
+ * @param bool $force
+ * @return bool
+ */
+ public function download(string $url, string $local_filename, array $options = [], bool $force = false) : bool {
+ if ($this->exists($local_filename) && !$force)
+ return true;
+
+ $data = UrlHelper::fetch(array_merge(["url" => $url,
+ "max_size" => Config::get(Config::MAX_CACHE_FILE_SIZE)], $options));
+
+ if ($data)
+ return $this->put($local_filename, $data) > 0;
+
+ return false;
+ }
+
public function get(string $filename): ?string {
if ($this->exists($filename))
return file_get_contents($this->get_full_path($filename));
diff --git a/plugins/af_comics/filters/af_comics_gocomics_farside.php b/plugins/af_comics/filters/af_comics_gocomics_farside.php
index 0399015ab..e4e230516 100644
--- a/plugins/af_comics/filters/af_comics_gocomics_farside.php
+++ b/plugins/af_comics/filters/af_comics_gocomics_farside.php
@@ -50,8 +50,22 @@ class Af_Comics_Gocomics_FarSide extends Af_ComicFilter {
if ($content_node) {
$imgs = $xpath->query('//img[@data-src]', $content_node);
+ $cache = new DiskCache("images");
+
foreach ($imgs as $img) {
- $img->setAttribute('src', $img->getAttribute('data-src'));
+ $image_url = $img->getAttribute('data-src');
+ $local_filename = sha1($image_url);
+
+ if ($image_url) {
+ $img->setAttribute('src', $image_url);
+
+ // try to cache image locally because they just 401 us otherwise
+ if (!$cache->exists($local_filename)) {
+ Debug::log("[Af_Comics_Gocomics_FarSide] caching: $image_url", Debug::LOG_VERBOSE);
+ $res = $cache->download($image_url, sha1($image_url), ["http_referrer" => $image_url]);
+ Debug::log("[Af_Comics_Gocomics_FarSide] cache result: $res", Debug::LOG_VERBOSE);
+ }
+ }
}
$junk_elems = $xpath->query("//*[@data-shareable-popover]");