22
33namespace Flow \Mongo ;
44
5+ use Exception ;
56use Flow \File ;
67use Flow \Request ;
78use Flow \RequestInterface ;
9+ use MongoDB \BSON \Binary ;
10+ use MongoDB \BSON \ObjectId ;
11+ use MongoDB \BSON \UTCDateTime ;
12+ use MongoDB \Operation \FindOneAndReplace ;
813
914
1015/**
@@ -42,9 +47,9 @@ protected function getGridFsFile()
4247 if (!$ this ->uploadGridFsFile ) {
4348 $ gridFsFileQuery = $ this ->getGridFsFileQuery ();
4449 $ changed = $ gridFsFileQuery ;
45- $ changed ['flowUpdated ' ] = new \ MongoDate ();
46- $ this ->uploadGridFsFile = $ this ->config ->getGridFs ()->findAndModify ( $ gridFsFileQuery , $ changed, null ,
47- ['upsert ' => true , 'new ' => true ]);
50+ $ changed ['flowUpdated ' ] = new UTCDateTime ();
51+ $ this ->uploadGridFsFile = $ this ->config ->getGridFs ()->getFilesCollection ()-> findOneAndReplace ( $ gridFsFileQuery , $ changed ,
52+ ['upsert ' => true , 'returnDocument ' => FindOneAndReplace:: RETURN_DOCUMENT_AFTER ]);
4853 }
4954
5055 return $ this ->uploadGridFsFile ;
@@ -56,10 +61,10 @@ protected function getGridFsFile()
5661 */
5762 public function chunkExists ($ index )
5863 {
59- return $ this ->config ->getGridFs ()->chunks -> find ([
64+ return $ this ->config ->getGridFs ()->getChunksCollection ()-> findOne ([
6065 'files_id ' => $ this ->getGridFsFile ()['_id ' ],
6166 'n ' => (intval ($ index ) - 1 )
62- ])-> limit ( 1 )-> hasNext () ;
67+ ]) !== null ;
6368 }
6469
6570 public function checkChunk ()
@@ -71,7 +76,7 @@ public function checkChunk()
7176 * Save chunk
7277 * @param $additionalUpdateOptions array additional options for the mongo update/upsert operation.
7378 * @return bool
74- * @throws \ Exception if upload size is invalid or some other unexpected error occurred.
79+ * @throws Exception if upload size is invalid or some other unexpected error occurred.
7580 */
7681 public function saveChunk ($ additionalUpdateOptions = [])
7782 {
@@ -89,19 +94,19 @@ public function saveChunk($additionalUpdateOptions = [])
8994 ($ actualChunkSize < $ this ->request ->getDefaultChunkSize () &&
9095 $ this ->request ->getCurrentChunkNumber () != $ this ->request ->getTotalChunks ())
9196 ) {
92- throw new \ Exception ("Invalid upload! (size: { $ actualChunkSize} ) " );
97+ throw new Exception ("Invalid upload! (size: $ actualChunkSize) " );
9398 }
94- $ chunk ['data ' ] = new \ MongoBinData ($ data , 0 ); // \MongoBinData::GENERIC is not defined for older mongo drivers
95- $ this ->config ->getGridFs ()->chunks -> update ($ chunkQuery , $ chunk , array_merge (['upsert ' => true ], $ additionalUpdateOptions ));
99+ $ chunk ['data ' ] = new Binary ($ data , Binary:: TYPE_GENERIC );
100+ $ this ->config ->getGridFs ()->getChunksCollection ()-> replaceOne ($ chunkQuery , $ chunk , array_merge (['upsert ' => true ], $ additionalUpdateOptions ));
96101 unlink ($ file ['tmp_name ' ]);
97102
98103 $ this ->ensureIndices ();
99104
100105 return true ;
101- } catch (\ Exception $ e ) {
106+ } catch (Exception $ e ) {
102107 // try to remove a possibly (partly) stored chunk:
103108 if (isset ($ chunkQuery )) {
104- $ this ->config ->getGridFs ()->chunks -> remove ($ chunkQuery );
109+ $ this ->config ->getGridFs ()->getChunksCollection ()-> deleteMany ($ chunkQuery );
105110 }
106111 throw $ e ;
107112 }
@@ -113,25 +118,24 @@ public function saveChunk($additionalUpdateOptions = [])
113118 public function validateFile ()
114119 {
115120 $ totalChunks = intval ($ this ->request ->getTotalChunks ());
116- $ storedChunks = $ this ->config ->getGridFs ()->chunks
117- ->find (['files_id ' => $ this ->getGridFsFile ()['_id ' ]])
118- ->count ();
121+ $ storedChunks = $ this ->config ->getGridFs ()->getChunksCollection ()
122+ ->countDocuments (['files_id ' => $ this ->getGridFsFile ()['_id ' ]]);
119123 return $ totalChunks === $ storedChunks ;
120124 }
121125
122126
123127 /**
124128 * Merge all chunks to single file
125129 * @param $metadata array additional metadata for final file
126- * @return \MongoId |bool of saved file or false if file was already saved
127- * @throws \ Exception
130+ * @return ObjectId |bool of saved file or false if file was already saved
131+ * @throws Exception
128132 */
129133 public function saveToGridFs ($ metadata = null )
130134 {
131135 $ file = $ this ->getGridFsFile ();
132136 $ file ['flowStatus ' ] = 'finished ' ;
133137 $ file ['metadata ' ] = $ metadata ;
134- $ result = $ this ->config ->getGridFs ()->findAndModify ($ this ->getGridFsFileQuery (), $ file );
138+ $ result = $ this ->config ->getGridFs ()->getFilesCollection ()-> findOneAndReplace ($ this ->getGridFsFileQuery (), $ file );
135139 // on second invocation no more file can be found, as the flowStatus changed:
136140 if (is_null ($ result )) {
137141 return false ;
@@ -142,7 +146,7 @@ public function saveToGridFs($metadata = null)
142146
143147 public function save ($ destination )
144148 {
145- throw new \ Exception ("Must not use 'save' on MongoFile - use 'saveToGridFs'! " );
149+ throw new Exception ("Must not use 'save' on MongoFile - use 'saveToGridFs'! " );
146150 }
147151
148152 public function deleteChunks ()
@@ -152,14 +156,10 @@ public function deleteChunks()
152156
153157 public function ensureIndices ()
154158 {
155- $ chunksCollection = $ this ->config ->getGridFs ()->chunks ;
159+ $ chunksCollection = $ this ->config ->getGridFs ()->getChunksCollection () ;
156160 $ indexKeys = ['files_id ' => 1 , 'n ' => 1 ];
157161 $ indexOptions = ['unique ' => true , 'background ' => true ];
158- if (method_exists ($ chunksCollection , 'createIndex ' )) { // only available for PECL mongo >= 1.5.0
159- $ chunksCollection ->createIndex ($ indexKeys , $ indexOptions );
160- } else {
161- $ chunksCollection ->ensureIndex ($ indexKeys , $ indexOptions );
162- }
162+ $ chunksCollection ->createIndex ($ indexKeys , $ indexOptions );
163163 }
164164
165165 /**
0 commit comments