MongoGridFS.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 MongoGridFS extends MongoCollection
  16. {
  17. const DEFAULT_CHUNK_SIZE = 262144; // 256 kb
  18. const ASCENDING = 1;
  19. const DESCENDING = -1;
  20. /**
  21. * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.chunks
  22. * @var $chunks MongoCollection
  23. */
  24. public $chunks;
  25. /**
  26. * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.filesname
  27. * @var $filesName string
  28. */
  29. protected $filesName;
  30. /**
  31. * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.chunksname
  32. * @var $chunksName string
  33. */
  34. protected $chunksName;
  35. /**
  36. * @var MongoDB
  37. */
  38. protected $database;
  39. protected $ensureIndexes = false;
  40. /**
  41. * Files as stored across two collections, the first containing file meta
  42. * information, the second containing chunks of the actual file. By default,
  43. * fs.files and fs.chunks are the collection names used.
  44. *
  45. * @link http://php.net/manual/en/mongogridfs.construct.php
  46. * @param MongoDB $db Database
  47. * @param string $prefix [optional] <p>Optional collection name prefix.</p>
  48. * @param mixed $chunks [optional]
  49. * @return MongoGridFS
  50. * @throws \Exception
  51. */
  52. public function __construct(MongoDB $db, $prefix = "fs", $chunks = null)
  53. {
  54. if ($chunks) {
  55. trigger_error("The 'chunks' argument is deprecated and ignored", E_DEPRECATED);
  56. }
  57. if (empty($prefix)) {
  58. throw new \Exception('MongoGridFS::__construct(): invalid prefix');
  59. }
  60. $this->database = $db;
  61. $this->filesName = $prefix . '.files';
  62. $this->chunksName = $prefix . '.chunks';
  63. $this->chunks = $db->selectCollection($this->chunksName);
  64. parent::__construct($db, $this->filesName);
  65. }
  66. /**
  67. * Drops the files and chunks collections
  68. * @link http://php.net/manual/en/mongogridfs.drop.php
  69. * @return array The database response
  70. */
  71. public function drop()
  72. {
  73. $this->chunks->drop();
  74. parent::drop();
  75. }
  76. /**
  77. * @link http://php.net/manual/en/mongogridfs.find.php
  78. * @param array $query The query
  79. * @param array $fields Fields to return
  80. * @return MongoGridFSCursor A MongoGridFSCursor
  81. */
  82. public function find(array $query = array(), array $fields = array())
  83. {
  84. $cursor = new MongoGridFSCursor($this, $this->db->getConnection(), (string)$this, $query, $fields);
  85. $cursor->setReadPreference($this->getReadPreference());
  86. return $cursor;
  87. }
  88. /**
  89. * Stores a file in the database
  90. * @link http://php.net/manual/en/mongogridfs.storefile.php
  91. * @param string $filename The name of the file
  92. * @param array $extra Other metadata to add to the file saved
  93. * @param array $options Options for the store. "safe": Check that this store succeeded
  94. * @return mixed Returns the _id of the saved object
  95. */
  96. public function storeFile($filename, array $extra = array(), array $options = array())
  97. {
  98. if (is_string($filename)) {
  99. $md5 = md5_file($filename);
  100. $shortName = basename($filename);
  101. $filename = fopen($filename, 'r');
  102. }
  103. if (! is_resource($filename)) {
  104. throw new \InvalidArgumentException();
  105. }
  106. $length = fstat($filename)['size'];
  107. $extra['chunkSize'] = isset($extra['chunkSize']) ? $extra['chunkSize']: self::DEFAULT_CHUNK_SIZE;
  108. $extra['_id'] = isset($extra['_id']) ?: new MongoId();
  109. $extra['length'] = $length;
  110. $extra['md5'] = isset($md5) ? $md5 : $this->calculateMD5($filename);
  111. $extra['filename'] = isset($extra['filename']) ? $extra['filename'] : $shortName;
  112. $fileDocument = $this->insertFile($extra);
  113. $this->insertChunksFromFile($filename, $fileDocument);
  114. return $fileDocument['_id'];
  115. }
  116. /**
  117. * Chunkifies and stores bytes in the database
  118. * @link http://php.net/manual/en/mongogridfs.storebytes.php
  119. * @param string $bytes A string of bytes to store
  120. * @param array $extra Other metadata to add to the file saved
  121. * @param array $options Options for the store. "safe": Check that this store succeeded
  122. * @return mixed The _id of the object saved
  123. */
  124. public function storeBytes($bytes, array $extra = array(), array $options = array())
  125. {
  126. $length = mb_strlen($bytes, '8bit');
  127. $extra['chunkSize'] = isset($extra['chunkSize']) ? $extra['chunkSize'] : self::DEFAULT_CHUNK_SIZE;
  128. $extra['_id'] = isset($extra['_id']) ?: new MongoId();
  129. $extra['length'] = $length;
  130. $extra['md5'] = md5($bytes);
  131. $file = $this->insertFile($extra);
  132. $this->insertChunksFromBytes($bytes, $file);
  133. return $file['_id'];
  134. }
  135. /**
  136. * Returns a single file matching the criteria
  137. * @link http://www.php.net/manual/en/mongogridfs.findone.php
  138. * @param array $query The fields for which to search.
  139. * @param array $fields Fields of the results to return.
  140. * @return MongoGridFSFile|null
  141. */
  142. public function findOne(array $query = [], array $fields = [], array $options = [])
  143. {
  144. $file = parent::findOne($query, $fields);
  145. if (! $file) {
  146. return;
  147. }
  148. return new MongoGridFSFile($this, $file);
  149. }
  150. /**
  151. * Removes files from the collections
  152. * @link http://www.php.net/manual/en/mongogridfs.remove.php
  153. * @param array $criteria Description of records to remove.
  154. * @param array $options Options for remove. Valid options are: "safe"- Check that the remove succeeded.
  155. * @throws MongoCursorException
  156. * @return boolean
  157. */
  158. public function remove(array $criteria = [], array $options = [])
  159. {
  160. $matchingFiles = parent::find($criteria, ['_id' => 1]);
  161. $ids = [];
  162. foreach ($matchingFiles as $file) {
  163. $ids[] = $file['_id'];
  164. }
  165. $this->chunks->remove(['files_id' => ['$in' => $ids]], ['justOne' => false]);
  166. return parent::remove($criteria, ['justOne' => false] + $options);
  167. }
  168. /**
  169. * Delete a file from the database
  170. * @link http://php.net/manual/en/mongogridfs.delete.php
  171. * @param mixed $id _id of the file to remove
  172. * @return boolean Returns true if the remove was successfully sent to the database.
  173. */
  174. public function delete($id)
  175. {
  176. if (is_string($id)) {
  177. $id = new MongoId($id);
  178. }
  179. if (! $id instanceof MongoId) {
  180. return false;
  181. }
  182. $this->chunks->remove(['files_id' => $id], ['justOne' => false]);
  183. return parent::remove(['_id' => $id]);
  184. }
  185. /**
  186. * Saves an uploaded file directly from a POST to the database
  187. * @link http://www.php.net/manual/en/mongogridfs.storeupload.php
  188. * @param string $name The name attribute of the uploaded file, from <input type="file" name="something"/>.
  189. * @param array $metadata An array of extra fields for the uploaded file.
  190. * @return mixed Returns the _id of the uploaded file.
  191. */
  192. public function storeUpload($name, array $metadata = array())
  193. {
  194. if (! isset($_FILES[$name]) || $_FILES[$name]['error'] !== UPLOAD_ERR_OK) {
  195. throw new \InvalidArgumentException();
  196. }
  197. $metadata += ['filename' => $_FILES[$name]['name']];
  198. return $this->storeFile($_FILES[$name]['tmp_name'], $metadata);
  199. }
  200. /**
  201. * Retrieve a file from the database
  202. * @link http://www.php.net/manual/en/mongogridfs.get.php
  203. * @param mixed $id _id of the file to find.
  204. * @return MongoGridFSFile|null Returns the file, if found, or NULL.
  205. */
  206. public function __get($id)
  207. {
  208. if (is_string($id)) {
  209. $id = new MongoId($id);
  210. }
  211. if (! $id instanceof MongoId) {
  212. return false;
  213. }
  214. return $this->findOne(['_id' => $id]);
  215. }
  216. /**
  217. * Stores a file in the database
  218. * @link http://php.net/manual/en/mongogridfs.put.php
  219. * @param string $filename The name of the file
  220. * @param array $extra Other metadata to add to the file saved
  221. * @return mixed Returns the _id of the saved object
  222. */
  223. public function put($filename, array $extra = array())
  224. {
  225. return $this->storeFile($filename, $extra);
  226. }
  227. private function ensureIndexes()
  228. {
  229. if ($this->ensureIndexes) {
  230. return;
  231. }
  232. $this->ensureFilesIndex();
  233. $this->ensureChunksIndex();
  234. $this->ensuredIndexes = true;
  235. }
  236. private function ensureChunksIndex()
  237. {
  238. foreach ($this->chunks->getIndexInfo() as $index) {
  239. if (isset($index['unique']) && $index['unique'] && $index['key'] === ['files_id' => 1, 'n' => 1]) {
  240. return;
  241. }
  242. }
  243. $this->chunks->createIndex(['files_id' => 1, 'n' => 1], ['unique' => true]);
  244. }
  245. private function ensureFilesIndex()
  246. {
  247. foreach ($this->getIndexInfo() as $index) {
  248. if ($index['key'] === ['filename' => 1, 'uploadDate' => 1]) {
  249. return;
  250. }
  251. }
  252. $this->createIndex(['filename' => 1, 'uploadDate' => 1]);
  253. }
  254. private function insertChunksFromFile($file, $fileInfo)
  255. {
  256. $length = $fileInfo['length'];
  257. $chunkSize = $fileInfo['chunkSize'];
  258. $fileId = $fileInfo['_id'];
  259. $offset = 0;
  260. $i = 0;
  261. rewind($file);
  262. while ($offset < $length) {
  263. $data = stream_get_contents($file, $chunkSize);
  264. $this->insertChunk($fileId, $data, $i++);
  265. $offset += $chunkSize;
  266. }
  267. }
  268. private function calculateMD5($file)
  269. {
  270. // XXX: this could be really a bad idea with big files...
  271. rewind($file);
  272. $data = stream_get_contents($file);
  273. return md5($data);
  274. }
  275. private function insertChunksFromBytes($bytes, $fileInfo)
  276. {
  277. $length = $fileInfo['length'];
  278. $chunkSize = $fileInfo['chunkSize'];
  279. $fileId = $fileInfo['_id'];
  280. $i = 0;
  281. $chunks = str_split($bytes, $chunkSize);
  282. foreach ($chunks as $chunk) {
  283. $this->insertChunk($fileId, $chunk, $i++);
  284. }
  285. }
  286. private function insertChunk($id, $data, $chunkNumber)
  287. {
  288. $chunk = [
  289. 'files_id' => $id,
  290. 'n' => $chunkNumber,
  291. 'data' => new MongoBinData($data),
  292. ];
  293. return $this->chunks->insert($chunk);
  294. }
  295. private function insertFile($metadata)
  296. {
  297. $this->ensureIndexes();
  298. $metadata['uploadDate'] = new MongoDate();
  299. $this->insert($metadata);
  300. return $metadata;
  301. }
  302. }