Fix: Added missing file download http caching

This commit is contained in:
Lucas Bartholemy 2017-03-18 16:41:52 +01:00
parent a0d14ae7a8
commit 6dfd5c8658
3 changed files with 39 additions and 12 deletions

View File

@ -94,6 +94,7 @@ HumHub Change Log
- Enh: Added option to show/hide deactivated user content in stream - Enh: Added option to show/hide deactivated user content in stream
- Enh: Allow any url route as homepage by homeUrl array application parameter - Enh: Allow any url route as homepage by homeUrl array application parameter
- Fix #2255: Added missing Social Account Settings menu - Fix #2255: Added missing Social Account Settings menu
- Fix: Added missing file download http caching
1.2.0-beta.2 (February 24, 2017) 1.2.0-beta.2 (February 24, 2017)

View File

@ -2,7 +2,7 @@
/** /**
* @link https://www.humhub.org/ * @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 * @license https://www.humhub.com/licences
*/ */
@ -13,6 +13,7 @@ use yii\web\HttpException;
use yii\base\Action; use yii\base\Action;
use humhub\modules\file\models\File; use humhub\modules\file\models\File;
use humhub\modules\file\libs\FileHelper; use humhub\modules\file\libs\FileHelper;
use yii\filters\HttpCache;
/** /**
* DownloadAction * DownloadAction
@ -23,6 +24,12 @@ use humhub\modules\file\libs\FileHelper;
class DownloadAction extends Action class DownloadAction extends Action
{ {
/**
* @see HttpCache
* @var boolean enable Http Caching
*/
public $enableHttpCache = true;
/** /**
* @var File the requested file object * @var File the requested file object
*/ */
@ -49,6 +56,35 @@ class DownloadAction extends Action
$this->checkFileExists(); $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 * @inheritdoc
*/ */

View File

@ -2,7 +2,7 @@
/** /**
* @link https://www.humhub.org/ * @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 * @license https://www.humhub.com/licences
*/ */
@ -33,16 +33,6 @@ class FileController extends \humhub\components\Controller
'class' => AccessControl::className(), 'class' => AccessControl::className(),
'guestAllowedActions' => ['download'] 'guestAllowedActions' => ['download']
], ],
/*
'httpCache' => [
'class' => 'yii\filters\HttpCache',
'only' => ['download'],
'cacheControlHeader' => 'public, max-age=31536000',
'etagSeed' => function ($action, $params) {
return serialize([\yii\helpers\Url::current()]);
},
],
*/
]; ];
} }