Refactor single file download into it's own method
This commit is contained in:
parent
57f841da2c
commit
be46cd6737
|
@ -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();
|
||||
$getType = self::FILE;
|
||||
$filename = $dir;
|
||||
try {
|
||||
|
||||
if (is_array($files) && count($files) === 1) {
|
||||
$files = $files[0];
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
self::lockFiles($view, $dir, $files);
|
||||
|
||||
$streamer->sendHeaders($name);
|
||||
$executionTime = intval(ini_get('max_execution_time'));
|
||||
set_time_limit(0);
|
||||
if ($getType === self::ZIP_FILES) {
|
||||
self::lockFiles($view, $dir, $files);
|
||||
}
|
||||
if ($getType === self::ZIP_DIR) {
|
||||
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);
|
||||
}
|
||||
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);
|
||||
|
@ -186,6 +154,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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue