Skip to content

Commit 3cc3363

Browse files
committed
Refactor GridFS chunk inserts and check for buffer fwrite() errors
1 parent 045731c commit 3cc3363

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

src/GridFS/StreamWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function stream_write($data)
150150
}
151151

152152
try {
153-
return $this->stream->insertChunks($data);
153+
return $this->stream->writeBytes($data);
154154
} catch (Exception $e) {
155155
trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), \E_USER_WARNING);
156156
return false;

src/GridFS/WritableStream.php

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use MongoDB\BSON\ObjectId;
77
use MongoDB\BSON\UTCDateTime;
88
use MongoDB\Exception\InvalidArgumentException;
9+
use MongoDB\Exception\RuntimeException;
910

1011
/**
1112
* WritableStream abstracts the process of writing a GridFS file.
@@ -113,14 +114,10 @@ public function close()
113114
return;
114115
}
115116

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();
121119
}
122120

123-
fclose($this->buffer);
124121
$this->fileCollectionInsert();
125122
$this->isClosed = true;
126123
}
@@ -151,36 +148,36 @@ public function getSize()
151148
* Inserts binary data into GridFS via chunks.
152149
*
153150
* 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.
156152
*
157-
* @param string $toWrite Binary data to write
153+
* @param string $data Binary data to write
158154
* @return integer
159155
*/
160-
public function insertChunks($toWrite)
156+
public function writeBytes($data)
161157
{
162158
if ($this->isClosed) {
163159
// TODO: Should this be an error condition? e.g. BadMethodCallException
164160
return;
165161
}
166162

167-
$readBytes = 0;
163+
$bytesRead = 0;
164+
165+
while ($bytesRead != strlen($data)) {
166+
$bytesWritten = fwrite($this->buffer, substr($data, $bytesRead, $this->chunkSize - $this->bufferLength));
168167

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;
174174

175175
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();
180177
}
181178
}
182179

183-
return $readBytes;
180+
return $bytesRead;
184181
}
185182

186183
private function abort()
@@ -206,22 +203,31 @@ private function fileCollectionInsert()
206203
return $this->file['_id'];
207204
}
208205

209-
private function insertChunk($data)
206+
private function insertChunkFromBuffer()
210207
{
211208
if ($this->isClosed) {
212209
// TODO: Should this be an error condition? e.g. BadMethodCallException
213210
return;
214211
}
215212

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 = [
217223
'files_id' => $this->file['_id'],
218224
'n' => $this->chunkOffset,
219225
'data' => new Binary($data, Binary::TYPE_GENERIC),
220226
];
221227

222228
hash_update($this->ctx, $data);
223229

224-
$this->collectionWrapper->insertChunk($toUpload);
230+
$this->collectionWrapper->insertChunk($chunk);
225231
$this->length += strlen($data);
226232
$this->chunkOffset++;
227233
}

tests/GridFS/WritableStreamFunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ public function provideInvalidConstructorOptions()
5555
/**
5656
* @dataProvider provideInputDataAndExpectedMD5
5757
*/
58-
public function testInsertChunksCalculatesMD5($input, $expectedMD5)
58+
public function testWriteBytesCalculatesMD5($input, $expectedMD5)
5959
{
6060
$stream = new WritableStream($this->collectionWrapper, 'filename');
61-
$stream->insertChunks($input);
61+
$stream->writeBytes($input);
6262
$stream->close();
6363

6464
$fileDocument = $this->filesCollection->findOne(

0 commit comments

Comments
 (0)