From: Joshua Rüsweg Date: Wed, 29 Oct 2014 19:16:12 +0000 (+0100) Subject: Add ETag caching for attachments X-Git-Tag: 3.0.0_Beta_1~2331 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=b64a4a291ce7f0700f9e6f7c059e4bec1cd65235;p=GitHub%2FWoltLab%2FWCF.git Add ETag caching for attachments Closes #1834 --- diff --git a/wcfsetup/install/files/lib/page/AttachmentPage.class.php b/wcfsetup/install/files/lib/page/AttachmentPage.class.php index c6e7de0da8..9fad13d30b 100644 --- a/wcfsetup/install/files/lib/page/AttachmentPage.class.php +++ b/wcfsetup/install/files/lib/page/AttachmentPage.class.php @@ -11,7 +11,7 @@ use wcf\util\FileReader; /** * Shows an attachment. * - * @author Marcel Werk + * @author Marcel Werk, Joshua Rüsweg * @copyright 2001-2015 WoltLab GmbH * @license GNU Lesser General Public License * @package com.woltlab.wcf @@ -60,6 +60,12 @@ class AttachmentPage extends AbstractPage { */ public static $inlineMimeTypes = array('image/gif', 'image/jpeg', 'image/png', 'image/x-png', 'application/pdf', 'image/pjpeg'); + /** + * etag for this attachment + * @var string + */ + public $eTag = null; + /** * @see \wcf\page\IPage::readParameters() */ @@ -120,16 +126,19 @@ class AttachmentPage extends AbstractPage { $mimeType = $this->attachment->tinyThumbnailType; $filesize = $this->attachment->tinyThumbnailSize; $location = $this->attachment->getTinyThumbnailLocation(); + $this->eTag = 'TINY_'.$this->attachmentID; } else if ($this->thumbnail) { $mimeType = $this->attachment->thumbnailType; $filesize = $this->attachment->thumbnailSize; $location = $this->attachment->getThumbnailLocation(); + $this->eTag = 'THUMB_'.$this->attachmentID; } else { $mimeType = $this->attachment->fileType; $filesize = $this->attachment->filesize; $location = $this->attachment->getLocation(); + $this->eTag = $this->attachmentID; } // init file reader @@ -144,9 +153,8 @@ class AttachmentPage extends AbstractPage { 'maxAge' => 31536000 )); - // add etag for non-thumbnail - if (!$this->thumbnail && !$this->tiny) { - $this->fileReader->addHeader('ETag', '"'.$this->attachmentID.'"'); + if ($this->eTag !== null) { + $this->fileReader->addHeader('ETag', '"'.$this->eTag.'"'); } } @@ -156,6 +164,12 @@ class AttachmentPage extends AbstractPage { public function show() { parent::show(); + // etag caching + if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == '"'.$this->eTag.'"') { + @header('HTTP/1.1 304 Not Modified'); + exit; + } + if (!$this->tiny && !$this->thumbnail) { // update download count $editor = new AttachmentEditor($this->attachment);