From 9999e05660637c77947c71656d2f03d841e19ab9 Mon Sep 17 00:00:00 2001 From: Piotr Filiciak Date: Fri, 20 May 2016 18:16:44 +0200 Subject: Http Range requests support in downloads Http range requests support is required for video preview --- lib/private/Files/View.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'lib/private/Files') diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index 22e53a00706..136b2c2ac34 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; @@ -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 -- cgit v1.2.3 From 6577bbe887840889e16634b9bf1c4ce247ec265e Mon Sep 17 00:00:00 2001 From: Piotr Filiciak Date: Mon, 23 May 2016 15:17:00 +0200 Subject: Code style and doc fix --- lib/private/Files/View.php | 8 ++++---- lib/private/legacy/files.php | 12 ++++++------ lib/public/Files/UnseekableException.php | 1 + 3 files changed, 11 insertions(+), 10 deletions(-) (limited to 'lib/private/Files') diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index 136b2c2ac34..27613903086 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -429,7 +429,8 @@ class View { * @param int $from * @param int $to * @return bool|mixed - * @throws \OCP\Files\InvalidPathException, \OCP\Files\UnseekableException + * @throws \OCP\Files\InvalidPathException + * @throws \OCP\Files\UnseekableException */ public function readfilePart($path, $from, $to) { $this->assertPathLength($path); @@ -450,9 +451,8 @@ class View { $size = ftell($handle) - $from; return $size; } - else { - throw new \OCP\Files\UnseekableException('fseek error'); - } + + throw new \OCP\Files\UnseekableException('fseek error'); } return false; } diff --git a/lib/private/legacy/files.php b/lib/private/legacy/files.php index 6327a0895e6..ad5ba6d94ae 100644 --- a/lib/private/legacy/files.php +++ b/lib/private/legacy/files.php @@ -51,16 +51,16 @@ class OC_Files { const UPLOAD_MIN_LIMIT_BYTES = 1048576; // 1 MiB - private static $MULTIPART_BOUNDARY = ''; + private static $multipartBoundary = ''; /** * @return string */ private static function getBoundary() { - if (empty(self::$MULTIPART_BOUNDARY)) { - self::$MULTIPART_BOUNDARY = md5(mt_rand()); + if (empty(self::$multipartBoundary)) { + self::$multipartBoundary = md5(mt_rand()); } - return self::$MULTIPART_BOUNDARY; + return self::$multipartBoundary; } /** @@ -101,7 +101,7 @@ class OC_Files { * @param string $files ; separated list of files to download * @param array $params ; 'head' boolean to only send header of the request ; 'range' http range header */ - public static function get($dir, $files, $params = array( 'head' => false )) { + public static function get($dir, $files, $params = null) { $view = \OC\Files\Filesystem::getView(); $getType = self::FILE; @@ -115,7 +115,7 @@ class OC_Files { if (!is_array($files)) { $filename = $dir . '/' . $files; if (!$view->is_dir($filename)) { - self::getSingleFile($view, $dir, $files, $params); + self::getSingleFile($view, $dir, $files, is_null($params) ? array() : $params); return; } } diff --git a/lib/public/Files/UnseekableException.php b/lib/public/Files/UnseekableException.php index fda93697690..b59f844ce2f 100644 --- a/lib/public/Files/UnseekableException.php +++ b/lib/public/Files/UnseekableException.php @@ -30,5 +30,6 @@ namespace OCP\Files; /** * Exception for seek problem + * @since 9.1.0 */ class UnseekableException extends \Exception {} -- cgit v1.2.3