6
6
use MongoDB \BSON \ObjectId ;
7
7
use MongoDB \BSON \UTCDateTime ;
8
8
use MongoDB \Exception \InvalidArgumentException ;
9
+ use MongoDB \Exception \RuntimeException ;
9
10
10
11
/**
11
12
* WritableStream abstracts the process of writing a GridFS file.
@@ -113,14 +114,10 @@ public function close()
113
114
return ;
114
115
}
115
116
116
- rewind ($ this ->buffer );
117
- $ cached = stream_get_contents ($ this ->buffer );
118
-
119
- if (strlen ($ cached ) > 0 ) {
120
- $ this ->insertChunk ($ cached );
117
+ if ($ this ->bufferLength > 0 ) {
118
+ $ this ->insertChunkFromBuffer ();
121
119
}
122
120
123
- fclose ($ this ->buffer );
124
121
$ this ->fileCollectionInsert ();
125
122
$ this ->isClosed = true ;
126
123
}
@@ -151,36 +148,36 @@ public function getSize()
151
148
* Inserts binary data into GridFS via chunks.
152
149
*
153
150
* Data will be buffered internally until chunkSizeBytes are accumulated, at
154
- * which point a chunk's worth of data will be inserted and the buffer
155
- * reset.
151
+ * which point a chunk document will be inserted and the buffer reset.
156
152
*
157
- * @param string $toWrite Binary data to write
153
+ * @param string $data Binary data to write
158
154
* @return integer
159
155
*/
160
- public function insertChunks ( $ toWrite )
156
+ public function writeBytes ( $ data )
161
157
{
162
158
if ($ this ->isClosed ) {
163
159
// TODO: Should this be an error condition? e.g. BadMethodCallException
164
160
return ;
165
161
}
166
162
167
- $ readBytes = 0 ;
163
+ $ bytesRead = 0 ;
164
+
165
+ while ($ bytesRead != strlen ($ data )) {
166
+ $ bytesWritten = fwrite ($ this ->buffer , substr ($ data , $ bytesRead , $ this ->chunkSize - $ this ->bufferLength ));
168
167
169
- while ($ readBytes != strlen ($ toWrite )) {
170
- $ addToBuffer = substr ($ toWrite , $ readBytes , $ this ->chunkSize - $ this ->bufferLength );
171
- fwrite ($ this ->buffer , $ addToBuffer );
172
- $ readBytes += strlen ($ addToBuffer );
173
- $ this ->bufferLength += strlen ($ addToBuffer );
168
+ if ($ bytesWritten === false ) {
169
+ throw new RuntimeException ('fwrite() failed ' );
170
+ }
171
+
172
+ $ bytesRead += $ bytesWritten ;
173
+ $ this ->bufferLength += $ bytesWritten ;
174
174
175
175
if ($ this ->bufferLength == $ this ->chunkSize ) {
176
- rewind ($ this ->buffer );
177
- $ this ->insertChunk (stream_get_contents ($ this ->buffer ));
178
- ftruncate ($ this ->buffer , 0 );
179
- $ this ->bufferLength = 0 ;
176
+ $ this ->insertChunkFromBuffer ();
180
177
}
181
178
}
182
179
183
- return $ readBytes ;
180
+ return $ bytesRead ;
184
181
}
185
182
186
183
private function abort ()
@@ -206,22 +203,31 @@ private function fileCollectionInsert()
206
203
return $ this ->file ['_id ' ];
207
204
}
208
205
209
- private function insertChunk ( $ data )
206
+ private function insertChunkFromBuffer ( )
210
207
{
211
208
if ($ this ->isClosed ) {
212
209
// TODO: Should this be an error condition? e.g. BadMethodCallException
213
210
return ;
214
211
}
215
212
216
- $ toUpload = [
213
+ $ data = stream_get_contents ($ this ->buffer , -1 , 0 );
214
+
215
+ if (strlen ($ data ) == 0 ) {
216
+ return ;
217
+ }
218
+
219
+ ftruncate ($ this ->buffer , 0 );
220
+ $ this ->bufferLength = 0 ;
221
+
222
+ $ chunk = [
217
223
'files_id ' => $ this ->file ['_id ' ],
218
224
'n ' => $ this ->chunkOffset ,
219
225
'data ' => new Binary ($ data , Binary::TYPE_GENERIC ),
220
226
];
221
227
222
228
hash_update ($ this ->ctx , $ data );
223
229
224
- $ this ->collectionWrapper ->insertChunk ($ toUpload );
230
+ $ this ->collectionWrapper ->insertChunk ($ chunk );
225
231
$ this ->length += strlen ($ data );
226
232
$ this ->chunkOffset ++;
227
233
}
0 commit comments