summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2012-11-18 04:35:46 -0800
committerThomas Müller <thomas.mueller@tmit.eu>2012-11-18 04:35:46 -0800
commitea80a8b9e3adb4fb091d0d5c80280de683b7b1a3 (patch)
tree287993984ee7c0a54860a68a13f03cc3428dc8a6
parente28d71bf5511a8e6b687814a54e5448d2827cbbb (diff)
parentde7e419610d3fde8a16367776279d76837a0ee62 (diff)
downloadnextcloud-server-ea80a8b9e3adb4fb091d0d5c80280de683b7b1a3.tar.gz
nextcloud-server-ea80a8b9e3adb4fb091d0d5c80280de683b7b1a3.zip
Merge pull request #114 from riso/xsendfile
Implement X-Sendfile support
-rw-r--r--cron.php3
-rw-r--r--lib/files.php37
-rw-r--r--lib/helper.php31
3 files changed, 67 insertions, 4 deletions
diff --git a/cron.php b/cron.php
index cd2e155a494..a202ca60bad 100644
--- a/cron.php
+++ b/cron.php
@@ -56,6 +56,9 @@ if( !OC_Config::getValue( 'installed', false )) {
// Handle unexpected errors
register_shutdown_function('handleUnexpectedShutdown');
+// Delete temp folder
+OC_Helper::cleanTmpNoClean();
+
// Exit if background jobs are disabled!
$appmode = OC_BackgroundJob::getExecutionType();
if( $appmode == 'none' ) {
diff --git a/lib/files.php b/lib/files.php
index e5bf78d032f..912de5655b0 100644
--- a/lib/files.php
+++ b/lib/files.php
@@ -140,6 +140,11 @@ class OC_Files {
* @param boolean $only_header ; boolean to only send header of the request
*/
public static function get($dir, $files, $only_header = false) {
+ $xsendfile = false;
+ if (isset($_SERVER['MOD_X_SENDFILE_ENABLED']) ||
+ isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) {
+ $xsendfile = true;
+ }
if(strpos($files, ';')) {
$files=explode(';', $files);
}
@@ -149,7 +154,11 @@ class OC_Files {
$executionTime = intval(ini_get('max_execution_time'));
set_time_limit(0);
$zip = new ZipArchive();
- $filename = OC_Helper::tmpFile('.zip');
+ if ($xsendfile) {
+ $filename = OC_Helper::tmpFileNoClean('.zip');
+ }else{
+ $filename = OC_Helper::tmpFile('.zip');
+ }
if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)!==true) {
exit("cannot open <$filename>\n");
}
@@ -170,7 +179,11 @@ class OC_Files {
$executionTime = intval(ini_get('max_execution_time'));
set_time_limit(0);
$zip = new ZipArchive();
- $filename = OC_Helper::tmpFile('.zip');
+ if ($xsendfile) {
+ $filename = OC_Helper::tmpFileNoClean('.zip');
+ }else{
+ $filename = OC_Helper::tmpFile('.zip');
+ }
if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)!==true) {
exit("cannot open <$filename>\n");
}
@@ -191,8 +204,13 @@ class OC_Files {
ini_set('zlib.output_compression', 'off');
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($filename));
+ self::addSendfileHeader($filename);
}else{
header('Content-Type: '.OC_Filesystem::getMimeType($filename));
+ $storage = OC_Filesystem::getStorage($filename);
+ if ($storage instanceof OC_Filestorage_Local) {
+ self::addSendfileHeader(OC_Filesystem::getLocalFile($filename));
+ }
}
}elseif($zip or !OC_Filesystem::file_exists($filename)) {
header("HTTP/1.0 404 Not Found");
@@ -217,7 +235,9 @@ class OC_Files {
flush();
}
}
- unlink($filename);
+ if (!$xsendfile) {
+ unlink($filename);
+ }
}else{
OC_Filesystem::readfile($filename);
}
@@ -228,11 +248,20 @@ class OC_Files {
}
}
+ private static function addSendfileHeader($filename) {
+ if (isset($_SERVER['MOD_X_SENDFILE_ENABLED'])) {
+ header("X-Sendfile: " . $filename);
+ }
+ if (isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) {
+ header("X-Accel-Redirect: " . $filename);
+ }
+ }
+
public static function zipAddDir($dir, $zip, $internalDir='') {
$dirname=basename($dir);
$zip->addEmptyDir($internalDir.$dirname);
$internalDir.=$dirname.='/';
- $files=OC_Files::getdirectorycontent($dir);
+ $files=OC_Files::getDirectoryContent($dir);
foreach($files as $file) {
$filename=$file['name'];
$file=$dir.'/'.$filename;
diff --git a/lib/helper.php b/lib/helper.php
index ccceb58cd4c..339a12dc1ed 100644
--- a/lib/helper.php
+++ b/lib/helper.php
@@ -525,6 +525,27 @@ class OC_Helper {
}
/**
+ * create a temporary file with an unique filename. It will not be deleted
+ * automatically
+ * @param string $postfix
+ * @return string
+ *
+ */
+ public static function tmpFileNoClean($postfix='') {
+ $tmpDirNoClean=get_temp_dir().'/oc-noclean/';
+ if (!file_exists($tmpDirNoClean) || !is_dir($tmpDirNoClean)) {
+ if (file_exists($tmpDirNoClean)) {
+ unlink($tmpDirNoClean);
+ }
+ mkdir($tmpDirNoClean);
+ }
+ $file=$tmpDirNoClean.md5(time().rand()).$postfix;
+ $fh=fopen($file,'w');
+ fclose($fh);
+ return $file;
+ }
+
+ /**
* create a temporary folder with an unique filename
* @return string
*
@@ -560,6 +581,16 @@ class OC_Helper {
}
/**
+ * remove all files created by self::tmpFileNoClean
+ */
+ public static function cleanTmpNoClean() {
+ $tmpDirNoCleanFile=get_temp_dir().'/oc-noclean/';
+ if(file_exists($tmpDirNoCleanFile)) {
+ self::rmdirr($tmpDirNoCleanFile);
+ }
+ }
+
+ /**
* Adds a suffix to the name in case the file exists
*
* @param $path