Ejemplo n.º 1
0
 /**
  * Apply a file range to a file and set the end offset.
  *
  * If an invalid range is requested a 416 Status code will be used
  * in the response.
  *
  * @param File $file The file to set a range on.
  * @param string $httpRange The range to use.
  * @return void
  */
 protected function _fileRange($file, $httpRange)
 {
     list(, $range) = explode('=', $httpRange);
     list($start, $end) = explode('-', $range);
     $fileSize = $file->size();
     $lastByte = $fileSize - 1;
     if ($start === '') {
         $start = $fileSize - $end;
         $end = $lastByte;
     }
     if ($end === '') {
         $end = $lastByte;
     }
     if ($start > $end || $end > $lastByte || $start > $lastByte) {
         $this->statusCode(416);
         $this->header(array('Content-Range' => 'bytes 0-' . $lastByte . '/' . $fileSize));
         return;
     }
     $this->header(array('Content-Length' => $end - $start + 1, 'Content-Range' => 'bytes ' . $start . '-' . $end . '/' . $fileSize));
     $this->statusCode(206);
     $this->_fileRange = array($start, $end);
 }
Ejemplo n.º 2
0
 /**
  * testAppend method
  *
  * @return void
  */
 public function testAppend()
 {
     if (!($tmpFile = $this->_getTmpFile())) {
         return false;
     }
     if (file_exists($tmpFile)) {
         unlink($tmpFile);
     }
     $TmpFile = new File($tmpFile);
     $this->assertFalse(file_exists($tmpFile));
     $fragments = array('CakePHP\'s', ' test suite', ' was here ...');
     $data = null;
     $size = 0;
     foreach ($fragments as $fragment) {
         $r = $TmpFile->append($fragment);
         $this->assertTrue($r);
         $this->assertTrue(file_exists($tmpFile));
         $data = $data . $fragment;
         $this->assertEquals($data, file_get_contents($tmpFile));
         $newSize = $TmpFile->size();
         $this->assertTrue($newSize > $size);
         $size = $newSize;
         $TmpFile->close();
     }
     $TmpFile->append('');
     $this->assertEquals($data, file_get_contents($tmpFile));
     $TmpFile->close();
 }