Range download support
parent
8e127b5ab1
commit
57fae2637d
@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OCA\Office;
|
||||||
|
|
||||||
|
class Download {
|
||||||
|
|
||||||
|
// File to be served
|
||||||
|
protected $filepath;
|
||||||
|
|
||||||
|
protected $instance;
|
||||||
|
|
||||||
|
public function __construct($filepath){
|
||||||
|
$this->filepath = $filepath;
|
||||||
|
if (isset($_SERVER['HTTP_RANGE'])) {
|
||||||
|
$this->instance = new Download\Range($filepath);
|
||||||
|
} else {
|
||||||
|
$this->instance = new Download\Simple($filepath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function sendResponse(){
|
||||||
|
\OCP\Response::disableCaching();
|
||||||
|
|
||||||
|
if (!$this->fileExists()){
|
||||||
|
$this->sendNotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->instance->sendResponse();
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getFilename(){
|
||||||
|
return basename($this->filepath);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getFilesize(){
|
||||||
|
return \OC\Files\Filesystem::filesize($this->filepath);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getMimeType(){
|
||||||
|
return \OC\Files\Filesystem::getMimeType($this->filepath);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function fileExists(){
|
||||||
|
return \OC\Files\Filesystem::file_exists($this->filepath);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function sendNotFound(){
|
||||||
|
header("HTTP/1.0 404 Not Found");
|
||||||
|
$tmpl = new OCP\Template('', '404', 'guest');
|
||||||
|
$tmpl->assign('file', $this->filepath);
|
||||||
|
$tmpl->printPage();
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OCA\Office\Download;
|
||||||
|
|
||||||
|
class Range extends \OCA\Office\Download {
|
||||||
|
|
||||||
|
// Start of the range
|
||||||
|
protected $start;
|
||||||
|
// End of the range
|
||||||
|
protected $end;
|
||||||
|
|
||||||
|
public function __construct($filepath){
|
||||||
|
$this->filepath = $filepath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function sendResponse(){
|
||||||
|
if (!preg_match('/^bytes=\d*-\d*(,\d*-\d*)*$/', $_SERVER['HTTP_RANGE'])){
|
||||||
|
$this->sendNotSatisfiable();
|
||||||
|
}
|
||||||
|
$ranges = explode(',', substr($_SERVER['HTTP_RANGE'], 6));
|
||||||
|
foreach ($ranges as $range){
|
||||||
|
$parts = explode('-', $range);
|
||||||
|
if (isset($parts[0])){
|
||||||
|
$start = $parts[0];
|
||||||
|
} else {
|
||||||
|
$start = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($parts[1])){
|
||||||
|
$end = $parts[1];
|
||||||
|
} else {
|
||||||
|
$end = $this->getFilesize() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($start > $end){
|
||||||
|
$this->sendNotSatisfiable();
|
||||||
|
}
|
||||||
|
|
||||||
|
$handle = \OC\Files\Filesystem::fopen($this->filepath, 'r');
|
||||||
|
\fseek($handle, $start);
|
||||||
|
$buffer = \fread($handle, $end - $start);
|
||||||
|
$md5Sum = md5($buffer);
|
||||||
|
\fclose($handle);
|
||||||
|
// send the headers and data
|
||||||
|
header("Content-Length: " . $end - $start);
|
||||||
|
header("Content-md5: " . $md5Sum);
|
||||||
|
header("Accept-Ranges: bytes");
|
||||||
|
header('Content-Range: bytes ' . $start . '-' . ($end) . '/' . $this->getFilesize());
|
||||||
|
header("Connection: close");
|
||||||
|
header("Content-type: " . $this->getMimeType());
|
||||||
|
header('Content-Disposition: attachment; filename=' . $this->getFilename());
|
||||||
|
echo $buffer;
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function sendNotSatisfiable(){
|
||||||
|
header('HTTP/1.1 416 Requested Range Not Satisfiable');
|
||||||
|
header('Content-Range: bytes */' . $this->getFilesize()); // Required in 416.
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OCA\Office\Download;
|
||||||
|
|
||||||
|
class Simple extends \OCA\Office\Download {
|
||||||
|
|
||||||
|
public function __construct($filepath){
|
||||||
|
$this->filepath = $filepath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function sendResponse(){
|
||||||
|
header( 'Content-Type:' . $this->getMimeType() );
|
||||||
|
|
||||||
|
$encodedName = rawurlencode($this->getFilename());
|
||||||
|
if (preg_match("/MSIE/", $_SERVER["HTTP_USER_AGENT"])){
|
||||||
|
header(
|
||||||
|
'Content-Disposition: attachment; filepath="' . $encodedName . '"'
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
header('Content-Disposition: attachment; filepath*=UTF-8\'\'' . $encodedName
|
||||||
|
. '; filepath="' . $encodedName . '"');
|
||||||
|
}
|
||||||
|
|
||||||
|
header('Content-Length: ' . \OC\Files\Filesystem::filesize($this->filepath));
|
||||||
|
|
||||||
|
\OC_Util::obEnd();
|
||||||
|
\OC\Files\Filesystem::readfile($this->filepath);
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue