@@ -107,11 +107,8 @@ public function uploadFromStream($source)
107
107
throw new UnexpectedTypeException ('stream ' , $ source );
108
108
} else {
109
109
$ streamMetadata = stream_get_meta_data ($ source );
110
- } if (!is_readable ($ streamMetadata ['uri ' ])) {
111
- // throw new InvalidArgumentException("stream not readable");
112
- //issue being that php's is_readable reports native streams as not readable like php://temp
113
- }
114
- while ($ data = fread ($ source , $ this ->chunkSize )) {
110
+
111
+ while ($ data = $ this ->readChunk ($ source )) {
115
112
$ this ->insertChunk ($ data );
116
113
}
117
114
return $ this ->fileCollectionInsert ();
@@ -198,12 +195,7 @@ private function insertChunk($data)
198
195
}
199
196
$ toUpload = ["files_id " => $ this ->file ['_id ' ], "n " => $ this ->chunkOffset , "data " => new \MongoDB \BSON \Binary ($ data , \MongoDB \BSON \Binary::TYPE_GENERIC )];
200
197
hash_update ($ this ->ctx , $ data );
201
- try {
202
- $ this ->collectionsWrapper ->chunkInsert ($ toUpload );
203
- } catch (\MongoDB \Exception $ e ){
204
- $ this ->abort ();
205
- throw $ e ;
206
- }
198
+ $ this ->collectionsWrapper ->chunkInsert ($ toUpload );
207
199
$ this ->length += strlen ($ data );
208
200
$ this ->chunkOffset ++;
209
201
}
@@ -214,12 +206,7 @@ private function fileCollectionInsert()
214
206
}
215
207
$ md5 = hash_final ($ this ->ctx );
216
208
$ this ->file = array_merge ($ this ->file , ['length ' => $ this ->length , 'md5 ' => $ md5 ]);
217
- try {
218
- $ this ->collectionsWrapper ->fileInsert ($ this ->file );
219
- } catch (\MongoDB \Exception $ e ){
220
- $ this ->abort ();
221
- throw $ e ;
222
- }
209
+ $ this ->collectionsWrapper ->fileInsert ($ this ->file );
223
210
return $ this ->file ['_id ' ];
224
211
}
225
212
//from: http://stackoverflow.com/questions/3656713/how-to-get-current-time-in-milliseconds-in-php
@@ -228,4 +215,16 @@ private function millitime() {
228
215
$ comps = explode (' ' , $ microtime );
229
216
return sprintf ('%d%03d ' , $ comps [1 ], $ comps [0 ] * 1000 );
230
217
}
218
+
219
+ private function readChunk ($ source )
220
+ {
221
+ $ data ;
222
+ try {
223
+ $ data = fread ($ source , $ this ->chunkSize );
224
+ } catch (Exception $ e ) {
225
+ $ this ->abort ();
226
+ throw $ e ;
227
+ }
228
+ return $ data ;
229
+ }
231
230
}
0 commit comments