* @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,
* 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.
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']) {
/**
* 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');
}
}
+ /**
+ * 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.
*