aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/files.php
diff options
context:
space:
mode:
authorVictor Dubiniuk <victor.dubiniuk@gmail.com>2015-09-23 19:00:15 +0300
committerThomas Müller <thomas.mueller@tmit.eu>2015-09-28 11:46:53 +0200
commite3b95e8473679c2f65abc84c07d121ea66059aee (patch)
tree49a7223d921eda7947c1df1e37f67469efa8419e /lib/private/files.php
parentfe32beb85d11e7a02e336f0f686f09fe4fe5bacd (diff)
downloadnextcloud-server-e3b95e8473679c2f65abc84c07d121ea66059aee.tar.gz
nextcloud-server-e3b95e8473679c2f65abc84c07d121ea66059aee.zip
Introduce streamer
Diffstat (limited to 'lib/private/files.php')
-rw-r--r--lib/private/files.php96
1 files changed, 33 insertions, 63 deletions
diff --git a/lib/private/files.php b/lib/private/files.php
index 658e708e5de..638d148adc4 100644
--- a/lib/private/files.php
+++ b/lib/private/files.php
@@ -57,20 +57,15 @@ class OC_Files {
/**
* @param string $filename
* @param string $name
- * @param bool $zip
*/
- private static function sendHeaders($filename, $name, $zip = false) {
+ private static function sendHeaders($filename, $name) {
OC_Response::setContentDispositionHeader($name, 'attachment');
header('Content-Transfer-Encoding: binary');
OC_Response::disableCaching();
- if ($zip) {
- header('Content-Type: application/zip');
- } else {
- $filesize = \OC\Files\Filesystem::filesize($filename);
- header('Content-Type: '.\OC_Helper::getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename)));
- if ($filesize > -1) {
- OC_Response::setContentLengthHeader($filesize);
- }
+ $filesize = \OC\Files\Filesystem::filesize($filename);
+ header('Content-Type: '.\OC_Helper::getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename)));
+ if ($filesize > -1) {
+ OC_Response::setContentLengthHeader($filesize);
}
}
@@ -79,9 +74,9 @@ class OC_Files {
*
* @param string $dir
* @param string $files ; separated list of files to download
- * @param boolean $only_header ; boolean to only send header of the request
+ * @param boolean $onlyHeader ; boolean to only send header of the request
*/
- public static function get($dir, $files, $only_header = false) {
+ public static function get($dir, $files, $onlyHeader = false) {
$view = \OC\Files\Filesystem::getView();
if (is_array($files) && count($files) === 1) {
@@ -89,45 +84,48 @@ class OC_Files {
}
if (is_array($files)) {
- $get_type = self::ZIP_FILES;
+ $getType = self::ZIP_FILES;
$basename = basename($dir);
if ($basename) {
- $name = $basename . '.zip';
+ $name = $basename;
} else {
- $name = 'download.zip';
+ $name = 'download';
}
$filename = $dir . '/' . $name;
} else {
$filename = $dir . '/' . $files;
if (\OC\Files\Filesystem::is_dir($dir . '/' . $files)) {
- $get_type = self::ZIP_DIR;
+ $getType = self::ZIP_DIR;
// downloading root ?
if ($files === '') {
- $name = 'download.zip';
+ $name = 'download';
} else {
- $name = $files . '.zip';
+ $name = $files;
}
} else {
- $get_type = self::FILE;
+ $getType = self::FILE;
$name = $files;
}
}
- if ($get_type === self::FILE) {
- $zip = false;
+ if ($getType === self::FILE) {
+ $streamer = false;
} else {
- $zip = new ZipStreamer\ZipStreamer();
+ $streamer = new OC_Streamer();
}
OC_Util::obEnd();
try {
- if ($get_type === self::FILE) {
+ if ($getType === self::FILE) {
$view->lockFile($filename, ILockingProvider::LOCK_SHARED);
}
- if ($zip or \OC\Files\Filesystem::isReadable($filename)) {
- self::sendHeaders($filename, $name, $zip);
+
+ 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');
@@ -137,33 +135,34 @@ class OC_Files {
header("HTTP/1.0 403 Forbidden");
die('403 Forbidden');
}
- if ($only_header) {
+ if ($onlyHeader) {
return;
}
- if ($zip) {
+ if ($streamer) {
$executionTime = intval(ini_get('max_execution_time'));
set_time_limit(0);
- if ($get_type === self::ZIP_FILES) {
+ 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');
- $zip->addFileFromStream($fh, basename($file));
+ $streamer->addFileFromStream($fh, basename($file), $fileSize);
fclose($fh);
} elseif (\OC\Files\Filesystem::is_dir($file)) {
- self::zipAddDir($file, $zip);
+ $streamer->addDirRecoursive($file);
}
}
- } elseif ($get_type === self::ZIP_DIR) {
+ } elseif ($getType === self::ZIP_DIR) {
$file = $dir . '/' . $files;
- self::zipAddDir($file, $zip);
+ $streamer->addDirRecoursive($file);
}
- $zip->finalize();
+ $streamer->finalize();
set_time_limit($executionTime);
} else {
\OC\Files\Filesystem::readfile($filename);
}
- if ($get_type === self::FILE) {
+ if ($getType === self::FILE) {
$view->unlockFile($filename, ILockingProvider::LOCK_SHARED);
}
} catch (\OCP\Lock\LockedException $ex) {
@@ -178,35 +177,6 @@ class OC_Files {
}
/**
- * @param string $dir
- * @param ZipStreamer $zip
- * @param string $internalDir
- */
- public static function zipAddDir($dir, ZipStreamer $zip, $internalDir='') {
- $dirname=basename($dir);
- $rootDir = $internalDir.$dirname;
- if (!empty($rootDir)) {
- $zip->addEmptyDir($rootDir);
- }
- $internalDir.=$dirname.='/';
- // prevent absolute dirs
- $internalDir = ltrim($internalDir, '/');
-
- $files=\OC\Files\Filesystem::getDirectoryContent($dir);
- foreach($files as $file) {
- $filename=$file['name'];
- $file=$dir.'/'.$filename;
- if(\OC\Files\Filesystem::is_file($file)) {
- $fh = \OC\Files\Filesystem::fopen($file, 'r');
- $zip->addFileFromStream($fh, $internalDir.$filename);
- fclose($fh);
- }elseif(\OC\Files\Filesystem::is_dir($file)) {
- self::zipAddDir($file, $zip, $internalDir);
- }
- }
- }
-
- /**
* set the maximum upload size limit for apache hosts using .htaccess
*
* @param int $size file size in bytes