14
14
* - Chunk preprocessor not supported (must not modify chunks size)!
15
15
* - Must use 'forceChunkSize=true' on client side.
16
16
*
17
- * @package Flow
17
+ * @codeCoverageIgnore
18
18
*/
19
19
class MongoFile extends File
20
20
{
@@ -44,7 +44,7 @@ protected function getGridFsFile()
44
44
$ changed = $ gridFsFileQuery ;
45
45
$ changed ['flowUpdated ' ] = new \MongoDate ();
46
46
$ this ->uploadGridFsFile = $ this ->config ->getGridFs ()->findAndModify ($ gridFsFileQuery , $ changed , null ,
47
- ['upsert ' => true ]);
47
+ ['upsert ' => true , ' new ' => true ]);
48
48
}
49
49
50
50
return $ this ->uploadGridFsFile ;
@@ -69,8 +69,8 @@ public function checkChunk()
69
69
70
70
/**
71
71
* Save chunk
72
- *
73
72
* @return bool
73
+ * @throws \Exception if upload size is invalid or some other unexpected error occurred.
74
74
*/
75
75
public function saveChunk ()
76
76
{
@@ -90,13 +90,19 @@ public function saveChunk()
90
90
) {
91
91
throw new \Exception ("Invalid upload! (size: {$ actualChunkSize }) " );
92
92
}
93
- $ chunk ['data ' ] = new \MongoBinData ($ data);
93
+ $ chunk ['data ' ] = new \MongoBinData ($ data, 0 ); // \MongoBinData::GENERIC is not defined for older mongo drivers
94
94
$ this ->config ->getGridFs ()->chunks ->findAndModify ($ chunkQuery , $ chunk , [], ['upsert ' => true ]);
95
95
unlink ($ file ['tmp_name ' ]);
96
96
97
+ $ this ->ensureIndices ();
98
+
97
99
return true ;
98
100
} catch (\Exception $ e ) {
99
- return false ;
101
+ // try to remove a possibly (partly) stored chunk:
102
+ if (isset ($ chunkQuery )) {
103
+ $ this ->config ->getGridFs ()->chunks ->remove ($ chunkQuery );
104
+ }
105
+ throw $ e ;
100
106
}
101
107
}
102
108
@@ -147,6 +153,18 @@ public function deleteChunks()
147
153
// nothing to do, as chunks are directly part of the final file
148
154
}
149
155
156
+ public function ensureIndices ()
157
+ {
158
+ $ chunksCollection = $ this ->config ->getGridFs ()->chunks ;
159
+ $ indexKeys = ['files_id ' => 1 , 'n ' => 1 ];
160
+ $ indexOptions = ['unique ' => true , 'background ' => true ];
161
+ if (method_exists ($ chunksCollection , 'createIndex ' )) { // only available for PECL mongo >= 1.5.0
162
+ $ chunksCollection ->createIndex ($ indexKeys , $ indexOptions );
163
+ } else {
164
+ $ chunksCollection ->ensureIndex ($ indexKeys , $ indexOptions );
165
+ }
166
+ }
167
+
150
168
/**
151
169
* @return array
152
170
*/
@@ -160,4 +178,4 @@ protected function getGridFsFileQuery()
160
178
'length ' => intval ($ this ->request ->getTotalSize ())
161
179
];
162
180
}
163
- }
181
+ }
0 commit comments