Skip to content

Commit

Permalink
Merge pull request #239
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikola committed Sep 12, 2016
2 parents 1e3be59 + 57b4012 commit 0195906
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 27 deletions.
38 changes: 31 additions & 7 deletions src/GridFS/Bucket.php
Expand Up @@ -218,20 +218,44 @@ public function getDatabaseName()
}

/**
* Gets the ID of the GridFS file associated with a stream.
* Gets the file document of the GridFS file associated with a stream.
*
* @param resource $stream GridFS stream
* @return mixed
* @return stdClass
* @throws InvalidArgumentException
*/
public function getIdFromStream($stream)
public function getFileDocumentForStream($stream)
{
if ( ! is_resource($stream) || get_resource_type($stream) != "stream") {
throw InvalidArgumentException::invalidType('$stream', $stream, 'resource');
}

$metadata = stream_get_meta_data($stream);

if ($metadata['wrapper_data'] instanceof StreamWrapper) {
return $metadata['wrapper_data']->getId();
if (!$metadata['wrapper_data'] instanceof StreamWrapper) {
throw InvalidArgumentException::invalidType('$stream wrapper data', $metadata['wrapper_data'], 'MongoDB\Driver\GridFS\StreamWrapper');
}

return $metadata['wrapper_data']->getFile();
}

/**
* Gets the file document's ID of the GridFS file associated with a stream.
*
* @param resource $stream GridFS stream
* @return stdClass
* @throws CorruptFileException
* @throws InvalidArgumentException
*/
public function getFileIdForStream($stream)
{
$file = $this->getFileDocumentForStream($stream);

if ( ! isset($file->_id) && ! property_exists($file, '_id')) {
throw new CorruptFileException('file._id does not exist');
}

// TODO: Throw if we cannot access the ID
return $file->_id;
}

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

return $this->getIdFromStream($destination);
return $this->getFileIdForStream($destination);
}

/**
Expand Down
20 changes: 9 additions & 11 deletions src/GridFS/ReadableStream.php
Expand Up @@ -21,8 +21,8 @@ class ReadableStream
private $chunkOffset = 0;
private $chunksIterator;
private $collectionWrapper;
private $file;
private $firstCheck = true;
private $id;
private $iteratorEmpty = false;
private $length;
private $numChunks;
Expand All @@ -44,15 +44,15 @@ public function __construct(CollectionWrapper $collectionWrapper, stdClass $file
throw new CorruptFileException('file.length is not an integer > 0');
}

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

$this->id = $file->_id;
$this->file = $file;
$this->chunkSize = $file->chunkSize;
$this->length = $file->length;

$this->chunksIterator = $collectionWrapper->getChunksIteratorByFilesId($this->id);
$this->chunksIterator = $collectionWrapper->getChunksIteratorByFilesId($file->_id);
$this->collectionWrapper = $collectionWrapper;
$this->numChunks = ceil($this->length / $this->chunkSize);
$this->initEmptyBuffer();
Expand All @@ -69,9 +69,7 @@ public function __debugInfo()
return [
'bucketName' => $this->collectionWrapper->getBucketName(),
'databaseName' => $this->collectionWrapper->getDatabaseName(),
'id' => $this->id,
'chunkSize' => $this->chunkSize,
'length' => $this->length,
'file' => $this->file,
];
}

Expand Down Expand Up @@ -130,13 +128,13 @@ public function downloadNumBytes($numBytes)
}

/**
* Return the stream's ID (i.e. file document identifier).
* Return the stream's file document.
*
* @return integer
* @return stdClass
*/
public function getId()
public function getFile()
{
return $this->id;
return $this->file;
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/GridFS/StreamWrapper.php
Expand Up @@ -22,9 +22,14 @@ class StreamWrapper
private $protocol;
private $stream;

public function getId()
/**
* Return the stream's file document.
*
* @return stdClass
*/
public function getFile()
{
return $this->stream->getId();
return $this->stream->getFile();
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/GridFS/WritableStream.php
Expand Up @@ -127,13 +127,13 @@ public function close()
}

/**
* Return the stream's ID (i.e. file document identifier).
* Return the stream's file document.
*
* @return integer
* @return stdClass
*/
public function getId()
public function getFile()
{
return $this->file['_id'];
return (object) $this->file;
}

/**
Expand Down
55 changes: 53 additions & 2 deletions tests/GridFS/BucketFunctionalTest.php
Expand Up @@ -323,12 +323,63 @@ public function testGetDatabaseName()
$this->assertEquals($this->getDatabaseName(), $this->bucket->getDatabaseName());
}

public function testGetIdFromStream()
public function testGetFileDocumentForStreamWithReadableStream()
{
$metadata = ['foo' => 'bar'];
$id = $this->bucket->uploadFromStream('filename', $this->createStream('foobar'), ['metadata' => $metadata]);
$stream = $this->bucket->openDownloadStream($id);

$fileDocument = $this->bucket->getFileDocumentForStream($stream);

$this->assertEquals($id, $fileDocument->_id);
$this->assertSame('filename', $fileDocument->filename);
$this->assertSame(6, $fileDocument->length);
$this->assertSameDocument($metadata, $fileDocument->metadata);
}

public function testGetFileDocumentForStreamWithWritableStream()
{
$metadata = ['foo' => 'bar'];
$stream = $this->bucket->openUploadStream('filename', ['_id' => 1, 'metadata' => $metadata]);

$fileDocument = $this->bucket->getFileDocumentForStream($stream);

$this->assertEquals(1, $fileDocument->_id);
$this->assertSame('filename', $fileDocument->filename);
$this->assertSameDocument($metadata, $fileDocument->metadata);
}

/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidStreamValues
*/
public function testGetFileDocumentForStreamShouldRequireStreamResource($stream)
{
$this->bucket->getFileDocumentForStream($stream);
}

public function testGetFileIdForStreamWithReadableStream()
{
$id = $this->bucket->uploadFromStream('filename', $this->createStream('foobar'));
$stream = $this->bucket->openDownloadStream($id);

$this->assertEquals($id, $this->bucket->getIdFromStream($stream));
$this->assertEquals($id, $this->bucket->getFileIdForStream($stream));
}

public function testGetFileIdForStreamWithWritableStream()
{
$stream = $this->bucket->openUploadStream('filename', ['_id' => 1]);

$this->assertEquals(1, $this->bucket->getFileIdForStream($stream));
}

/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidStreamValues
*/
public function testGetFileIdForStreamShouldRequireStreamResource($stream)
{
$this->bucket->getFileIdForStream($stream);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/GridFS/WritableStreamFunctionalTest.php
Expand Up @@ -62,7 +62,7 @@ public function testInsertChunksCalculatesMD5($input, $expectedMD5)
$stream->close();

$fileDocument = $this->filesCollection->findOne(
['_id' => $stream->getId()],
['_id' => $stream->getFile()->_id],
['projection' => ['md5' => 1, '_id' => 0]]
);

Expand Down

0 comments on commit 0195906

Please sign in to comment.