Skip to content

Commit

Permalink
Merge pull request #535 from bsperduto/gridfs-metadata-retrieve
Browse files Browse the repository at this point in the history
GridFS Support for Metadata Retrieval after Write
  • Loading branch information
nicolasmure authored Jan 4, 2018
2 parents 21cda6d + 6364659 commit 42cf3a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Gaufrette/Adapter/GridFS.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,16 @@ public function setMetadata($key, $metadata)
*/
public function getMetadata($key)
{
return isset($this->metadata[$key]) ? $this->metadata[$key] : array();
if (isset($this->metadata[$key])) {
return $this->metadata[$key];
} else {
$meta = $this->bucket->findOne(['filename' => $key], ['projection' => ['metadata' => 1,'_id' => 0]]);
if ($meta === null) {
return array();
}
$this->metadata[$key] = iterator_to_array($meta['metadata']);
return $this->metadata[$key];
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Gaufrette/Functional/Adapter/GridFSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,19 @@ public function shouldListKeys()
$keys['keys'],
'', 0, 10, true);
}

/**
* @test
* Tests metadata written to GridFS can be retrieved after writing
*/
public function testMetadataRetrieveAfterWrite()
{
//Create local copy of fileadapter
$fileadpt = clone $this->filesystem->getAdapter();

$this->filesystem->getAdapter()->setMetadata('metadatatest', array('testing' => true));
$this->filesystem->write('metadatatest', 'test');

$this->assertEquals($this->filesystem->getAdapter()->getMetadata('metadatatest'), $fileadpt->getMetadata('metadatatest'));
}
}

0 comments on commit 42cf3a0

Please sign in to comment.