Jelajahi Sumber

Use memory stream in call to getResource()

This prevents us from keeping around temporary files and lets PHP manage the tempfile.
Andreas Braun 10 tahun lalu
induk
melakukan
1a8e2ef9ee
1 mengubah file dengan 18 tambahan dan 19 penghapusan
  1. 18 19
      lib/Mongo/MongoGridFSFile.php

+ 18 - 19
lib/Mongo/MongoGridFSFile.php

@@ -29,6 +29,7 @@ class MongoGridFSFile
 
 
     /**
     /**
      * @link http://php.net/manual/en/mongogridfsfile.construct.php
      * @link http://php.net/manual/en/mongogridfsfile.construct.php
+     *
      * @param MongoGridFS $gridfs The parent MongoGridFS instance
      * @param MongoGridFS $gridfs The parent MongoGridFS instance
      * @param array $file A file from the database
      * @param array $file A file from the database
      * @return MongoGridFSFile Returns a new MongoGridFSFile
      * @return MongoGridFSFile Returns a new MongoGridFSFile
@@ -46,10 +47,7 @@ class MongoGridFSFile
      */
      */
     public function getFilename()
     public function getFilename()
     {
     {
-        if (isset($this->file['filename'])) {
-            return $this->file['filename'];
-        }
-        return null;
+        return isset($this->file['filename']) ? $this->file['filename'] : null;
     }
     }
 
 
     /**
     /**
@@ -77,9 +75,14 @@ class MongoGridFSFile
             $filename = 'file';
             $filename = 'file';
         }
         }
 
 
-        $handle = fopen($filename, 'w');
-        $written = $this->writeFromRessource($handle);
+        if (! $handle = fopen($filename, 'w')) {
+            trigger_error(E_ERROR, 'Can not open the destination file');
+            return 0;
+        }
+
+        $written = $this->copyToResource($handle);
         fclose($handle);
         fclose($handle);
+
         return $written;
         return $written;
     }
     }
 
 
@@ -91,10 +94,10 @@ class MongoGridFSFile
     public function getBytes()
     public function getBytes()
     {
     {
         $result = '';
         $result = '';
-        $chunks = $this->getChunks();
-        foreach ($chunks as $chunk) {
+        foreach ($this->getChunks() as $chunk) {
             $result .= $chunk['data']->bin;
             $result .= $chunk['data']->bin;
         }
         }
+
         return $result;
         return $result;
     }
     }
 
 
@@ -108,33 +111,29 @@ class MongoGridFSFile
      */
      */
     public function getResource()
     public function getResource()
     {
     {
-        $handle = tmpfile();
-        $this->writeFromRessource($handle);
+        $handle = fopen('php://temp', 'w+');
+        $this->copyToResource($handle);
         rewind($handle);
         rewind($handle);
+
         return $handle;
         return $handle;
     }
     }
 
 
-    private function writeFromRessource($handle)
+    private function copyToResource($handle)
     {
     {
-
-        if (! $handle) {
-            trigger_error(E_ERROR, 'can not open the destination file');
-        }
         $written = 0;
         $written = 0;
-        $chunks = $this->getChunks();
-        foreach ($chunks as $chunk) {
+        foreach ($this->getChunks() as $chunk) {
             $written += fwrite($handle, $chunk['data']->bin);
             $written += fwrite($handle, $chunk['data']->bin);
         }
         }
+
         return $written;
         return $written;
     }
     }
 
 
     private function getChunks()
     private function getChunks()
     {
     {
         return $chunks = $this->gridfs->chunks->find(
         return $chunks = $this->gridfs->chunks->find(
-            ['files_id' => new \MongoDB\BSON\ObjectID((string) $this->file['_id'])],
+            ['files_id' => $this->file['_id']],
             ['data' => 1],
             ['data' => 1],
             ['n' => 1]
             ['n' => 1]
         );
         );
     }
     }
-
 }
 }