From 3b5839031e819f16a27ddd7f60add85865c96ed2 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 20 Jun 2014 18:03:13 +0200 Subject: [PATCH] Added opt-in thumbnail proxying in upload Rationale: some services thought that images linked from /posts/upload are hotlinked. This is because they were receiving referrer set to a host that is unknown for them. By using proxying, referrer is blank, which increases thumbnail coverage at the expense of increased server-side traffic. --- data/config.ini | 1 + public_html/media/js/post-upload.js | 2 +- src/Controllers/PostController.php | 28 ++++++++++++++++++++++++++++ src/Router.php | 1 + 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/data/config.ini b/data/config.ini index 75ebd703..23177a5b 100644 --- a/data/config.ini +++ b/data/config.ini @@ -19,6 +19,7 @@ title = "szurubooru" [misc] featuredPostMaxDays=7 +proxyThumbsInUpload=0 debugQueries=0 githubLink = http://github.com/rr-/szurubooru diff --git a/public_html/media/js/post-upload.js b/public_html/media/js/post-upload.js index 77d385f7..85eb29e8 100644 --- a/public_html/media/js/post-upload.js +++ b/public_html/media/js/post-upload.js @@ -189,7 +189,7 @@ $(function() { $(img) .css('background-image', 'none') - .attr('src', url) + .attr('src', '/posts/upload/thumb/' + btoa(url)) .data('custom-thumb', true); } diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 9e684c8e..21f7a113 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -143,6 +143,34 @@ class PostController extends AbstractController $this->redirectToPostList(); } + public function uploadThumbnailView($url) + { + $url = base64_decode($url); + + if (!Core::getConfig()->misc->proxyThumbsInUpload) + { + $this->redirect($url); + return; + } + + $tmpPath = tempnam(sys_get_temp_dir(), 'thumb4upload'); + try + { + TransferHelper::download($url, $tmpPath); + $context = Core::getContext(); + $context->transport->lastModified = time(); + $context->transport->mimeType = mime_content_type($tmpPath); + $context->transport->cacheDaysToLive = 0.5; + $context->transport->fileHash = md5($url); + $context->transport->fileContent = file_get_contents($tmpPath); + $this->renderFile(); + } + finally + { + TransferHelper::remove($tmpPath); + } + } + public function editView($identifier) { $jobArgs = []; diff --git a/src/Router.php b/src/Router.php index feed8c2a..8ef719b8 100644 --- a/src/Router.php +++ b/src/Router.php @@ -56,6 +56,7 @@ class Router extends \Chibi\Routing\Router $this->register(['PostController', 'editAction'], 'POST', '/post/{identifier}/edit', $postValidation); $this->register(['PostController', 'deleteAction'], null, '/post/{identifier}/delete', $postValidation); + $this->register(['PostController', 'uploadThumbnailView'], 'GET', '/posts/upload/thumb/{url}', ['url' => '.*']); $this->register(['PostController', 'listView'], 'GET', '/{source}', $postValidation); $this->register(['PostController', 'listView'], 'GET', '/{source}/{page}', $postValidation); $this->register(['PostController', 'listView'], 'GET', '/{source}/{query}/{page}', $postValidation);