1
0
mirror of https://git.tt-rss.org/git/tt-rss.git synced 2025-12-13 15:55:56 +00:00

diskcache: unify naming

This commit is contained in:
Andrew Dolgov
2021-02-15 16:11:30 +03:00
parent 8e79f1717d
commit 166f2d4666
8 changed files with 44 additions and 44 deletions

View File

@@ -348,7 +348,7 @@ class API extends Handler {
}, },
$hook_object); $hook_object);
$article['content'] = DiskCache::rewriteUrls($article['content']); $article['content'] = DiskCache::rewrite_urls($article['content']);
array_push($articles, $article); array_push($articles, $article);
@@ -792,7 +792,7 @@ class API extends Handler {
}, },
$hook_object); $hook_object);
$headline_row["content"] = DiskCache::rewriteUrls($headline_row['content']); $headline_row["content"] = DiskCache::rewrite_urls($headline_row['content']);
} }
array_push($headlines, $headline_row); array_push($headlines, $headline_row);

View File

@@ -508,7 +508,7 @@ class Article extends Handler_Protected {
while ($line = $sth->fetch(PDO::FETCH_ASSOC)) { while ($line = $sth->fetch(PDO::FETCH_ASSOC)) {
if ($cache->exists(sha1($line["content_url"]))) { if ($cache->exists(sha1($line["content_url"]))) {
$line["content_url"] = $cache->getUrl(sha1($line["content_url"])); $line["content_url"] = $cache->get_url(sha1($line["content_url"]));
} }
array_push($rv, $line); array_push($rv, $line);
@@ -678,10 +678,10 @@ class Article extends Handler_Protected {
$cache = new DiskCache("images"); $cache = new DiskCache("images");
if ($article_image && $cache->exists(sha1($article_image))) if ($article_image && $cache->exists(sha1($article_image)))
$article_image = $cache->getUrl(sha1($article_image)); $article_image = $cache->get_url(sha1($article_image));
if ($article_stream && $cache->exists(sha1($article_stream))) if ($article_stream && $cache->exists(sha1($article_stream)))
$article_stream = $cache->getUrl(sha1($article_stream)); $article_stream = $cache->get_url(sha1($article_stream));
return [$article_image, $article_stream, $article_kind]; return [$article_image, $article_stream, $article_kind];
} }

View File

@@ -194,20 +194,20 @@ class DiskCache {
$this->dir = CACHE_DIR . "/" . basename(clean($dir)); $this->dir = CACHE_DIR . "/" . basename(clean($dir));
} }
public function getDir() { public function get_dir() {
return $this->dir; return $this->dir;
} }
public function makeDir() { public function make_dir() {
if (!is_dir($this->dir)) { if (!is_dir($this->dir)) {
return mkdir($this->dir); return mkdir($this->dir);
} }
} }
public function isWritable($filename = "") { public function is_writable($filename = "") {
if ($filename) { if ($filename) {
if (file_exists($this->getFullPath($filename))) if (file_exists($this->get_full_path($filename)))
return is_writable($this->getFullPath($filename)); return is_writable($this->get_full_path($filename));
else else
return is_writable($this->dir); return is_writable($this->dir);
} else { } else {
@@ -216,44 +216,44 @@ class DiskCache {
} }
public function exists($filename) { public function exists($filename) {
return file_exists($this->getFullPath($filename)); return file_exists($this->get_full_path($filename));
} }
public function getSize($filename) { public function get_size($filename) {
if ($this->exists($filename)) if ($this->exists($filename))
return filesize($this->getFullPath($filename)); return filesize($this->get_full_path($filename));
else else
return -1; return -1;
} }
public function getFullPath($filename) { public function get_full_path($filename) {
return $this->dir . "/" . basename(clean($filename)); return $this->dir . "/" . basename(clean($filename));
} }
public function put($filename, $data) { public function put($filename, $data) {
return file_put_contents($this->getFullPath($filename), $data); return file_put_contents($this->get_full_path($filename), $data);
} }
public function touch($filename) { public function touch($filename) {
return touch($this->getFullPath($filename)); return touch($this->get_full_path($filename));
} }
public function get($filename) { public function get($filename) {
if ($this->exists($filename)) if ($this->exists($filename))
return file_get_contents($this->getFullPath($filename)); return file_get_contents($this->get_full_path($filename));
else else
return null; return null;
} }
public function getMimeType($filename) { public function get_mime_type($filename) {
if ($this->exists($filename)) if ($this->exists($filename))
return mime_content_type($this->getFullPath($filename)); return mime_content_type($this->get_full_path($filename));
else else
return null; return null;
} }
public function getFakeExtension($filename) { public function get_fake_extension($filename) {
$mimetype = $this->getMimeType($filename); $mimetype = $this->get_mime_type($filename);
if ($mimetype) if ($mimetype)
return isset($this->mimeMap[$mimetype]) ? $this->mimeMap[$mimetype] : ""; return isset($this->mimeMap[$mimetype]) ? $this->mimeMap[$mimetype] : "";
@@ -262,17 +262,17 @@ class DiskCache {
} }
public function send($filename) { public function send($filename) {
$fake_extension = $this->getFakeExtension($filename); $fake_extension = $this->get_fake_extension($filename);
if ($fake_extension) if ($fake_extension)
$fake_extension = ".$fake_extension"; $fake_extension = ".$fake_extension";
header("Content-Disposition: inline; filename=\"${filename}${fake_extension}\""); header("Content-Disposition: inline; filename=\"${filename}${fake_extension}\"");
return $this->send_local_file($this->getFullPath($filename)); return $this->send_local_file($this->get_full_path($filename));
} }
public function getUrl($filename) { public function get_url($filename) {
return get_self_url_prefix() . "/public.php?op=cached_url&file=" . basename($this->dir) . "/" . basename($filename); return get_self_url_prefix() . "/public.php?op=cached_url&file=" . basename($this->dir) . "/" . basename($filename);
} }
@@ -280,7 +280,7 @@ class DiskCache {
// this is called separately after sanitize() and plugin render article hooks to allow // this is called separately after sanitize() and plugin render article hooks to allow
// plugins work on original source URLs used before caching // plugins work on original source URLs used before caching
// NOTE: URLs should be already absolutized because this is called after sanitize() // NOTE: URLs should be already absolutized because this is called after sanitize()
static public function rewriteUrls($str) static public function rewrite_urls($str)
{ {
$res = trim($str); $res = trim($str);
if (!$res) return ''; if (!$res) return '';
@@ -301,7 +301,7 @@ class DiskCache {
$cached_filename = sha1($url); $cached_filename = sha1($url);
if ($cache->exists($cached_filename)) { if ($cache->exists($cached_filename)) {
$url = $cache->getUrl($cached_filename); $url = $cache->get_url($cached_filename);
$entry->setAttribute($attr, $url); $entry->setAttribute($attr, $url);
$entry->removeAttribute("srcset"); $entry->removeAttribute("srcset");
@@ -318,7 +318,7 @@ class DiskCache {
$cached_filename = sha1($matches[$i]["url"]); $cached_filename = sha1($matches[$i]["url"]);
if ($cache->exists($cached_filename)) { if ($cache->exists($cached_filename)) {
$matches[$i]["url"] = $cache->getUrl($cached_filename); $matches[$i]["url"] = $cache->get_url($cached_filename);
$need_saving = true; $need_saving = true;
} }

View File

@@ -260,7 +260,7 @@ class Feeds extends Handler_Protected {
$this->_mark_timestamp(" hook_render_cdm"); $this->_mark_timestamp(" hook_render_cdm");
$line['content'] = DiskCache::rewriteUrls($line['content']); $line['content'] = DiskCache::rewrite_urls($line['content']);
$this->_mark_timestamp(" disk_cache_rewrite"); $this->_mark_timestamp(" disk_cache_rewrite");

View File

@@ -106,7 +106,7 @@ class Handler_Public extends Handler {
$content = Sanitizer::sanitize($line["content"], false, $owner_uid, $content = Sanitizer::sanitize($line["content"], false, $owner_uid,
$feed_site_url, false, $line["id"]); $feed_site_url, false, $line["id"]);
$content = DiskCache::rewriteUrls($content); $content = DiskCache::rewrite_urls($content);
if ($line['note']) { if ($line['note']) {
$content = "<div style=\"$note_style\">Article note: " . $line['note'] . "</div>" . $content = "<div style=\"$note_style\">Article note: " . $line['note'] . "</div>" .
@@ -354,7 +354,7 @@ class Handler_Public extends Handler {
}, },
$line); $line);
$line['content'] = DiskCache::rewriteUrls($line['content']); $line['content'] = DiskCache::rewrite_urls($line['content']);
header("Content-Type: text/html"); header("Content-Type: text/html");

View File

@@ -1281,7 +1281,7 @@ class RSSUtils {
static function cache_enclosures($enclosures, $site_url) { static function cache_enclosures($enclosures, $site_url) {
$cache = new DiskCache("images"); $cache = new DiskCache("images");
if ($cache->isWritable()) { if ($cache->is_writable()) {
foreach ($enclosures as $enc) { foreach ($enclosures as $enc) {
if (preg_match("/(image|audio|video)/", $enc[1])) { if (preg_match("/(image|audio|video)/", $enc[1])) {
@@ -1335,7 +1335,7 @@ class RSSUtils {
} else { } else {
Debug::log("cache_media: failed with $fetch_last_error_code: $fetch_last_error"); Debug::log("cache_media: failed with $fetch_last_error_code: $fetch_last_error");
} }
} else if ($cache->isWritable($local_filename)) { } else if ($cache->is_writable($local_filename)) {
$cache->touch($local_filename); $cache->touch($local_filename);
} }
} }
@@ -1344,7 +1344,7 @@ class RSSUtils {
static function cache_media($html, $site_url) { static function cache_media($html, $site_url) {
$cache = new DiskCache("images"); $cache = new DiskCache("images");
if ($html && $cache->isWritable()) { if ($html && $cache->is_writable()) {
$doc = new DOMDocument(); $doc = new DOMDocument();
if (@$doc->loadHTML($html)) { if (@$doc->loadHTML($html)) {
$xpath = new DOMXPath($doc); $xpath = new DOMXPath($doc);

View File

@@ -59,14 +59,14 @@ class Af_Proxy_Http extends Plugin {
$local_filename = sha1($url); $local_filename = sha1($url);
if ($this->cache->exists($local_filename)) { if ($this->cache->exists($local_filename)) {
header("Location: " . $this->cache->getUrl($local_filename)); header("Location: " . $this->cache->get_url($local_filename));
return; return;
} else { } else {
$data = UrlHelper::fetch(["url" => $url, "max_size" => MAX_CACHE_FILE_SIZE]); $data = UrlHelper::fetch(["url" => $url, "max_size" => MAX_CACHE_FILE_SIZE]);
if ($data) { if ($data) {
if ($this->cache->put($local_filename, $data)) { if ($this->cache->put($local_filename, $data)) {
header("Location: " . $this->cache->getUrl($local_filename)); header("Location: " . $this->cache->get_url($local_filename));
return; return;
} }
} else { } else {

View File

@@ -17,18 +17,18 @@ class Cache_Starred_Images extends Plugin {
$this->host = $host; $this->host = $host;
$this->cache = new DiskCache("starred-images"); $this->cache = new DiskCache("starred-images");
if ($this->cache->makeDir()) if ($this->cache->make_dir())
chmod($this->cache->getDir(), 0777); chmod($this->cache->get_dir(), 0777);
if (!$this->cache->exists(".no-auto-expiry")) if (!$this->cache->exists(".no-auto-expiry"))
$this->cache->touch(".no-auto-expiry"); $this->cache->touch(".no-auto-expiry");
if ($this->cache->isWritable()) { if ($this->cache->is_writable()) {
$host->add_hook($host::HOOK_HOUSE_KEEPING, $this); $host->add_hook($host::HOOK_HOUSE_KEEPING, $this);
$host->add_hook($host::HOOK_ENCLOSURE_ENTRY, $this); $host->add_hook($host::HOOK_ENCLOSURE_ENTRY, $this);
$host->add_hook($host::HOOK_SANITIZE, $this); $host->add_hook($host::HOOK_SANITIZE, $this);
} else { } else {
user_error("Starred cache directory ".$this->cache->getDir()." is not writable.", E_USER_WARNING); user_error("Starred cache directory ".$this->cache->get_dir()." is not writable.", E_USER_WARNING);
} }
} }
@@ -69,9 +69,9 @@ class Cache_Starred_Images extends Plugin {
/* actual housekeeping */ /* actual housekeeping */
Debug::log("expiring " . $this->cache->getDir() . "..."); Debug::log("expiring " . $this->cache->get_dir() . "...");
$files = glob($this->cache->getDir() . "/*.{png,mp4,status}", GLOB_BRACE); $files = glob($this->cache->get_dir() . "/*.{png,mp4,status}", GLOB_BRACE);
$last_article_id = 0; $last_article_id = 0;
$article_exists = 1; $article_exists = 1;
@@ -98,7 +98,7 @@ class Cache_Starred_Images extends Plugin {
$local_filename = $article_id . "-" . sha1($enc["content_url"]); $local_filename = $article_id . "-" . sha1($enc["content_url"]);
if ($this->cache->exists($local_filename)) { if ($this->cache->exists($local_filename)) {
$enc["content_url"] = $this->cache->getUrl($local_filename); $enc["content_url"] = $this->cache->get_url($local_filename);
} }
return $enc; return $enc;
@@ -117,7 +117,7 @@ class Cache_Starred_Images extends Plugin {
$local_filename = $article_id . "-" . sha1($src); $local_filename = $article_id . "-" . sha1($src);
if ($this->cache->exists($local_filename)) { if ($this->cache->exists($local_filename)) {
$entry->setAttribute("src", $this->cache->getUrl($local_filename)); $entry->setAttribute("src", $this->cache->get_url($local_filename));
$entry->removeAttribute("srcset"); $entry->removeAttribute("srcset");
} }
} }
@@ -151,7 +151,7 @@ class Cache_Starred_Images extends Plugin {
$status_filename = $article_id . "-" . sha1($site_url) . ".status"; $status_filename = $article_id . "-" . sha1($site_url) . ".status";
/* housekeeping might run as a separate user, in this case status/media might not be writable */ /* housekeeping might run as a separate user, in this case status/media might not be writable */
if (!$this->cache->isWritable($status_filename)) { if (!$this->cache->is_writable($status_filename)) {
Debug::log("status not writable: $status_filename", Debug::$LOG_VERBOSE); Debug::log("status not writable: $status_filename", Debug::$LOG_VERBOSE);
return false; return false;
} }