Skip to content

Commit 0195906

Browse files
committed
Merge pull request #239
2 parents 1e3be59 + 57b4012 commit 0195906

File tree

6 files changed

+105
-27
lines changed

6 files changed

+105
-27
lines changed

src/GridFS/Bucket.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,20 +218,44 @@ public function getDatabaseName()
218218
}
219219

220220
/**
221-
* Gets the ID of the GridFS file associated with a stream.
221+
* Gets the file document of the GridFS file associated with a stream.
222222
*
223223
* @param resource $stream GridFS stream
224-
* @return mixed
224+
* @return stdClass
225+
* @throws InvalidArgumentException
225226
*/
226-
public function getIdFromStream($stream)
227+
public function getFileDocumentForStream($stream)
227228
{
229+
if ( ! is_resource($stream) || get_resource_type($stream) != "stream") {
230+
throw InvalidArgumentException::invalidType('$stream', $stream, 'resource');
231+
}
232+
228233
$metadata = stream_get_meta_data($stream);
229234

230-
if ($metadata['wrapper_data'] instanceof StreamWrapper) {
231-
return $metadata['wrapper_data']->getId();
235+
if (!$metadata['wrapper_data'] instanceof StreamWrapper) {
236+
throw InvalidArgumentException::invalidType('$stream wrapper data', $metadata['wrapper_data'], 'MongoDB\Driver\GridFS\StreamWrapper');
237+
}
238+
239+
return $metadata['wrapper_data']->getFile();
240+
}
241+
242+
/**
243+
* Gets the file document's ID of the GridFS file associated with a stream.
244+
*
245+
* @param resource $stream GridFS stream
246+
* @return stdClass
247+
* @throws CorruptFileException
248+
* @throws InvalidArgumentException
249+
*/
250+
public function getFileIdForStream($stream)
251+
{
252+
$file = $this->getFileDocumentForStream($stream);
253+
254+
if ( ! isset($file->_id) && ! property_exists($file, '_id')) {
255+
throw new CorruptFileException('file._id does not exist');
232256
}
233257

234-
// TODO: Throw if we cannot access the ID
258+
return $file->_id;
235259
}
236260

237261
/**
@@ -379,7 +403,7 @@ public function uploadFromStream($filename, $source, array $options = [])
379403
$destination = $this->openUploadStream($filename, $options);
380404
stream_copy_to_stream($source, $destination);
381405

382-
return $this->getIdFromStream($destination);
406+
return $this->getFileIdForStream($destination);
383407
}
384408

385409
/**

src/GridFS/ReadableStream.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class ReadableStream
2121
private $chunkOffset = 0;
2222
private $chunksIterator;
2323
private $collectionWrapper;
24+
private $file;
2425
private $firstCheck = true;
25-
private $id;
2626
private $iteratorEmpty = false;
2727
private $length;
2828
private $numChunks;
@@ -44,15 +44,15 @@ public function __construct(CollectionWrapper $collectionWrapper, stdClass $file
4444
throw new CorruptFileException('file.length is not an integer > 0');
4545
}
4646

47-
if ( ! isset($file->_id) && ! array_key_exists('_id', (array) $file)) {
47+
if ( ! isset($file->_id) && ! property_exists($file, '_id')) {
4848
throw new CorruptFileException('file._id does not exist');
4949
}
5050

51-
$this->id = $file->_id;
51+
$this->file = $file;
5252
$this->chunkSize = $file->chunkSize;
5353
$this->length = $file->length;
5454

55-
$this->chunksIterator = $collectionWrapper->getChunksIteratorByFilesId($this->id);
55+
$this->chunksIterator = $collectionWrapper->getChunksIteratorByFilesId($file->_id);
5656
$this->collectionWrapper = $collectionWrapper;
5757
$this->numChunks = ceil($this->length / $this->chunkSize);
5858
$this->initEmptyBuffer();
@@ -69,9 +69,7 @@ public function __debugInfo()
6969
return [
7070
'bucketName' => $this->collectionWrapper->getBucketName(),
7171
'databaseName' => $this->collectionWrapper->getDatabaseName(),
72-
'id' => $this->id,
73-
'chunkSize' => $this->chunkSize,
74-
'length' => $this->length,
72+
'file' => $this->file,
7573
];
7674
}
7775

@@ -130,13 +128,13 @@ public function downloadNumBytes($numBytes)
130128
}
131129

132130
/**
133-
* Return the stream's ID (i.e. file document identifier).
131+
* Return the stream's file document.
134132
*
135-
* @return integer
133+
* @return stdClass
136134
*/
137-
public function getId()
135+
public function getFile()
138136
{
139-
return $this->id;
137+
return $this->file;
140138
}
141139

142140
/**

src/GridFS/StreamWrapper.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ class StreamWrapper
2222
private $protocol;
2323
private $stream;
2424

25-
public function getId()
25+
/**
26+
* Return the stream's file document.
27+
*
28+
* @return stdClass
29+
*/
30+
public function getFile()
2631
{
27-
return $this->stream->getId();
32+
return $this->stream->getFile();
2833
}
2934

3035
/**

src/GridFS/WritableStream.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ public function close()
127127
}
128128

129129
/**
130-
* Return the stream's ID (i.e. file document identifier).
130+
* Return the stream's file document.
131131
*
132-
* @return integer
132+
* @return stdClass
133133
*/
134-
public function getId()
134+
public function getFile()
135135
{
136-
return $this->file['_id'];
136+
return (object) $this->file;
137137
}
138138

139139
/**

tests/GridFS/BucketFunctionalTest.php

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,63 @@ public function testGetDatabaseName()
323323
$this->assertEquals($this->getDatabaseName(), $this->bucket->getDatabaseName());
324324
}
325325

326-
public function testGetIdFromStream()
326+
public function testGetFileDocumentForStreamWithReadableStream()
327+
{
328+
$metadata = ['foo' => 'bar'];
329+
$id = $this->bucket->uploadFromStream('filename', $this->createStream('foobar'), ['metadata' => $metadata]);
330+
$stream = $this->bucket->openDownloadStream($id);
331+
332+
$fileDocument = $this->bucket->getFileDocumentForStream($stream);
333+
334+
$this->assertEquals($id, $fileDocument->_id);
335+
$this->assertSame('filename', $fileDocument->filename);
336+
$this->assertSame(6, $fileDocument->length);
337+
$this->assertSameDocument($metadata, $fileDocument->metadata);
338+
}
339+
340+
public function testGetFileDocumentForStreamWithWritableStream()
341+
{
342+
$metadata = ['foo' => 'bar'];
343+
$stream = $this->bucket->openUploadStream('filename', ['_id' => 1, 'metadata' => $metadata]);
344+
345+
$fileDocument = $this->bucket->getFileDocumentForStream($stream);
346+
347+
$this->assertEquals(1, $fileDocument->_id);
348+
$this->assertSame('filename', $fileDocument->filename);
349+
$this->assertSameDocument($metadata, $fileDocument->metadata);
350+
}
351+
352+
/**
353+
* @expectedException MongoDB\Exception\InvalidArgumentException
354+
* @dataProvider provideInvalidStreamValues
355+
*/
356+
public function testGetFileDocumentForStreamShouldRequireStreamResource($stream)
357+
{
358+
$this->bucket->getFileDocumentForStream($stream);
359+
}
360+
361+
public function testGetFileIdForStreamWithReadableStream()
327362
{
328363
$id = $this->bucket->uploadFromStream('filename', $this->createStream('foobar'));
329364
$stream = $this->bucket->openDownloadStream($id);
330365

331-
$this->assertEquals($id, $this->bucket->getIdFromStream($stream));
366+
$this->assertEquals($id, $this->bucket->getFileIdForStream($stream));
367+
}
368+
369+
public function testGetFileIdForStreamWithWritableStream()
370+
{
371+
$stream = $this->bucket->openUploadStream('filename', ['_id' => 1]);
372+
373+
$this->assertEquals(1, $this->bucket->getFileIdForStream($stream));
374+
}
375+
376+
/**
377+
* @expectedException MongoDB\Exception\InvalidArgumentException
378+
* @dataProvider provideInvalidStreamValues
379+
*/
380+
public function testGetFileIdForStreamShouldRequireStreamResource($stream)
381+
{
382+
$this->bucket->getFileIdForStream($stream);
332383
}
333384

334385
/**

tests/GridFS/WritableStreamFunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testInsertChunksCalculatesMD5($input, $expectedMD5)
6262
$stream->close();
6363

6464
$fileDocument = $this->filesCollection->findOne(
65-
['_id' => $stream->getId()],
65+
['_id' => $stream->getFile()->_id],
6666
['projection' => ['md5' => 1, '_id' => 0]]
6767
);
6868

0 commit comments

Comments
 (0)