cache_starred_images: also handle html5 mp4 video files

This commit is contained in:
Andrew Dolgov 2015-07-07 13:07:58 +03:00
parent b7d1306b19
commit 88bf000f8d

View file

@ -6,7 +6,7 @@ class Cache_Starred_Images extends Plugin implements IHandler {
function about() { function about() {
return array(1.0, return array(1.0,
"Automatically cache images in Starred articles", "Automatically cache Starred articles' images and HTML5 video files",
"fox", "fox",
true); true);
} }
@ -59,7 +59,8 @@ class Cache_Starred_Images extends Plugin implements IHandler {
if ($hash) { if ($hash) {
$filename = $this->cache_dir . "/" . $hash . '.png'; $filename = $this->cache_dir . "/" . $hash;
$is_video = strpos($filename, ".mp4") !== FALSE;
if (file_exists($filename)) { if (file_exists($filename)) {
/* See if we can use X-Sendfile */ /* See if we can use X-Sendfile */
@ -73,7 +74,7 @@ class Cache_Starred_Images extends Plugin implements IHandler {
header("Content-type: application/octet-stream"); header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
} else { } else {
header("Content-type: image/png"); header("Content-type: " . ($is_video ? "video/mp4" : "image/png"));
$stamp = gmdate("D, d M Y H:i:s", filemtime($filename)). " GMT"; $stamp = gmdate("D, d M Y H:i:s", filemtime($filename)). " GMT";
header("Last-Modified: $stamp", true); header("Last-Modified: $stamp", true);
readfile($filename); readfile($filename);
@ -86,7 +87,7 @@ class Cache_Starred_Images extends Plugin implements IHandler {
} }
function hook_house_keeping() { function hook_house_keeping() {
$files = glob($this->cache_dir . "/*.png"); $files = glob($this->cache_dir . "/*.{png,mp4}", GLOB_BRACE);
$last_article_id = 0; $last_article_id = 0;
$article_exists = 1; $article_exists = 1;
@ -113,18 +114,19 @@ class Cache_Starred_Images extends Plugin implements IHandler {
$xpath = new DOMXpath($doc); $xpath = new DOMXpath($doc);
if ($article_id) { if ($article_id) {
$entries = $xpath->query('(//img[@src])'); $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
foreach ($entries as $entry) { foreach ($entries as $entry) {
if ($entry->hasAttribute('src')) { if ($entry->hasAttribute('src')) {
$src = rewrite_relative_url($site_url, $entry->getAttribute('src')); $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
$local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . ".png"; $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
$local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
if (file_exists($local_filename)) { if (file_exists($local_filename)) {
$entry->setAttribute("src", get_self_url_prefix() . $entry->setAttribute("src", get_self_url_prefix() .
"/public.php?op=cache_starred_images_getimage&method=image&hash=" . "/public.php?op=cache_starred_images_getimage&method=image&hash=" .
$article_id . "-" . sha1($src)); $article_id . "-" . sha1($src) . $extension);
} }
} }
@ -140,12 +142,11 @@ class Cache_Starred_Images extends Plugin implements IHandler {
(ttrss_user_entries.feed_id = ttrss_feeds.id) (ttrss_user_entries.feed_id = ttrss_feeds.id)
WHERE ref_id = ttrss_entries.id AND WHERE ref_id = ttrss_entries.id AND
marked = true AND marked = true AND
UPPER(content) LIKE '%<IMG%' AND (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND
site_url != '' AND site_url != '' AND
plugin_data NOT LIKE '%starred_cache_images%' plugin_data NOT LIKE '%starred_cache_images%'
ORDER BY ".sql_random_function()." LIMIT 100"); ORDER BY ".sql_random_function()." LIMIT 100");
while ($line = db_fetch_assoc($result)) { while ($line = db_fetch_assoc($result)) {
if ($line["site_url"]) { if ($line["site_url"]) {
$success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]); $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
@ -170,17 +171,20 @@ class Cache_Starred_Images extends Plugin implements IHandler {
$doc->loadHTML($charset_hack . $content); $doc->loadHTML($charset_hack . $content);
$xpath = new DOMXPath($doc); $xpath = new DOMXPath($doc);
$entries = $xpath->query('(//img[@src])'); $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
$success = false; $success = false;
$has_images = false; $has_images = false;
foreach ($entries as $entry) { foreach ($entries as $entry) {
if ($entry->hasAttribute('src')) { if ($entry->hasAttribute('src')) {
$has_images = true; $has_images = true;
$src = rewrite_relative_url($site_url, $entry->getAttribute('src')); $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
$local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . ".png"; $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
$local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
//_debug("cache_images: downloading: $src to $local_filename"); //_debug("cache_images: downloading: $src to $local_filename");