89 lines
2.3 KiB
PHP
Raw Normal View History

2013-07-19 18:52:33 +03:00
<?php
2013-08-08 00:22:21 +03:00
/**
2013-08-28 12:02:27 +02:00
* ownCloud - Documents App
2013-08-08 00:22:21 +03:00
*
* @author Victor Dubiniuk
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
2013-07-19 18:52:33 +03:00
2013-09-07 20:05:35 +03:00
namespace OCA\Documents;
2013-09-26 22:55:17 +03:00
/**
* Class processing range HTTP request (partial download)
*/
2013-09-07 20:05:35 +03:00
class Download_Range extends \OCA\Documents\Download {
2013-07-19 18:52:33 +03:00
// Start of the range
protected $start;
// End of the range
protected $end;
2013-08-16 18:54:31 +03:00
/**
* Build download model to serve HTTP_RANGE
* @param type $view - filesystem view
* @param type $filepath - path to the file relative to this view root
*/
public function __construct($owner, $filepath){
$this->view = $this->getView($owner);
2013-07-19 18:52:33 +03:00
$this->filepath = $filepath;
}
2013-08-16 18:54:31 +03:00
/**
* Send the requested parts of the file
*/
2013-07-19 18:52:33 +03:00
public function sendResponse(){
if (!preg_match('/^bytes=\d*-\d*(,\d*-\d*)*$/', $_SERVER['HTTP_RANGE'])){
$this->sendNotSatisfiable();
}
2014-01-04 00:24:59 +03:00
$mimetype = $this->getMimeType();
$content = $this->view->file_get_contents($this->filepath);
$data = Filter::read($content, $mimetype);
$size = strlen($data['content']);
2013-07-19 18:52:33 +03:00
$ranges = explode(',', substr($_SERVER['HTTP_RANGE'], 6));
foreach ($ranges as $range){
$parts = explode('-', $range);
if ($parts[0]==='' && $parts[1]=='') {$this->sendNotSatisfiable();}
if ($parts[0]==='') {
$start = $size - $parts[1];
$end = $size - 1;}
else {
$start = $parts[0];
$end = ($parts[1]==='') ? $size - 1 : $parts[1];}
2013-07-19 18:52:33 +03:00
if ($start > $end){
$this->sendNotSatisfiable();
}
2014-01-04 00:24:59 +03:00
$buffer = substr($data['content'], $start, $end - $start);
2013-07-19 18:52:33 +03:00
$md5Sum = md5($buffer);
2014-01-04 00:24:59 +03:00
2013-07-19 18:52:33 +03:00
// send the headers and data
header("Content-Length: " . ($end - $start));
2013-07-19 18:52:33 +03:00
header("Content-md5: " . $md5Sum);
header("Accept-Ranges: bytes");
2014-01-04 00:24:59 +03:00
header('Content-Range: bytes ' . $start . '-' . ($end) . '/' . $size);
2013-07-19 18:52:33 +03:00
header("Connection: close");
2014-01-04 00:24:59 +03:00
header("Content-type: " . $data['mimetype']);
2013-07-19 18:52:33 +03:00
header('Content-Disposition: attachment; filename=' . $this->getFilename());
2013-07-19 19:53:39 +03:00
\OC_Util::obEnd();
2013-07-19 18:52:33 +03:00
echo $buffer;
flush();
}
}
2013-08-16 18:54:31 +03:00
/**
* Send 416 if we can't satisfy the requested ranges
*/
2013-07-19 18:52:33 +03:00
protected function sendNotSatisfiable(){
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header('Content-Range: bytes */' . $this->getFilesize()); // Required in 416.
exit;
}
}