]> git.wh0rd.org Git - tt-rss.git/commitdiff
remove local file extensions and generalize some method names for cached media
authorAndrew Dolgov <noreply@madoka.volgo-balt.ru>
Thu, 23 Mar 2017 11:55:40 +0000 (14:55 +0300)
committerAndrew Dolgov <noreply@madoka.volgo-balt.ru>
Thu, 23 Mar 2017 11:55:40 +0000 (14:55 +0300)
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

classes/handler/public.php
include/functions2.php
include/rssfuncs.php
plugins/af_zz_imgproxy/init.php

index 35f677f94a00c933afab71b4614dd7ae69c40c38..a6bc3ff6ff3efae9f4f7bb093df47c1771655a1e 100644 (file)
@@ -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;
index 34f027a5558354f43df562e7ef7222cb97c1fbbd..39c7f746b6e0b01d8eb84b46826333b3474c2b75 100644 (file)
                                // 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');
index e26fc38beb9851d9c1897f6406f673234c34499a..997ea4594b0feca2bbcc8e3d105e84339ef6db18 100644 (file)
@@ -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 = "";
                                _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);
 
                return $rss;
        }
 
-       function cache_images($html, $site_url, $debug) {
+       function cache_media($html, $site_url, $debug) {
                libxml_use_internal_errors(true);
 
                $charset_hack = '<head>
                        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 {
index f326b8e07627c88500577c5050449e71a3ce5394..e0ec2f1745c8919d38dc6aefc130f2afd250cc2d 100644 (file)
@@ -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);