1
0
mirror of https://tt-rss.org/git/tt-rss.git synced 2024-06-26 11:59:02 +02:00

RSSUtils::cache_media, cache_enclosures: use DiskCache

This commit is contained in:
Andrew Dolgov 2019-08-14 12:15:56 +03:00
parent 39f459eb04
commit 84974c60a7

View File

@ -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,30 +1181,36 @@ class RSSUtils {
}
static function cache_enclosures($enclosures, $site_url) {
$cache = new DiskCache("images");
if ($cache->isWritable()) {
foreach ($enclosures as $enc) {
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);
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);
$cache->put($local_filename, $file_content);
}
} else if (is_writable($local_filename)) {
touch($local_filename);
$cache->touch($local_filename);
}
}
}
}
}
static function cache_media($html, $site_url) {
$cache = new DiskCache("images");
if ($cache->isWritable()) {
$doc = new DOMDocument();
if ($doc->loadHTML($html)) {
$xpath = new DOMXPath($doc);
@ -1215,20 +1221,21 @@ class RSSUtils {
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);
if (!file_exists($local_filename)) {
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);
$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);
}
}
}