@@ -17,11 +17,13 @@ class ReadableStream
17
17
private $ bufferEmpty ;
18
18
private $ bufferFresh ;
19
19
private $ bytesSeen = 0 ;
20
+ private $ chunkSize ;
20
21
private $ chunkOffset = 0 ;
21
22
private $ chunksIterator ;
22
- private $ file ;
23
23
private $ firstCheck = true ;
24
+ private $ id ;
24
25
private $ iteratorEmpty = false ;
26
+ private $ length ;
25
27
private $ numChunks ;
26
28
27
29
/**
@@ -33,10 +35,24 @@ class ReadableStream
33
35
*/
34
36
public function __construct (CollectionWrapper $ collectionWrapper , stdClass $ file )
35
37
{
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 ;
37
53
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 );
40
56
$ this ->initEmptyBuffer ();
41
57
}
42
58
@@ -77,7 +93,7 @@ public function downloadNumBytes($numBytes)
77
93
$ output .= substr ($ this ->chunksIterator ->current ()->data ->getData (), 0 , $ bytesLeft );
78
94
}
79
95
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 ())) {
81
97
fwrite ($ this ->buffer , substr ($ this ->chunksIterator ->current ()->data ->getData (), $ bytesLeft ));
82
98
$ this ->bufferEmpty = false ;
83
99
}
@@ -103,19 +119,24 @@ public function downloadToStream($destination)
103
119
}
104
120
}
105
121
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
+ */
111
127
public function getId ()
112
128
{
113
- return $ this ->file -> _id ;
129
+ return $ this ->id ;
114
130
}
115
131
132
+ /**
133
+ * Return the stream size in bytes.
134
+ *
135
+ * @return integer
136
+ */
116
137
public function getSize ()
117
138
{
118
- return $ this ->file -> length ;
139
+ return $ this ->length ;
119
140
}
120
141
121
142
public function isEOF ()
@@ -149,8 +170,8 @@ private function advanceChunks()
149
170
$ actualChunkSize = strlen ($ this ->chunksIterator ->current ()->data ->getData ());
150
171
151
172
$ expectedChunkSize = ($ this ->chunkOffset == $ this ->numChunks - 1 )
152
- ? ($ this ->file -> length - $ this ->bytesSeen )
153
- : $ this ->file -> chunkSize ;
173
+ ? ($ this ->length - $ this ->bytesSeen )
174
+ : $ this ->chunkSize ;
154
175
155
176
if ($ actualChunkSize != $ expectedChunkSize ) {
156
177
throw CorruptFileException::unexpectedSize ($ actualChunkSize , $ expectedChunkSize );
0 commit comments