diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2015-10-05 17:47:15 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2015-10-05 17:47:15 +0200 |
commit | be46cd6737f7b4414d4583d0096d90b73929ce3d (patch) | |
tree | 6c8cc1e4e57030523a99c25be4d7afc73f320769 | |
parent | 57f841da2c414209ad9d4ae5a1cd0ac5ba604b7b (diff) | |
download | nextcloud-server-be46cd6737f7b4414d4583d0096d90b73929ce3d.tar.gz nextcloud-server-be46cd6737f7b4414d4583d0096d90b73929ce3d.zip |
Refactor single file download into it's own method
-rw-r--r-- | lib/private/files.php | 147 |
1 files changed, 71 insertions, 76 deletions
diff --git a/lib/private/files.php b/lib/private/files.php index 099602d56b0..e49e0ffdbc2 100644 --- a/lib/private/files.php +++ b/lib/private/files.php @@ -79,97 +79,65 @@ class OC_Files { * @param boolean $onlyHeader ; boolean to only send header of the request */ public static function get($dir, $files, $onlyHeader = false) { - $view = \OC\Files\Filesystem::getView(); - if (is_array($files) && count($files) === 1) { - $files = $files[0]; - } + $view = \OC\Files\Filesystem::getView(); + $getType = self::FILE; + $filename = $dir; + try { - if (is_array($files)) { - $getType = self::ZIP_FILES; - $basename = basename($dir); - if ($basename) { - $name = $basename; - } else { - $name = 'download'; + if (is_array($files) && count($files) === 1) { + $files = $files[0]; + $filename = $dir . '/' . $files; + if (!$view->is_dir($files)) { + self::getSingleFile($view, $dir, $files, $onlyHeader); + return; + } } - $filename = $dir . '/' . $name; - } else { - $filename = $dir . '/' . $files; - if (\OC\Files\Filesystem::is_dir($dir . '/' . $files)) { + $name = 'download'; + if (is_array($files)) { + $getType = self::ZIP_FILES; + $basename = basename($dir); + if ($basename) { + $name = $basename; + } + + $filename = $dir . '/' . $name; + } else { + $filename = $dir . '/' . $files; $getType = self::ZIP_DIR; // downloading root ? - if ($files === '') { - $name = 'download'; - } else { + if ($files !== '') { $name = $files; } - - } else { - $getType = self::FILE; - $name = $files; } - } - if ($getType === self::FILE) { - $streamer = false; - } else { $streamer = new Streamer(); - } - OC_Util::obEnd(); + OC_Util::obEnd(); - try { - if ($getType === self::FILE) { - $view->lockFile($filename, ILockingProvider::LOCK_SHARED); - } - if ($getType === self::ZIP_FILES) { - self::lockFiles($view, $dir, $files); - } - if ($getType === self::ZIP_DIR) { - self::lockFiles($view, $dir, $files); - } + self::lockFiles($view, $dir, $files); - if ($streamer) { - $streamer->sendHeaders($name); - } elseif (\OC\Files\Filesystem::isReadable($filename)) { - self::sendHeaders($filename, $name); - } elseif (!\OC\Files\Filesystem::file_exists($filename)) { - header("HTTP/1.0 404 Not Found"); - $tmpl = new OC_Template('', '404', 'guest'); - $tmpl->printPage(); - exit(); - } else { - header("HTTP/1.0 403 Forbidden"); - die('403 Forbidden'); - } - if ($onlyHeader) { - return; - } - if ($streamer) { - $executionTime = intval(ini_get('max_execution_time')); - set_time_limit(0); - if ($getType === self::ZIP_FILES) { - foreach ($files as $file) { - $file = $dir . '/' . $file; - if (\OC\Files\Filesystem::is_file($file)) { - $fileSize = \OC\Files\Filesystem::filesize($file); - $fh = \OC\Files\Filesystem::fopen($file, 'r'); - $streamer->addFileFromStream($fh, basename($file), $fileSize); - fclose($fh); - } elseif (\OC\Files\Filesystem::is_dir($file)) { - $streamer->addDirRecursive($file); - } + $streamer->sendHeaders($name); + $executionTime = intval(ini_get('max_execution_time')); + set_time_limit(0); + if ($getType === self::ZIP_FILES) { + foreach ($files as $file) { + $file = $dir . '/' . $file; + if (\OC\Files\Filesystem::is_file($file)) { + $fileSize = \OC\Files\Filesystem::filesize($file); + $fh = \OC\Files\Filesystem::fopen($file, 'r'); + $streamer->addFileFromStream($fh, basename($file), $fileSize); + fclose($fh); + } elseif (\OC\Files\Filesystem::is_dir($file)) { + $streamer->addDirRecursive($file); } - } elseif ($getType === self::ZIP_DIR) { - $file = $dir . '/' . $files; - $streamer->addDirRecursive($file); } - $streamer->finalize(); - set_time_limit($executionTime); - } else { - \OC\Files\Filesystem::readfile($filename); + } elseif ($getType === self::ZIP_DIR) { + $file = $dir . '/' . $files; + $streamer->addDirRecursive($file); } + $streamer->finalize(); + set_time_limit($executionTime); self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); } catch (\OCP\Lock\LockedException $ex) { self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); @@ -188,6 +156,32 @@ class OC_Files { /** * @param View $view + * @param string $name + */ + private static function getSingleFile($view, $dir, $name, $onlyHeader) { + $filename = $dir . '/' . $name; + OC_Util::obEnd(); + $view->lockFile($filename, ILockingProvider::LOCK_SHARED); + + if (\OC\Files\Filesystem::isReadable($filename)) { + self::sendHeaders($filename, $name); + } elseif (!\OC\Files\Filesystem::file_exists($filename)) { + header("HTTP/1.0 404 Not Found"); + $tmpl = new OC_Template('', '404', 'guest'); + $tmpl->printPage(); + exit(); + } else { + header("HTTP/1.0 403 Forbidden"); + die('403 Forbidden'); + } + if ($onlyHeader) { + return; + } + $view->readfile($filename); + } + + /** + * @param View $view * @param $dir * @param string[]|string $files */ @@ -296,7 +290,7 @@ class OC_Files { * @param $dir * @param $files * @param $getType - * @param $view + * @param View $view * @param $filename */ private static function unlockAllTheFiles($dir, $files, $getType, $view, $filename) { @@ -314,4 +308,5 @@ class OC_Files { $view->unlockFile($file, ILockingProvider::LOCK_SHARED); } } + } |