Commit 9709007b authored by Will Banfield's avatar Will Banfield Committed by Jeremy Mikola

Rename function added to bucket

parent f418ac21
...@@ -163,7 +163,11 @@ class Bucket ...@@ -163,7 +163,11 @@ class Bucket
{ {
return $this->collectionsWrapper->getFilesCollection()->find($filter, $options); return $this->collectionsWrapper->getFilesCollection()->find($filter, $options);
} }
/**
* Gets the id of the GridFs file associated with $stream
*
* @param resource $stream wrapped gridFsStream
*/
public function getIdFromStream($stream) public function getIdFromStream($stream)
{ {
$metadata = stream_get_meta_data($stream); $metadata = stream_get_meta_data($stream);
...@@ -172,7 +176,23 @@ class Bucket ...@@ -172,7 +176,23 @@ class Bucket
} }
return null; return null;
} }
/**
* Gets the id of the GridFs file associated with $stream
*
* @param \MongoDB\BSON\ObjectId $id id of the file to rename
* @param string $newFilename new name for the file
* @throws \MongoDB\Exception\GridFSFileNotFoundException
*/
public function rename(\MongoDB\BSON\ObjectId $id, $newFilename)
{
$filesCollection = $this->collectionsWrapper->getFilesCollection();
$file = $filesCollection->findOne(["_id" => $id]);
if (is_null($file)) {
throw new \MongoDB\Exception\GridFSFileNotFoundException($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace());
}
$file->filename = $newFilename;
$filesCollection->replaceOne(["_id"=> $id], $file);
}
public function getCollectionsWrapper() public function getCollectionsWrapper()
{ {
return $this->collectionsWrapper; return $this->collectionsWrapper;
......
...@@ -220,6 +220,24 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -220,6 +220,24 @@ class BucketFunctionalTest extends FunctionalTestCase
fclose($download); fclose($download);
$this->assertTrue($id instanceof \MongoDB\BSON\ObjectId); $this->assertTrue($id instanceof \MongoDB\BSON\ObjectId);
} }
public function testRename()
{
$id = $this->bucket->uploadFromStream("first_name", $this->generateStream("testing"));
$this->assertEquals("testing", stream_get_contents($this->bucket->openDownloadStream($id)));
$this->bucket->rename($id, "second_name");
$error = null;
try{
$this->bucket->openDownloadStreamByName("first_name");
} catch(\MongoDB\Exception\Exception $e) {
$error = $e;
}
$fileNotFound = '\MongoDB\Exception\GridFSFileNotFoundException';
$this->assertTrue($error instanceof $fileNotFound);
$this->assertEquals("testing", stream_get_contents($this->bucket->openDownloadStreamByName("second_name")));
}
/** /**
*@dataProvider provideInsertChunks *@dataProvider provideInsertChunks
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment