Skip to content

Commit 6ab8c72

Browse files
committed
Do not store entire files document in ReadableStream
1 parent 3cf9944 commit 6ab8c72

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

src/GridFS/ReadableStream.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ class ReadableStream
1717
private $bufferEmpty;
1818
private $bufferFresh;
1919
private $bytesSeen = 0;
20+
private $chunkSize;
2021
private $chunkOffset = 0;
2122
private $chunksIterator;
22-
private $file;
2323
private $firstCheck = true;
24+
private $id;
2425
private $iteratorEmpty = false;
26+
private $length;
2527
private $numChunks;
2628

2729
/**
@@ -33,10 +35,24 @@ class ReadableStream
3335
*/
3436
public function __construct(CollectionWrapper $collectionWrapper, stdClass $file)
3537
{
36-
$this->file = $file;
38+
if ( ! isset($file->chunkSize) || ! is_integer($file->chunkSize) || $file->chunkSize < 1) {
39+
throw new CorruptFileException('file.chunkSize is not an integer >= 1');
40+
}
41+
42+
if ( ! isset($file->length) || ! is_integer($file->length) || $file->length < 0) {
43+
throw new CorruptFileException('file.length is not an integer > 0');
44+
}
45+
46+
if ( ! isset($file->_id) && ! array_key_exists('_id', (array) $file)) {
47+
throw new CorruptFileException('file._id does not exist');
48+
}
49+
50+
$this->id = $file->_id;
51+
$this->chunkSize = $file->chunkSize;
52+
$this->length = $file->length;
3753

38-
$this->chunksIterator = $collectionWrapper->getChunksIteratorByFilesId($this->file->_id);
39-
$this->numChunks = ($file->length >= 0) ? ceil($file->length / $file->chunkSize) : 0;
54+
$this->chunksIterator = $collectionWrapper->getChunksIteratorByFilesId($this->id);
55+
$this->numChunks = ceil($this->length / $this->chunkSize);
4056
$this->initEmptyBuffer();
4157
}
4258

@@ -77,7 +93,7 @@ public function downloadNumBytes($numBytes)
7793
$output .= substr($this->chunksIterator->current()->data->getData(), 0, $bytesLeft);
7894
}
7995

80-
if ( ! $this->iteratorEmpty && $this->file->length > 0 && $bytesLeft < strlen($this->chunksIterator->current()->data->getData())) {
96+
if ( ! $this->iteratorEmpty && $this->length > 0 && $bytesLeft < strlen($this->chunksIterator->current()->data->getData())) {
8197
fwrite($this->buffer, substr($this->chunksIterator->current()->data->getData(), $bytesLeft));
8298
$this->bufferEmpty = false;
8399
}
@@ -103,19 +119,24 @@ public function downloadToStream($destination)
103119
}
104120
}
105121

106-
public function getFile()
107-
{
108-
return $this->file;
109-
}
110-
122+
/**
123+
* Return the ID of the GridFS file document for this stream.
124+
*
125+
* @return integer
126+
*/
111127
public function getId()
112128
{
113-
return $this->file->_id;
129+
return $this->id;
114130
}
115131

132+
/**
133+
* Return the stream size in bytes.
134+
*
135+
* @return integer
136+
*/
116137
public function getSize()
117138
{
118-
return $this->file->length;
139+
return $this->length;
119140
}
120141

121142
public function isEOF()
@@ -149,8 +170,8 @@ private function advanceChunks()
149170
$actualChunkSize = strlen($this->chunksIterator->current()->data->getData());
150171

151172
$expectedChunkSize = ($this->chunkOffset == $this->numChunks - 1)
152-
? ($this->file->length - $this->bytesSeen)
153-
: $this->file->chunkSize;
173+
? ($this->length - $this->bytesSeen)
174+
: $this->chunkSize;
154175

155176
if ($actualChunkSize != $expectedChunkSize) {
156177
throw CorruptFileException::unexpectedSize($actualChunkSize, $expectedChunkSize);

0 commit comments

Comments
 (0)