From b2952843f50c7b5d2e8aafd62fadb4674acc59b1 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 15 Nov 2021 23:22:21 +0300 Subject: * DiskCache: add download() helper * Af_Comics_Gocomics_FarSide: cache linked images because it seems to be required anyway --- classes/diskcache.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'classes/diskcache.php') 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 $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)); -- cgit v1.2.3-54-g00ecf