summaryrefslogtreecommitdiff
path: root/classes
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2019-08-14 12:15:56 +0300
committerAndrew Dolgov <[email protected]>2019-08-14 12:15:56 +0300
commit84974c60a7aeed1fc2f22ef170c53fec5099abd0 (patch)
tree47f8215c0c9d0603cdfe794afe484c31b0fedff5 /classes
parent39f459eb0407cdf4bd0a072828300d161a0ef4e8 (diff)
RSSUtils::cache_media, cache_enclosures: use DiskCache
Diffstat (limited to 'classes')
-rwxr-xr-xclasses/rssutils.php65
1 files changed, 36 insertions, 29 deletions
diff --git a/classes/rssutils.php b/classes/rssutils.php
index 6ba5eaa0b..f942fad31 100755
--- a/classes/rssutils.php
+++ b/classes/rssutils.php
@@ -808,7 +808,7 @@ class RSSUtils {
Debug::log("force catchup: $entry_force_catchup", Debug::$LOG_VERBOSE);
- if ($cache_images && is_writable(CACHE_DIR . '/images'))
+ if ($cache_images)
RSSUtils::cache_media($entry_content, $site_url);
$csth = $pdo->prepare("SELECT id FROM ttrss_entries
@@ -1033,7 +1033,7 @@ class RSSUtils {
}
}
- if ($cache_images && is_writable(CACHE_DIR . '/images'))
+ if ($cache_images)
RSSUtils::cache_enclosures($enclosures, $site_url);
if (Debug::get_loglevel() >= Debug::$LOG_EXTENDED) {
@@ -1181,54 +1181,61 @@ class RSSUtils {
}
static function cache_enclosures($enclosures, $site_url) {
- foreach ($enclosures as $enc) {
+ $cache = new DiskCache("images");
- if (preg_match("/(image|audio|video)/", $enc[1])) {
+ if ($cache->isWritable()) {
+ foreach ($enclosures as $enc) {
- $src = rewrite_relative_url($site_url, $enc[0]);
+ if (preg_match("/(image|audio|video)/", $enc[1])) {
+ $src = rewrite_relative_url($site_url, $enc[0]);
- $local_filename = CACHE_DIR . "/images/" . sha1($src);
+ $local_filename = sha1($src);
- Debug::log("cache_enclosures: downloading: $src to $local_filename", Debug::$LOG_VERBOSE);
+ Debug::log("cache_enclosures: downloading: $src to $local_filename", Debug::$LOG_VERBOSE);
- if (!file_exists($local_filename)) {
- $file_content = fetch_file_contents($src);
+ if (!$cache->exists($local_filename)) {
+ $file_content = fetch_file_contents(array("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 && strlen($file_content) > MIN_CACHE_FILE_SIZE) {
+ $cache->put($local_filename, $file_content);
+ }
+ } else if (is_writable($local_filename)) {
+ $cache->touch($local_filename);
}
- } else if (is_writable($local_filename)) {
- touch($local_filename);
}
}
}
}
static function cache_media($html, $site_url) {
- $doc = new DOMDocument();
- if ($doc->loadHTML($html)) {
- $xpath = new DOMXPath($doc);
+ $cache = new DiskCache("images");
+
+ if ($cache->isWritable()) {
+ $doc = new DOMDocument();
+ if ($doc->loadHTML($html)) {
+ $xpath = new DOMXPath($doc);
- $entries = $xpath->query('(//img[@src])|(//video/source[@src])|(//audio/source[@src])');
+ $entries = $xpath->query('(//img[@src])|(//video/source[@src])|(//audio/source[@src])');
- foreach ($entries as $entry) {
- if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
- $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
+ foreach ($entries as $entry) {
+ if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
+ $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
- $local_filename = CACHE_DIR . "/images/" . sha1($src);
+ $local_filename = sha1($src);
- Debug::log("cache_media: checking $src", Debug::$LOG_VERBOSE);
+ Debug::log("cache_media: checking $src", Debug::$LOG_VERBOSE);
- if (!file_exists($local_filename)) {
- Debug::log("cache_media: downloading: $src to $local_filename", Debug::$LOG_VERBOSE);
+ if (!$cache->exists($local_filename)) {
+ Debug::log("cache_media: downloading: $src to $local_filename", Debug::$LOG_VERBOSE);
- $file_content = fetch_file_contents($src);
+ $file_content = fetch_file_contents(array("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 && strlen($file_content) > MIN_CACHE_FILE_SIZE) {
+ $cache->put($local_filename, $file_content);
+ }
+ } else if ($cache->isWritable($local_filename)) {
+ $cache->touch($local_filename);
}
- } else if (is_writable($local_filename)) {
- touch($local_filename);
}
}
}