diff options
author | Piotr Filiciak <piotr@filiciak.pl> | 2016-05-20 18:16:44 +0200 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2016-05-20 18:16:44 +0200 |
commit | 9999e05660637c77947c71656d2f03d841e19ab9 (patch) | |
tree | 3eac375c092a2013e7d9fdb6be8d0f74d298defd /lib/private/legacy | |
parent | 59a85a4c76b80658d9373e3acf4f71b872b244a0 (diff) | |
download | nextcloud-server-9999e05660637c77947c71656d2f03d841e19ab9.tar.gz nextcloud-server-9999e05660637c77947c71656d2f03d841e19ab9.zip |
Http Range requests support in downloads
Http range requests support is required for video preview
Diffstat (limited to 'lib/private/legacy')
-rw-r--r-- | lib/private/legacy/files.php | 143 |
1 files changed, 131 insertions, 12 deletions
diff --git a/lib/private/legacy/files.php b/lib/private/legacy/files.php index 9b6a1a4465f..6327a0895e6 100644 --- a/lib/private/legacy/files.php +++ b/lib/private/legacy/files.php @@ -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; } } @@ -157,18 +186,77 @@ 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); + } } /** |