From 6dfd5c8658ffd23ac45243a01a390e27100dcffc Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Sat, 18 Mar 2017 16:41:52 +0100 Subject: [PATCH] Fix: Added missing file download http caching --- protected/humhub/docs/CHANGELOG.md | 1 + .../modules/file/actions/DownloadAction.php | 38 ++++++++++++++++++- .../file/controllers/FileController.php | 12 +----- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/protected/humhub/docs/CHANGELOG.md b/protected/humhub/docs/CHANGELOG.md index 09c24606f1..13512820e9 100644 --- a/protected/humhub/docs/CHANGELOG.md +++ b/protected/humhub/docs/CHANGELOG.md @@ -94,6 +94,7 @@ HumHub Change Log - Enh: Added option to show/hide deactivated user content in stream - Enh: Allow any url route as homepage by homeUrl array application parameter - Fix #2255: Added missing Social Account Settings menu +- Fix: Added missing file download http caching 1.2.0-beta.2 (February 24, 2017) diff --git a/protected/humhub/modules/file/actions/DownloadAction.php b/protected/humhub/modules/file/actions/DownloadAction.php index 2d4b2d3e8c..6fcbb24edb 100644 --- a/protected/humhub/modules/file/actions/DownloadAction.php +++ b/protected/humhub/modules/file/actions/DownloadAction.php @@ -2,7 +2,7 @@ /** * @link https://www.humhub.org/ - * @copyright Copyright (c) 2016 HumHub GmbH & Co. KG + * @copyright Copyright (c) 2017 HumHub GmbH & Co. KG * @license https://www.humhub.com/licences */ @@ -13,6 +13,7 @@ use yii\web\HttpException; use yii\base\Action; use humhub\modules\file\models\File; use humhub\modules\file\libs\FileHelper; +use yii\filters\HttpCache; /** * DownloadAction @@ -23,6 +24,12 @@ use humhub\modules\file\libs\FileHelper; class DownloadAction extends Action { + /** + * @see HttpCache + * @var boolean enable Http Caching + */ + public $enableHttpCache = true; + /** * @var File the requested file object */ @@ -49,6 +56,35 @@ class DownloadAction extends Action $this->checkFileExists(); } + /** + * @inheritdoc + */ + public function beforeRun() + { + if (!parent::beforeRun()) { + return false; + } + if (!$this->enableHttpCache) { + return true; + } + + $httpCache = new HttpCache(); + $httpCache->lastModified = function() { + return Yii::$app->formatter->asTimestamp($this->file->updated_at); + }; + $httpCache->etagSeed = function() { + if (file_exists($this->getStoredFilePath())) { + return md5_file($this->getStoredFilePath()); + } + return null; + }; + if (!$httpCache->beforeAction($this)) { + return false; + } + + return true; + } + /** * @inheritdoc */ diff --git a/protected/humhub/modules/file/controllers/FileController.php b/protected/humhub/modules/file/controllers/FileController.php index 27e57b9d62..bffb2ac626 100644 --- a/protected/humhub/modules/file/controllers/FileController.php +++ b/protected/humhub/modules/file/controllers/FileController.php @@ -2,7 +2,7 @@ /** * @link https://www.humhub.org/ - * @copyright Copyright (c) 2015 HumHub GmbH & Co. KG + * @copyright Copyright (c) 2017 HumHub GmbH & Co. KG * @license https://www.humhub.com/licences */ @@ -33,16 +33,6 @@ class FileController extends \humhub\components\Controller 'class' => AccessControl::className(), 'guestAllowedActions' => ['download'] ], - /* - 'httpCache' => [ - 'class' => 'yii\filters\HttpCache', - 'only' => ['download'], - 'cacheControlHeader' => 'public, max-age=31536000', - 'etagSeed' => function ($action, $params) { - return serialize([\yii\helpers\Url::current()]); - }, - ], - */ ]; }