better download handling #8
authorCameron Ball <c.ball1729@gmail.com>
Sun, 22 Feb 2015 17:02:34 +0000 (01:02 +0800)
committerCameron Ball <c.ball1729@gmail.com>
Sun, 22 Feb 2015 17:06:16 +0000 (01:06 +0800)
Controllers/FileController.php
Services/Http/HttpResponse.php

index 744a5fc..feb9ce1 100644 (file)
@@ -118,13 +118,11 @@ class FileController implements IDivineController
         $this->_downloadRepository->save($download);
         
         $zip = $this->_configManager->getDirective('filesPath') . '/' . $file->getPath() . '/' . $file->getHash() . '.zip';
-        //TODO: This may not work on all browser or something. We'll have to see. Also it may hog ram so...
         $this->_response->setHeader('Content-Type', $file->getMimetype())
                         ->setHeader('Content-Length', $file->getSize())
                         ->setHeader('Content-Disposition', 'filename="' . $file->getFileName() . '";')
-                        ->setHeader('Content-Transfer-Encoding', 'binary')
-                        ->setBody(file_get_contents($zip))
-                        ->sendResponse();
+                        ->download($zip);                                
+        exit();
     }
         
     private function notFound()
index e115d61..e9e5166 100644 (file)
@@ -96,10 +96,16 @@ class HttpResponse implements IHttpResponse
     
     public function download($path)
     {
-        $fp = fopen($path, "rb");
-        $this->sendHeaders();
-        @ob_clean();
-        rewind($fp);
-        fpassthru($fp);
+        $this->sendResponse();
+        $fd = fopen($path, 'r');
+        if(!$fd) throw new Exception ('Failed to open file.');
+        
+        while(!feof($fd)) {
+            $buffer = fread($fd, 2048);
+            print $buffer;
+        }
+        fclose ($fd);
+        
+        exit();
     }
 }