]> git.wh0rd.org - tt-rss.git/commitdiff
enable caching of media in article enclosures
authorAndrew Dolgov <noreply@madoka.volgo-balt.ru>
Thu, 23 Mar 2017 12:19:25 +0000 (15:19 +0300)
committerAndrew Dolgov <noreply@madoka.volgo-balt.ru>
Thu, 23 Mar 2017 12:19:25 +0000 (15:19 +0300)
include/functions2.php
include/rssfuncs.php

index 9d6eb9330f07e385137b6264522a19ec2542f044..e00a0ba13305f15b0435cb1eed96c9b35a7f2e1b 100644 (file)
 
                if (db_num_rows($result) > 0) {
                        while ($line = db_fetch_assoc($result)) {
+
+                               if (file_exists(CACHE_DIR . '/images/' . sha1($line["content_url"]))) {
+                                       $line["content_url"] = get_self_url_prefix() . '/public.php?op=cached_url&hash=' . sha1($line["content_url"]);
+                               }
+
                                array_push($rv, $line);
                        }
                }
index 997ea4594b0feca2bbcc8e3d105e84339ef6db18..c03e6681e0e1b9c2128f265e2aa06948f16eb7a9 100644 (file)
                                        }
                                }
 
+                               if ($cache_images && is_writable(CACHE_DIR . '/images'))
+                                       cache_enclosures($enclosures, $site_url, $debug_enabled);
+
                                if ($debug_enabled) {
                                        _debug("article enclosures:", $debug_enabled);
                                        print_r($enclosures);
                return $rss;
        }
 
+       function cache_enclosures($enclosures, $site_url, $debug) {
+               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);
+
+                               if ($debug) _debug("cache_enclosures: downloading: $src to $local_filename");
+
+                               if (!file_exists($local_filename)) {
+                                       $file_content = fetch_file_contents($src);
+
+                                       if ($file_content && strlen($file_content) > _MIN_CACHE_FILE_SIZE) {
+                                               file_put_contents($local_filename, $file_content);
+                                       }
+                               } else {
+                                       touch($local_filename);
+                               }
+                       }
+               }
+       }
+
        function cache_media($html, $site_url, $debug) {
                libxml_use_internal_errors(true);
 
                $doc->loadHTML($charset_hack . $html);
                $xpath = new DOMXPath($doc);
 
-               $entries = $xpath->query('(//img[@src])|(//video/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) {