remove local file extensions and generalize some method names for cached media

file extensions may still be present in urls, but are ignored by the backend

MIGRATION (if you have any cached data worth keeping, not required):
in cache/images run "rename 's/\..*$//' *" i.e. strip file extensions
master
Andrew Dolgov 7 years ago
parent 63f0ed3d9c
commit 41bead9baa

@ -1046,9 +1046,12 @@ class Handler_Public extends Handler {
<?php
}
function cached_image() {
function cached_url() {
@$hash = basename($_GET['hash']);
// we don't need an extension to find the file, hash is a complete URL
$hash = preg_replace("/\.[^\.]*$/", "", $hash);
if ($hash) {
$filename = CACHE_DIR . '/images/' . $hash;

@ -913,11 +913,11 @@
// check cache only for video and images for the time being
if ($entry->nodeName == 'img' || ($entry->parentNode && $entry->parentNode->nodeName == "video")) {
$cached_filename = CACHE_DIR . '/images/' . sha1($src);
$extension = $entry->tagName == 'source' ? '.mp4' : '.png';
$cached_filename = CACHE_DIR . '/images/' . sha1($src) . $extension;
if (file_exists($cached_filename)) {
$src = get_self_url_prefix() . '/public.php?op=cached_image&hash=' . sha1($src) . $extension;
$src = get_self_url_prefix() . '/public.php?op=cached_url&hash=' . sha1($src) . $extension;
if ($entry->hasAttribute('srcset')) {
$entry->removeAttribute('srcset');

@ -2,7 +2,7 @@
define_default('DAEMON_UPDATE_LOGIN_LIMIT', 30);
define_default('DAEMON_FEED_LIMIT', 500);
define_default('DAEMON_SLEEP_INTERVAL', 120);
define_default('_MIN_CACHE_IMAGE_SIZE', 1024);
define_default('_MIN_CACHE_FILE_SIZE', 1024);
function calculate_article_hash($article, $pluginhost) {
$tmp = "";
@ -864,7 +864,7 @@
_debug("force catchup: $entry_force_catchup");
if ($cache_images && is_writable(CACHE_DIR . '/images'))
cache_images($entry_content, $site_url, $debug_enabled);
cache_media($entry_content, $site_url, $debug_enabled);
$entry_content = db_escape_string($entry_content, false);
@ -1227,7 +1227,7 @@
return $rss;
}
function cache_images($html, $site_url, $debug) {
function cache_media($html, $site_url, $debug) {
libxml_use_internal_errors(true);
$charset_hack = '<head>
@ -1244,15 +1244,14 @@
if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
$src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
$extension = $entry->tagName == 'source' ? '.mp4' : '.png';
$local_filename = CACHE_DIR . "/images/" . sha1($src) . $extension;
$local_filename = CACHE_DIR . "/images/" . sha1($src);
if ($debug) _debug("cache_images: downloading: $src to $local_filename");
if ($debug) _debug("cache_media: downloading: $src to $local_filename");
if (!file_exists($local_filename)) {
$file_content = fetch_file_contents($src);
if ($file_content && strlen($file_content) > _MIN_CACHE_IMAGE_SIZE) {
if ($file_content && strlen($file_content) > _MIN_CACHE_FILE_SIZE) {
file_put_contents($local_filename, $file_content);
}
} else {

@ -26,7 +26,7 @@ class Af_Zz_ImgProxy extends Plugin {
if (preg_match("/image/", $enc["content_type"]) || preg_match("/\.(jpe?g|png|gif|bmp)$/i", $enc["filename"])) {
$proxy_all = $this->host->get($this, "proxy_all");
$enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], 0, $proxy_all);
$enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], $proxy_all);
}
return $enc;
@ -39,7 +39,6 @@ class Af_Zz_ImgProxy extends Plugin {
public function imgproxy() {
$url = rewrite_relative_url(SELF_URL_PATH, $_REQUEST["url"]);
$kind = (int) $_REQUEST["kind"]; // 1 = video
// called without user context, let's just redirect to original URL
if (!$_SESSION["uid"]) {
@ -47,8 +46,7 @@ class Af_Zz_ImgProxy extends Plugin {
return;
}
$extension = $kind == 1 ? '.mp4' : '.png';
$local_filename = CACHE_DIR . "/images/" . sha1($url) . $extension;
$local_filename = CACHE_DIR . "/images/" . sha1($url);
if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; }
@ -107,7 +105,7 @@ class Af_Zz_ImgProxy extends Plugin {
}
}
function rewrite_url_if_needed($url, $kind, $all_remote = false) {
function rewrite_url_if_needed($url, $all_remote = false) {
$scheme = parse_url($url, PHP_URL_SCHEME);
if ($all_remote) {
@ -121,7 +119,7 @@ class Af_Zz_ImgProxy extends Plugin {
if (($scheme != 'https' && $scheme != "") || $is_remote) {
if (strpos($url, "data:") !== 0) {
$url = get_self_url_prefix() . "/public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&kind=$kind&url=" .
$url = get_self_url_prefix() . "/public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&url=" .
urlencode($url);
}
}
@ -140,7 +138,7 @@ class Af_Zz_ImgProxy extends Plugin {
$imgs = $xpath->query("//img[@src]");
foreach ($imgs as $img) {
$new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), 0, $proxy_all);
$new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), $proxy_all);
if ($new_src != $img->getAttribute("src")) {
$img->setAttribute("src", $new_src);
@ -154,7 +152,7 @@ class Af_Zz_ImgProxy extends Plugin {
foreach ($vids as $vid) {
if ($vid->hasAttribute("poster")) {
$new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), 0, $proxy_all);
$new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), $proxy_all);
if ($new_src != $vid->getAttribute("poster")) {
$vid->setAttribute("poster", $new_src);
@ -166,7 +164,7 @@ class Af_Zz_ImgProxy extends Plugin {
$vsrcs = $xpath->query("source", $vid);
foreach ($vsrcs as $vsrc) {
$new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), 1, $proxy_all);
$new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), $proxy_all);
if ($new_src != $vsrc->getAttribute("src")) {
$vid->setAttribute("src", $new_src);

Loading…
Cancel
Save