From fe451af7be8bbe3e07f7fa6f8bb9c817a85b7385 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 20 Jun 2014 18:31:43 +0200 Subject: [PATCH] Added cache to upload thumbnails proxy --- cache/.gitignore | 2 ++ data/config.ini | 1 + src/Controllers/PostController.php | 25 ++++++++++--------------- src/FileRenderer.php | 5 ++++- 4 files changed, 17 insertions(+), 16 deletions(-) create mode 100644 cache/.gitignore diff --git a/cache/.gitignore b/cache/.gitignore new file mode 100644 index 00000000..d6b7ef32 --- /dev/null +++ b/cache/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/data/config.ini b/data/config.ini index 23177a5b..7af3f9b0 100644 --- a/data/config.ini +++ b/data/config.ini @@ -3,6 +3,7 @@ dbDriver = "sqlite" dbLocation = "./data/db.sqlite" dbUser = "test" dbPass = "test" +cachePath = "./cache/" filesPath = "./data/files/" logsPath = "./data/logs/{yyyy}-{mm}.log" mediaPath = "./public_html/media/" diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index ef22b9d7..16c0606c 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -153,22 +153,17 @@ class PostController extends AbstractController return; } - $tmpPath = tempnam(sys_get_temp_dir(), 'thumb4upload'); - try - { + $tmpPath = Core::getConfig()->main->cachePath . DS . md5($url) . '.upload-cache'; + if (!file_exists($tmpPath)) TransferHelper::download($url, $tmpPath); - $options = new FileRendererOptions(); - $options->lastModified = time(); - $options->mimeType = mime_content_type($tmpPath); - $options->cacheDaysToLive = 0.5; - $options->fileHash = md5($url); - $options->fileContent = file_get_contents($tmpPath); - $this->renderFile($options); - } - finally - { - TransferHelper::remove($tmpPath); - } + + $options = new FileRendererOptions(); + $options->lastModified = time() - 3600; + $options->cacheDaysToLive = 0.5; + $options->mimeType = mime_content_type($tmpPath); + $options->fileHash = md5($url); + $options->fileContent = file_get_contents($tmpPath); + $this->renderFile($options); } public function editView($identifier) diff --git a/src/FileRenderer.php b/src/FileRenderer.php index 1fe6d45c..1401f2b0 100644 --- a/src/FileRenderer.php +++ b/src/FileRenderer.php @@ -28,7 +28,10 @@ class FileRenderer 'inline; filename="' . $options->customFileName . '"'); } - \Chibi\Util\Headers::set('Content-Type', $options->mimeType); + if (isset($options->mimeType)) + { + \Chibi\Util\Headers::set('Content-Type', $options->mimeType); + } if (strtotime($ifModifiedSince) == $lastModified or $eTagHeader == $eTag) {