MongoGridFSFile.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. */
  15. class MongoGridFSFile
  16. {
  17. /**
  18. * @link http://php.net/manual/en/class.mongogridfsfile.php#mongogridfsfile.props.file
  19. * @var array
  20. */
  21. public $file;
  22. /**
  23. * @link http://php.net/manual/en/class.mongogridfsfile.php#mongogridfsfile.props.gridfs
  24. * @var $gridfs
  25. */
  26. protected $gridfs;
  27. /**
  28. * @link http://php.net/manual/en/mongogridfsfile.construct.php
  29. * @param MongoGridFS $gridfs The parent MongoGridFS instance
  30. * @param array $file A file from the database
  31. * @return MongoGridFSFile Returns a new MongoGridFSFile
  32. */
  33. public function __construct(MongoGridFS $gridfs, array $file)
  34. {
  35. $this->gridfs = $gridfs;
  36. $this->file = $file;
  37. }
  38. /**
  39. * Returns this file's filename
  40. * @link http://php.net/manual/en/mongogridfsfile.getfilename.php
  41. * @return string Returns the filename
  42. */
  43. public function getFilename()
  44. {
  45. if (isset($this->file['filename'])) {
  46. return $this->file['filename'];
  47. }
  48. return null;
  49. }
  50. /**
  51. * Returns this file's size
  52. * @link http://php.net/manual/en/mongogridfsfile.getsize.php
  53. * @return int Returns this file's size
  54. */
  55. public function getSize()
  56. {
  57. return $this->file['length'];
  58. }
  59. /**
  60. * Writes this file to the filesystem
  61. * @link http://php.net/manual/en/mongogridfsfile.write.php
  62. * @param string $filename The location to which to write the file (path+filename+extension). If none is given, the stored filename will be used.
  63. * @return int Returns the number of bytes written
  64. */
  65. public function write($filename = null)
  66. {
  67. if ($filename === null) {
  68. $filename = $this->getFilename();
  69. }
  70. if (empty($filename)) {
  71. $filename = 'file';
  72. }
  73. $handle = fopen($filename, 'w');
  74. $written = $this->writeFromRessource($handle);
  75. fclose($handle);
  76. return $written;
  77. }
  78. /**
  79. * This will load the file into memory. If the file is bigger than your memory, this will cause problems!
  80. * @link http://php.net/manual/en/mongogridfsfile.getbytes.php
  81. * @return string Returns a string of the bytes in the file
  82. */
  83. public function getBytes()
  84. {
  85. $result = '';
  86. $chunks = $this->getChunks();
  87. foreach ($chunks as $chunk) {
  88. $result .= $chunk['data']->bin;
  89. }
  90. return $result;
  91. }
  92. /**
  93. * This method returns a stream resource that can be used to read the stored file with all file functions in PHP.
  94. * The contents of the file are pulled out of MongoDB on the fly, so that the whole file does not have to be loaded into memory first.
  95. * At most two GridFSFile chunks will be loaded in memory.
  96. *
  97. * @link http://php.net/manual/en/mongogridfsfile.getresource.php
  98. * @return resource Returns a resource that can be used to read the file with
  99. */
  100. public function getResource()
  101. {
  102. $handle = tmpfile();
  103. $this->writeFromRessource($handle);
  104. fseek($handle, 0);
  105. return $handle;
  106. }
  107. private function writeFromRessource($handle)
  108. {
  109. if (! $handle) {
  110. trigger_error(E_ERROR, 'can not open the destination file');
  111. }
  112. $written = 0;
  113. $chunks = $this->getChunks();
  114. foreach ($chunks as $chunk) {
  115. $written += fwrite($handle, $chunk['data']->bin);
  116. }
  117. return $written;
  118. }
  119. private function getChunks()
  120. {
  121. return $chunks = $this->gridfs->chunks->find(
  122. ['files_id' => new \MongoDB\BSON\ObjectID((string) $this->file['_id'])],
  123. ['data' => 1],
  124. ['n' => 1]
  125. );
  126. }
  127. }