Add ETag caching for attachments
authorJoshua Rüsweg <josh@bastelstu.be>
Wed, 29 Oct 2014 19:16:12 +0000 (20:16 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Sat, 23 May 2015 15:19:17 +0000 (17:19 +0200)
Closes #1834

wcfsetup/install/files/lib/page/AttachmentPage.class.php

index c6e7de0da8113db4a56e554e69831834e34f16bf..9fad13d30b31078794ebc33301ccf7c9bb4224c0 100644 (file)
@@ -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 <http://opensource.org/licenses/lgpl-license.php>
  * @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);