Http Range requests support in downloads

Http range requests support is required for video preview
This commit is contained in:
Piotr Filiciak 2016-05-20 18:16:44 +02:00 committed by Vincent Petry
parent 59a85a4c76
commit 9999e05660
No known key found for this signature in database
GPG Key ID: AF8F9EFC56562186
5 changed files with 220 additions and 15 deletions

View File

@ -50,4 +50,13 @@ if(isset($_GET['downloadStartSecret'])
setcookie('ocDownloadStarted', $_GET['downloadStartSecret'], time() + 20, '/');
}
OC_Files::get($dir, $files_list, $_SERVER['REQUEST_METHOD'] == 'HEAD');
$server_params = array( 'head' => \OC::$server->getRequest()->getMethod() == 'HEAD' );
/**
* Http range requests support
*/
if (isset($_SERVER['HTTP_RANGE'])) {
$server_params['range'] = \OC::$server->getRequest()->getHeader('Range');
}
OC_Files::get($dir, $files_list, $server_params);

View File

@ -484,16 +484,25 @@ class ShareController extends Controller {
$this->emitAccessShareHook($share);
$server_params = array( 'head' => $this->request->getMethod() == 'HEAD' );
/**
* Http range requests support
*/
if (isset($_SERVER['HTTP_RANGE'])) {
$server_params['range'] = $this->request->getHeader('Range');
}
// download selected files
if (!is_null($files) && $files !== '') {
// FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well
// after dispatching the request which results in a "Cannot modify header information" notice.
OC_Files::get($originalSharePath, $files_list, $_SERVER['REQUEST_METHOD'] == 'HEAD');
OC_Files::get($originalSharePath, $files_list, $server_params);
exit();
} else {
// FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well
// after dispatching the request which results in a "Cannot modify header information" notice.
OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $_SERVER['REQUEST_METHOD'] == 'HEAD');
OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $server_params);
exit();
}
}

View File

@ -53,6 +53,7 @@ use OCP\Files\InvalidCharacterInPathException;
use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException;
use OCP\Files\ReservedWordException;
use OCP\Files\UnseekableException;
use OCP\Files\Storage\ILockingStorage;
use OCP\IUser;
use OCP\Lock\ILockingProvider;
@ -423,6 +424,39 @@ class View {
return false;
}
/**
* @param string $path
* @param int $from
* @param int $to
* @return bool|mixed
* @throws \OCP\Files\InvalidPathException, \OCP\Files\UnseekableException
*/
public function readfilePart($path, $from, $to) {
$this->assertPathLength($path);
@ob_end_clean();
$handle = $this->fopen($path, 'rb');
if ($handle) {
if (fseek($handle, $from) === 0) {
$chunkSize = 8192; // 8 kB chunks
$end = $to + 1;
while (!feof($handle) && ftell($handle) < $end) {
$len = $end-ftell($handle);
if ($len > $chunkSize) {
$len = $chunkSize;
}
echo fread($handle, $len);
flush();
}
$size = ftell($handle) - $from;
return $size;
}
else {
throw new \OCP\Files\UnseekableException('fseek error');
}
}
return false;
}
/**
* @param string $path
* @return mixed

View File

@ -16,6 +16,7 @@
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
* @author Vincent Petry <pvince81@owncloud.com>
* @author Piotr Filiciak <piotr@filiciak.pl>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
@ -49,20 +50,48 @@ class OC_Files {
const UPLOAD_MIN_LIMIT_BYTES = 1048576; // 1 MiB
private static $MULTIPART_BOUNDARY = '';
/**
* @return string
*/
private static function getBoundary() {
if (empty(self::$MULTIPART_BOUNDARY)) {
self::$MULTIPART_BOUNDARY = md5(mt_rand());
}
return self::$MULTIPART_BOUNDARY;
}
/**
* @param string $filename
* @param string $name
* @param array $rangeArray ('from'=>int,'to'=>int), ...
*/
private static function sendHeaders($filename, $name) {
private static function sendHeaders($filename, $name, array $rangeArray) {
OC_Response::setContentDispositionHeader($name, 'attachment');
header('Content-Transfer-Encoding: binary');
header('Content-Transfer-Encoding: binary', true);
OC_Response::disableCaching();
$fileSize = \OC\Files\Filesystem::filesize($filename);
$type = \OC::$server->getMimeTypeDetector()->getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename));
header('Content-Type: '.$type);
if ($fileSize > -1) {
OC_Response::setContentLengthHeader($fileSize);
if (!empty($rangeArray)) {
header('HTTP/1.1 206 Partial Content', true);
header('Accept-Ranges: bytes', true);
if (count($rangeArray) > 1) {
$type = 'multipart/byteranges; boundary='.self::getBoundary();
// no Content-Length header here
}
else {
header(sprintf('Content-Range: bytes %d-%d/%d', $rangeArray[0]['from'], $rangeArray[0]['to'], $fileSize), true);
OC_Response::setContentLengthHeader($rangeArray[0]['to'] - $rangeArray[0]['from'] + 1);
}
}
else {
OC_Response::setContentLengthHeader($fileSize);
}
}
header('Content-Type: '.$type, true);
}
/**
@ -70,9 +99,9 @@ class OC_Files {
*
* @param string $dir
* @param string $files ; separated list of files to download
* @param boolean $onlyHeader ; boolean to only send header of the request
* @param array $params ; 'head' boolean to only send header of the request ; 'range' http range header
*/
public static function get($dir, $files, $onlyHeader = false) {
public static function get($dir, $files, $params = array( 'head' => false )) {
$view = \OC\Files\Filesystem::getView();
$getType = self::FILE;
@ -86,7 +115,7 @@ class OC_Files {
if (!is_array($files)) {
$filename = $dir . '/' . $files;
if (!$view->is_dir($filename)) {
self::getSingleFile($view, $dir, $files, $onlyHeader);
self::getSingleFile($view, $dir, $files, $params);
return;
}
}
@ -156,19 +185,78 @@ class OC_Files {
}
}
/**
* @param string $rangeHeaderPos
* @param int $fileSize
* @return array $rangeArray ('from'=>int,'to'=>int), ...
*/
private static function parseHttpRangeHeader($rangeHeaderPos, $fileSize) {
$rArray=split(',', $rangeHeaderPos);
$minOffset = 0;
$ind = 0;
$rangeArray = array();
foreach ($rArray as $value) {
$ranges = explode('-', $value);
if (is_numeric($ranges[0])) {
if ($ranges[0] < $minOffset) { // case: bytes=500-700,601-999
$ranges[0] = $minOffset;
}
if ($ind > 0 && $rangeArray[$ind-1]['to']+1 == $ranges[0]) { // case: bytes=500-600,601-999
$ind--;
$ranges[0] = $rangeArray[$ind]['from'];
}
}
if (is_numeric($ranges[0]) && is_numeric($ranges[1]) && $ranges[0] < $fileSize && $ranges[0] <= $ranges[1]) {
// case: x-x
if ($ranges[1] >= $fileSize) {
$ranges[1] = $fileSize-1;
}
$rangeArray[$ind++] = array( 'from' => $ranges[0], 'to' => $ranges[1], 'size' => $fileSize );
$minOffset = $ranges[1] + 1;
if ($minOffset >= $fileSize) {
break;
}
}
elseif (is_numeric($ranges[0]) && $ranges[0] < $fileSize) {
// case: x-
$rangeArray[$ind++] = array( 'from' => $ranges[0], 'to' => $fileSize-1, 'size' => $fileSize );
break;
}
elseif (is_numeric($ranges[1])) {
// case: -x
if ($ranges[1] > $fileSize) {
$ranges[1] = $fileSize;
}
$rangeArray[$ind++] = array( 'from' => $fileSize-$ranges[1], 'to' => $fileSize-1, 'size' => $fileSize );
break;
}
}
return $rangeArray;
}
/**
* @param View $view
* @param string $name
* @param string $dir
* @param boolean $onlyHeader
* @param array $params ; 'head' boolean to only send header of the request ; 'range' http range header
*/
private static function getSingleFile($view, $dir, $name, $onlyHeader) {
private static function getSingleFile($view, $dir, $name, $params) {
$filename = $dir . '/' . $name;
OC_Util::obEnd();
$view->lockFile($filename, ILockingProvider::LOCK_SHARED);
$rangeArray = array();
if (isset($params['range']) && substr($params['range'], 0, 6) === 'bytes=') {
$rangeArray = self::parseHttpRangeHeader(substr($params['range'], 6),
\OC\Files\Filesystem::filesize($filename));
}
if (\OC\Files\Filesystem::isReadable($filename)) {
self::sendHeaders($filename, $name);
self::sendHeaders($filename, $name, $rangeArray);
} elseif (!\OC\Files\Filesystem::file_exists($filename)) {
header("HTTP/1.0 404 Not Found");
$tmpl = new OC_Template('', '404', 'guest');
@ -178,10 +266,41 @@ class OC_Files {
header("HTTP/1.0 403 Forbidden");
die('403 Forbidden');
}
if ($onlyHeader) {
if (isset($params['head']) && $params['head']) {
return;
}
$view->readfile($filename);
if (!empty($rangeArray)) {
try {
if (count($rangeArray) == 1) {
$view->readfilePart($filename, $rangeArray[0]['from'], $rangeArray[0]['to']);
}
else {
// check if file is seekable (if not throw UnseekableException)
// we have to check it before body contents
$view->readfilePart($filename, $rangeArray[0]['size'], $rangeArray[0]['size']);
$type = \OC::$server->getMimeTypeDetector()->getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename));
foreach ($rangeArray as $range) {
echo "\r\n--".self::getBoundary()."\r\n".
"Content-type: ".$type."\r\n".
"Content-range: bytes ".$range['from']."-".$range['to']."/".$range['size']."\r\n\r\n";
$view->readfilePart($filename, $range['from'], $range['to']);
}
echo "\r\n--".self::getBoundary()."--\r\n";
}
} catch (\OCP\Files\UnseekableException $ex) {
// file is unseekable
header_remove('Accept-Ranges');
header_remove('Content-Range');
header("HTTP/1.1 200 OK");
self::sendHeaders($filename, $name, array());
$view->readfile($filename);
}
}
else {
$view->readfile($filename);
}
}
/**

View File

@ -0,0 +1,34 @@
<?php
/**
* @author Piotr Filiciak <piotr@filiciak.pl>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
/**
* Public interface of ownCloud for apps to use.
* Files/UnseekableException class
*/
// use OCP namespace for all classes that are considered public.
// This means that they should be used by apps instead of the internal ownCloud classes
namespace OCP\Files;
/**
* Exception for seek problem
*/
class UnseekableException extends \Exception {}