2
2
3
3
namespace Flow \Mongo ;
4
4
5
+ use Exception ;
5
6
use Flow \File ;
6
7
use Flow \Request ;
7
8
use Flow \RequestInterface ;
9
+ use MongoDB \BSON \Binary ;
10
+ use MongoDB \BSON \ObjectId ;
11
+ use MongoDB \BSON \UTCDateTime ;
12
+ use MongoDB \Operation \FindOneAndReplace ;
8
13
9
14
10
15
/**
@@ -42,9 +47,9 @@ protected function getGridFsFile()
42
47
if (!$ this ->uploadGridFsFile ) {
43
48
$ gridFsFileQuery = $ this ->getGridFsFileQuery ();
44
49
$ 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 ]);
48
53
}
49
54
50
55
return $ this ->uploadGridFsFile ;
@@ -56,10 +61,10 @@ protected function getGridFsFile()
56
61
*/
57
62
public function chunkExists ($ index )
58
63
{
59
- return $ this ->config ->getGridFs ()->chunks -> find ([
64
+ return $ this ->config ->getGridFs ()->getChunksCollection ()-> findOne ([
60
65
'files_id ' => $ this ->getGridFsFile ()['_id ' ],
61
66
'n ' => (intval ($ index ) - 1 )
62
- ])-> limit ( 1 )-> hasNext () ;
67
+ ]) !== null ;
63
68
}
64
69
65
70
public function checkChunk ()
@@ -71,7 +76,7 @@ public function checkChunk()
71
76
* Save chunk
72
77
* @param $additionalUpdateOptions array additional options for the mongo update/upsert operation.
73
78
* @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.
75
80
*/
76
81
public function saveChunk ($ additionalUpdateOptions = [])
77
82
{
@@ -89,19 +94,19 @@ public function saveChunk($additionalUpdateOptions = [])
89
94
($ actualChunkSize < $ this ->request ->getDefaultChunkSize () &&
90
95
$ this ->request ->getCurrentChunkNumber () != $ this ->request ->getTotalChunks ())
91
96
) {
92
- throw new \ Exception ("Invalid upload! (size: { $ actualChunkSize} ) " );
97
+ throw new Exception ("Invalid upload! (size: $ actualChunkSize) " );
93
98
}
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 ));
96
101
unlink ($ file ['tmp_name ' ]);
97
102
98
103
$ this ->ensureIndices ();
99
104
100
105
return true ;
101
- } catch (\ Exception $ e ) {
106
+ } catch (Exception $ e ) {
102
107
// try to remove a possibly (partly) stored chunk:
103
108
if (isset ($ chunkQuery )) {
104
- $ this ->config ->getGridFs ()->chunks -> remove ($ chunkQuery );
109
+ $ this ->config ->getGridFs ()->getChunksCollection ()-> deleteMany ($ chunkQuery );
105
110
}
106
111
throw $ e ;
107
112
}
@@ -113,25 +118,24 @@ public function saveChunk($additionalUpdateOptions = [])
113
118
public function validateFile ()
114
119
{
115
120
$ 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 ' ]]);
119
123
return $ totalChunks === $ storedChunks ;
120
124
}
121
125
122
126
123
127
/**
124
128
* Merge all chunks to single file
125
129
* @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
128
132
*/
129
133
public function saveToGridFs ($ metadata = null )
130
134
{
131
135
$ file = $ this ->getGridFsFile ();
132
136
$ file ['flowStatus ' ] = 'finished ' ;
133
137
$ file ['metadata ' ] = $ metadata ;
134
- $ result = $ this ->config ->getGridFs ()->findAndModify ($ this ->getGridFsFileQuery (), $ file );
138
+ $ result = $ this ->config ->getGridFs ()->getFilesCollection ()-> findOneAndReplace ($ this ->getGridFsFileQuery (), $ file );
135
139
// on second invocation no more file can be found, as the flowStatus changed:
136
140
if (is_null ($ result )) {
137
141
return false ;
@@ -142,7 +146,7 @@ public function saveToGridFs($metadata = null)
142
146
143
147
public function save ($ destination )
144
148
{
145
- throw new \ Exception ("Must not use 'save' on MongoFile - use 'saveToGridFs'! " );
149
+ throw new Exception ("Must not use 'save' on MongoFile - use 'saveToGridFs'! " );
146
150
}
147
151
148
152
public function deleteChunks ()
@@ -152,14 +156,10 @@ public function deleteChunks()
152
156
153
157
public function ensureIndices ()
154
158
{
155
- $ chunksCollection = $ this ->config ->getGridFs ()->chunks ;
159
+ $ chunksCollection = $ this ->config ->getGridFs ()->getChunksCollection () ;
156
160
$ indexKeys = ['files_id ' => 1 , 'n ' => 1 ];
157
161
$ 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 );
163
163
}
164
164
165
165
/**
0 commit comments