Improved FileReader class and fixed issue in AttachmentPage
authorSebastian Öttl <sebastian.oettl@wcfsolutions.com>
Sat, 24 Aug 2013 15:42:25 +0000 (17:42 +0200)
committerSebastian Öttl <sebastian.oettl@wcfsolutions.com>
Sat, 24 Aug 2013 15:42:25 +0000 (17:42 +0200)
wcfsetup/install/files/lib/page/AttachmentPage.class.php
wcfsetup/install/files/lib/util/FileReader.class.php

index a2dc17331427986fccdee197da193a4928abb63e..ee64f8e750a556cb934a3cb9844202ba4a3937f8 100644 (file)
@@ -135,7 +135,7 @@ class AttachmentPage extends AbstractPage {
                
                // add etag for non-thumbnail
                if (!$this->thumbnail && !$this->tiny) {
-                       $this->fileReader->addHeader('ETag', '"'.$this->attachmentID."'");
+                       $this->fileReader->addHeader('ETag', '"'.$this->attachmentID.'"');
                }
        }
        
index 5a164cd5cf14ab59533c0845c9eb2fb776cebcc7..a206589216bf347c1fe8c53c1e0fafb7b81b0285 100644 (file)
@@ -14,12 +14,12 @@ use wcf\system\Regex;
  * @subpackage util
  * @category   Community Framework
  */
-final class FileReader {
+class FileReader {
        /**
         * http options
         * @var array
         */
-       private $options = array(
+       protected $options = array(
                'filename' => '',
                'mimeType' => 'application/octet-stream',
                'filesize' => 0,
@@ -34,25 +34,25 @@ final class FileReader {
         * list of header items
         * @var array
         */
-       private $headers = array();
+       protected $headers = array();
        
        /**
         * start byte
         * @var integer
         */
-       private $startByte = 0;
+       protected $startByte = 0;
        
        /**
         * end byte
         * @var integer
         */
-       private $endByte = 0;
+       protected $endByte = 0;
        
        /**
         * True if http range is invalid.
         * @var boolean
         */
-       private $invalidRange = false;
+       protected $invalidRange = false;
        
        /**
         * Creates a new instance of the HTTPFileReader class.
@@ -85,46 +85,22 @@ final class FileReader {
                if (empty($this->options['filesize'])) {
                        $this->options['filesize'] = @filesize($this->location);
                }
-                       
-               // handle range
-               $this->handleRange();
                
-               // handle headers
+               // prepare range and headers
+               $this->handleRange();
                $this->handleHeaders();
                
-               // show headers
-               foreach ($this->headers as $name => $value) {
-                       if (empty($name)) {
-                               @header($value);
-                       }
-                       else {
-                               @header($name.': '.$value);
-                       }
-               }
-               
-               // show file
+               // send file to client
+               $this->sendHeaders();
                if (!$this->invalidRange) {
-                       if ($this->startByte > 0 || $this->endByte < $this->options['filesize'] - 1) {
-                               $file = new File($this->location, 'rb');
-                               if ($this->startByte > 0) $file->seek($this->startByte);
-                               while ($this->startByte <= $this->endByte) {
-                                       $remainingBytes = $this->endByte - $this->startByte;
-                                       $readBytes = ($remainingBytes > 1048576 ? 1048576 : $remainingBytes + 1);
-                                       echo $file->read($readBytes);
-                                       $this->startByte += $readBytes;
-                               }
-                               $file->close();
-                       }
-                       else {
-                               readfile($this->location);
-                       }
+                       $this->sendFile();
                }
        }
        
        /**
         * Handles the given range options.
         */
-       private function handleRange() {
+       protected function handleRange() {
                $this->startByte = 0;
                $this->endByte = $this->options['filesize'] - 1;
                if ($this->options['enableRangeSupport']) {
@@ -153,7 +129,7 @@ final class FileReader {
        /**
         * Handles the given header items.
         */
-       private function handleHeaders() {
+       protected function handleHeaders() {
                if ($this->startByte < 0 || $this->startByte >= $this->options['filesize'] || $this->endByte >= $this->options['filesize']) {
                        // invalid range given
                        $this->addHeader('', 'HTTP/1.1 416 Requested Range Not Satisfiable');
@@ -193,6 +169,40 @@ final class FileReader {
                }
        }
        
+       /**
+        * Sends the headers of the file to the client.
+        */
+       protected function sendHeaders() {
+               foreach ($this->headers as $name => $value) {
+                       if (empty($name)) {
+                               @header($value);
+                       }
+                       else {
+                               @header($name.': '.$value);
+                       }
+               }
+       }
+       
+       /**
+        * Sends the actual file to the client.
+        */
+       protected function sendFile() {
+               if ($this->startByte > 0 || $this->endByte < $this->options['filesize'] - 1) {
+                       $file = new File($this->location, 'rb');
+                       if ($this->startByte > 0) $file->seek($this->startByte);
+                       while ($this->startByte <= $this->endByte) {
+                               $remainingBytes = $this->endByte - $this->startByte;
+                               $readBytes = ($remainingBytes > 1048576 ? 1048576 : $remainingBytes + 1);
+                               echo $file->read($readBytes);
+                               $this->startByte += $readBytes;
+                       }
+                       $file->close();
+               }
+               else {
+                       readfile($this->location);
+               }
+       }
+       
        /**
         * Sets the options for the http response.
         *