MongoGridFS.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 ASCENDING = 1;
  18. const DESCENDING = -1;
  19. /**
  20. * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.chunks
  21. * @var $chunks MongoCollection
  22. */
  23. public $chunks;
  24. /**
  25. * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.filesname
  26. * @var $filesName string
  27. */
  28. protected $filesName;
  29. /**
  30. * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.chunksname
  31. * @var $chunksName string
  32. */
  33. protected $chunksName;
  34. /**
  35. * @var MongoDB
  36. */
  37. private $database;
  38. private $prefix;
  39. private $defaultChunkSize = 261120;
  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. * @throws \Exception
  50. */
  51. public function __construct(MongoDB $db, $prefix = "fs", $chunks = null)
  52. {
  53. if ($chunks) {
  54. trigger_error("The 'chunks' argument is deprecated and ignored", E_DEPRECATED);
  55. }
  56. if (empty($prefix)) {
  57. throw new \Exception('MongoGridFS::__construct(): invalid prefix');
  58. }
  59. $this->database = $db;
  60. $this->prefix = $prefix;
  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. * Delete a file from the database
  68. *
  69. * @link http://php.net/manual/en/mongogridfs.delete.php
  70. * @param mixed $id _id of the file to remove
  71. * @return boolean Returns true if the remove was successfully sent to the database.
  72. */
  73. public function delete($id)
  74. {
  75. $this->createChunksIndex();
  76. $this->chunks->remove(['files_id' => $id], ['justOne' => false]);
  77. return parent::remove(['_id' => $id]);
  78. }
  79. /**
  80. * Drops the files and chunks collections
  81. * @link http://php.net/manual/en/mongogridfs.drop.php
  82. * @return array The database response
  83. */
  84. public function drop()
  85. {
  86. $this->chunks->drop();
  87. return parent::drop();
  88. }
  89. /**
  90. * @link http://php.net/manual/en/mongogridfs.find.php
  91. * @param array $query The query
  92. * @param array $fields Fields to return
  93. * @param array $options Options for the find command
  94. * @return MongoGridFSCursor A MongoGridFSCursor
  95. */
  96. public function find(array $query = [], array $fields = [])
  97. {
  98. $cursor = new MongoGridFSCursor($this, $this->db->getConnection(), (string) $this, $query, $fields);
  99. $cursor->setReadPreference($this->getReadPreference());
  100. return $cursor;
  101. }
  102. /**
  103. * Returns a single file matching the criteria
  104. *
  105. * @link http://www.php.net/manual/en/mongogridfs.findone.php
  106. * @param array $query The fields for which to search.
  107. * @param array $fields Fields of the results to return.
  108. * @param array $options Options for the find command
  109. * @return MongoGridFSFile|null
  110. */
  111. public function findOne(array $query = [], array $fields = [], array $options = [])
  112. {
  113. if (is_string($query)) {
  114. $query = ['filename' => $query];
  115. }
  116. $items = iterator_to_array($this->find($query, $fields)->limit(1));
  117. return count($items) ? current($items) : null;
  118. }
  119. /**
  120. * Retrieve a file from the database
  121. *
  122. * @link http://www.php.net/manual/en/mongogridfs.get.php
  123. * @param mixed $id _id of the file to find.
  124. * @return MongoGridFSFile|null
  125. */
  126. public function get($id)
  127. {
  128. return $this->findOne(['_id' => $id]);
  129. }
  130. /**
  131. * Stores a file in the database
  132. *
  133. * @link http://php.net/manual/en/mongogridfs.put.php
  134. * @param string $filename The name of the file
  135. * @param array $extra Other metadata to add to the file saved
  136. * @param array $options An array of options for the insert operations executed against the chunks and files collections.
  137. * @return mixed Returns the _id of the saved object
  138. */
  139. public function put($filename, array $extra = [], array $options = [])
  140. {
  141. return $this->storeFile($filename, $extra, $options);
  142. }
  143. /**
  144. * Removes files from the collections
  145. *
  146. * @link http://www.php.net/manual/en/mongogridfs.remove.php
  147. * @param array $criteria Description of records to remove.
  148. * @param array $options Options for remove.
  149. * @throws MongoCursorException
  150. * @return boolean
  151. */
  152. public function remove(array $criteria = [], array $options = [])
  153. {
  154. $this->createChunksIndex();
  155. $matchingFiles = parent::find($criteria, ['_id' => 1]);
  156. $ids = [];
  157. foreach ($matchingFiles as $file) {
  158. $ids[] = $file['_id'];
  159. }
  160. $this->chunks->remove(['files_id' => ['$in' => $ids]], ['justOne' => false] + $options);
  161. return parent::remove(['_id' => ['$in' => $ids]], ['justOne' => false] + $options);
  162. }
  163. /**
  164. * Chunkifies and stores bytes in the database
  165. * @link http://php.net/manual/en/mongogridfs.storebytes.php
  166. * @param string $bytes A string of bytes to store
  167. * @param array $extra Other metadata to add to the file saved
  168. * @param array $options Options for the store. "safe": Check that this store succeeded
  169. * @return mixed The _id of the object saved
  170. */
  171. public function storeBytes($bytes, array $extra = [], array $options = [])
  172. {
  173. $this->createChunksIndex();
  174. $record = $extra + [
  175. 'length' => mb_strlen($bytes, '8bit'),
  176. 'md5' => md5($bytes),
  177. ];
  178. try {
  179. $file = $this->insertFile($record, $options);
  180. } catch (MongoException $e) {
  181. throw new MongoGridFSException('Could not store file: '. $e->getMessage(), 0, $e);
  182. }
  183. try {
  184. $this->insertChunksFromBytes($bytes, $file);
  185. } catch (MongoException $e) {
  186. $this->delete($file['_id']);
  187. throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), 0, $e);
  188. }
  189. return $file['_id'];
  190. }
  191. /**
  192. * Stores a file in the database
  193. *
  194. * @link http://php.net/manual/en/mongogridfs.storefile.php
  195. * @param string $filename The name of the file
  196. * @param array $extra Other metadata to add to the file saved
  197. * @param array $options Options for the store. "safe": Check that this store succeeded
  198. * @return mixed Returns the _id of the saved object
  199. * @throws MongoGridFSException
  200. * @throws Exception
  201. */
  202. public function storeFile($filename, array $extra = [], array $options = [])
  203. {
  204. $this->createChunksIndex();
  205. $record = $extra;
  206. if (is_string($filename)) {
  207. $record += [
  208. 'md5' => md5_file($filename),
  209. 'length' => filesize($filename),
  210. 'filename' => $filename,
  211. ];
  212. $handle = fopen($filename, 'r');
  213. if (! $handle) {
  214. throw new MongoGridFSException('could not open file: ' . $filename);
  215. }
  216. } elseif (! is_resource($filename)) {
  217. throw new \Exception('first argument must be a string or stream resource');
  218. } else {
  219. $handle = $filename;
  220. }
  221. $md5 = null;
  222. try {
  223. $file = $this->insertFile($record, $options);
  224. } catch (MongoException $e) {
  225. throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), 0, $e);
  226. }
  227. try {
  228. $length = $this->insertChunksFromFile($handle, $file, $md5);
  229. } catch (MongoException $e) {
  230. $this->delete($file['_id']);
  231. throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), 0, $e);
  232. }
  233. // Add length and MD5 if they were not present before
  234. $update = [];
  235. if (! isset($record['length'])) {
  236. $update['length'] = $length;
  237. }
  238. if (! isset($record['md5'])) {
  239. try {
  240. $update['md5'] = $md5;
  241. } catch (MongoException $e) {
  242. throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), 0, $e);
  243. }
  244. }
  245. if (count($update)) {
  246. try {
  247. $result = $this->update(['_id' => $file['_id']], ['$set' => $update]);
  248. if (! $this->isOKResult($result)) {
  249. throw new MongoGridFSException('Could not store file');
  250. }
  251. } catch (MongoException $e) {
  252. $this->delete($file['_id']);
  253. throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), 0, $e);
  254. }
  255. }
  256. return $file['_id'];
  257. }
  258. /**
  259. * Saves an uploaded file directly from a POST to the database
  260. *
  261. * @link http://www.php.net/manual/en/mongogridfs.storeupload.php
  262. * @param string $name The name attribute of the uploaded file, from <input type="file" name="something"/>.
  263. * @param array $metadata An array of extra fields for the uploaded file.
  264. * @return mixed Returns the _id of the uploaded file.
  265. * @throws MongoGridFSException
  266. */
  267. public function storeUpload($name, array $metadata = [])
  268. {
  269. if (! isset($_FILES[$name]) || $_FILES[$name]['error'] !== UPLOAD_ERR_OK) {
  270. throw new MongoGridFSException("Could not find uploaded file $name");
  271. }
  272. if (! isset($_FILES[$name]['tmp_name'])) {
  273. throw new MongoGridFSException("Couldn't find tmp_name in the \$_FILES array. Are you sure the upload worked?");
  274. }
  275. $uploadedFile = $_FILES[$name];
  276. $uploadedFile['tmp_name'] = (array) $uploadedFile['tmp_name'];
  277. $uploadedFile['name'] = (array) $uploadedFile['name'];
  278. if (count($uploadedFile['tmp_name']) > 1) {
  279. foreach ($uploadedFile['tmp_name'] as $key => $file) {
  280. $metadata['filename'] = $uploadedFile['name'][$key];
  281. $this->storeFile($file, $metadata);
  282. }
  283. return null;
  284. } else {
  285. $metadata += ['filename' => array_pop($uploadedFile['name'])];
  286. return $this->storeFile(array_pop($uploadedFile['tmp_name']), $metadata);
  287. }
  288. }
  289. /**
  290. * Creates the index on the chunks collection
  291. */
  292. private function createChunksIndex()
  293. {
  294. try {
  295. $this->chunks->createIndex(['files_id' => 1, 'n' => 1], ['unique' => true]);
  296. } catch (MongoDuplicateKeyException $e) {}
  297. }
  298. /**
  299. * Inserts a single chunk into the database
  300. *
  301. * @param mixed $fileId
  302. * @param string $data
  303. * @param int $chunkNumber
  304. * @return array|bool
  305. */
  306. private function insertChunk($fileId, $data, $chunkNumber)
  307. {
  308. $chunk = [
  309. 'files_id' => $fileId,
  310. 'n' => $chunkNumber,
  311. 'data' => new MongoBinData($data),
  312. ];
  313. $result = $this->chunks->insert($chunk);
  314. if (! $this->isOKResult($result)) {
  315. throw new \MongoException('error inserting chunk');
  316. }
  317. return $result;
  318. }
  319. /**
  320. * Splits a string into chunks and writes them to the database
  321. *
  322. * @param string $bytes
  323. * @param array $record
  324. */
  325. private function insertChunksFromBytes($bytes, $record)
  326. {
  327. $chunkSize = $record['chunkSize'];
  328. $fileId = $record['_id'];
  329. $i = 0;
  330. $chunks = str_split($bytes, $chunkSize);
  331. foreach ($chunks as $chunk) {
  332. $this->insertChunk($fileId, $chunk, $i++);
  333. }
  334. }
  335. /**
  336. * Reads chunks from a file and writes them to the database
  337. *
  338. * @param resource $handle
  339. * @param array $record
  340. * @param string $md5
  341. * @return int Returns the number of bytes written to the database
  342. */
  343. private function insertChunksFromFile($handle, $record, &$md5)
  344. {
  345. $written = 0;
  346. $offset = 0;
  347. $i = 0;
  348. $fileId = $record['_id'];
  349. $chunkSize = $record['chunkSize'];
  350. $hash = hash_init('md5');
  351. rewind($handle);
  352. while (! feof($handle)) {
  353. $data = stream_get_contents($handle, $chunkSize);
  354. hash_update($hash, $data);
  355. $this->insertChunk($fileId, $data, $i++);
  356. $written += strlen($data);
  357. $offset += $chunkSize;
  358. }
  359. $md5 = hash_final($hash);
  360. return $written;
  361. }
  362. /**
  363. * Writes a file record to the database
  364. *
  365. * @param $record
  366. * @param array $options
  367. * @return array
  368. */
  369. private function insertFile($record, array $options = [])
  370. {
  371. $record += [
  372. '_id' => new MongoId(),
  373. 'uploadDate' => new MongoDate(),
  374. 'chunkSize' => $this->defaultChunkSize,
  375. ];
  376. $result = $this->insert($record, $options);
  377. if (! $this->isOKResult($result)) {
  378. throw new \MongoException('error inserting file');
  379. }
  380. return $record;
  381. }
  382. private function isOKResult($result)
  383. {
  384. return (is_array($result) && $result['ok'] == 1.0) ||
  385. (is_bool($result) && $result);
  386. }
  387. /**
  388. * @return array
  389. */
  390. public function __sleep()
  391. {
  392. return ['chunks', 'chunksName', 'database', 'defaultChunkSize', 'filesName', 'prefix'] + parent::__sleep();
  393. }
  394. }