From c8c3b8a63ea1c4763e1a5197487f0ae3506339c1 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 26 Feb 2012 03:54:21 +0100 Subject: [PATCH] chunked implementation for readfile prevents memory issues when downloading large files --- apps/files_sharing/sharedstorage.php | 8 -------- lib/filestorage.php | 1 - lib/filestorage/local.php | 3 --- lib/filestoragecommon.php | 8 -------- lib/filesystemview.php | 9 ++++++++- 5 files changed, 8 insertions(+), 21 deletions(-) diff --git a/apps/files_sharing/sharedstorage.php b/apps/files_sharing/sharedstorage.php index cb641e68a8..0e110da30b 100644 --- a/apps/files_sharing/sharedstorage.php +++ b/apps/files_sharing/sharedstorage.php @@ -281,14 +281,6 @@ class OC_Filestorage_Shared extends OC_Filestorage { } } - public function readfile($path) { - $source = $this->getSource($path); - if ($source) { - $storage = OC_Filesystem::getStorage($source); - return $storage->readfile($this->getInternalPath($source)); - } - } - public function filectime($path) { if ($path == "" || $path == "/") { $ctime = 0; diff --git a/lib/filestorage.php b/lib/filestorage.php index 4523144f6f..989f5b311f 100644 --- a/lib/filestorage.php +++ b/lib/filestorage.php @@ -36,7 +36,6 @@ class OC_Filestorage{ public function is_readable($path){} public function is_writable($path){} public function file_exists($path){} - public function readfile($path){} public function filectime($path){} public function filemtime($path){} public function file_get_contents($path){} diff --git a/lib/filestorage/local.php b/lib/filestorage/local.php index dcb516a3af..243fdd3ba6 100644 --- a/lib/filestorage/local.php +++ b/lib/filestorage/local.php @@ -52,9 +52,6 @@ class OC_Filestorage_Local extends OC_Filestorage{ public function file_exists($path){ return file_exists($this->datadir.$path); } - public function readfile($path){ - return readfile($this->datadir.$path); - } public function filectime($path){ return filectime($this->datadir.$path); } diff --git a/lib/filestoragecommon.php b/lib/filestoragecommon.php index f522d15c4e..45ad11fcde 100644 --- a/lib/filestoragecommon.php +++ b/lib/filestoragecommon.php @@ -37,14 +37,6 @@ class OC_Filestorage_Common extends OC_Filestorage { public function is_readable($path){} public function is_writable($path){} public function file_exists($path){} - public function readfile($path) { - $handle = $this->fopen($path, "r"); - $chunk = 1024; - while (!feof($handle)) { - echo fread($handle, $chunk); - } - return $this->filesize($path); - } public function filectime($path) { $stat = $this->stat($path); return $stat['ctime']; diff --git a/lib/filesystemview.php b/lib/filesystemview.php index 91c6cd1772..ab254cc512 100644 --- a/lib/filesystemview.php +++ b/lib/filesystemview.php @@ -136,7 +136,14 @@ class OC_FilesystemView { return $this->basicOperation('filesize',$path); } public function readfile($path){ - return $this->basicOperation('readfile',$path,array('read')); + $handle=$this->fopen($path,'r'); + $chunkSize = 1024*1024;// 1 MB chunks + while (!feof($handle)) { + echo fread($handle, $chunkSize); + @ob_flush(); + flush(); + } + return $this->filesize($path); } public function is_readable($path){ return $this->basicOperation('is_readable',$path);