diff options
author | Vincent Petry <pvince81@owncloud.com> | 2016-05-23 21:01:26 +0200 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2016-05-23 21:01:26 +0200 |
commit | adcf942901fd567d97dbe105e8f3dfb7cea738e3 (patch) | |
tree | 3f3721d872941383689ba8ff661960d521af69c4 /lib | |
parent | 5a8af2f0be87cada2827ee3b86d2900146a62f77 (diff) | |
parent | 6577bbe887840889e16634b9bf1c4ce247ec265e (diff) | |
download | nextcloud-server-adcf942901fd567d97dbe105e8f3dfb7cea738e3.tar.gz nextcloud-server-adcf942901fd567d97dbe105e8f3dfb7cea738e3.zip |
Merge pull request #24750 from owncloud/lenz1111-share_download_range_requests_support
Http Range requests support in downloads
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Files/View.php | 34 | ||||
-rw-r--r-- | lib/private/legacy/files.php | 143 | ||||
-rw-r--r-- | lib/public/Files/UnseekableException.php | 35 |
3 files changed, 200 insertions, 12 deletions
diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index 22e53a00706..27613903086 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -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; @@ -425,6 +426,39 @@ class View { /** * @param string $path + * @param int $from + * @param int $to + * @return bool|mixed + * @throws \OCP\Files\InvalidPathException + * @throws \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; + } + + throw new \OCP\Files\UnseekableException('fseek error'); + } + return false; + } + + /** + * @param string $path * @return mixed */ public function isCreatable($path) { diff --git a/lib/private/legacy/files.php b/lib/private/legacy/files.php index 9b6a1a4465f..ad5ba6d94ae 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 $multipartBoundary = ''; + + /** + * @return string + */ + private static function getBoundary() { + if (empty(self::$multipartBoundary)) { + self::$multipartBoundary = md5(mt_rand()); + } + return self::$multipartBoundary; + } + /** * @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 = null) { $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, is_null($params) ? array() : $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); + } } /** diff --git a/lib/public/Files/UnseekableException.php b/lib/public/Files/UnseekableException.php new file mode 100644 index 00000000000..b59f844ce2f --- /dev/null +++ b/lib/public/Files/UnseekableException.php @@ -0,0 +1,35 @@ +<?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 + * @since 9.1.0 + */ +class UnseekableException extends \Exception {} |