Skip to content

PHPLIB-114: Implement GridFS specification #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
56023b0
PHPLIB-146: Implement GridFS upload, plus initial GridFS commit
Dec 7, 2015
623a98b
PHPLIB-147: Implement GridFS download
Dec 9, 2015
c787545
PHPLIB-148: Implement file deletion
Dec 10, 2015
2f76c46
PHPLIB-154: implement downloadByName
Dec 21, 2015
22e0480
PHPLIB-149: implement find on files collection
Dec 21, 2015
dacd44d
Response to PR comments to mongodb-php-library pull 57
Dec 28, 2015
8bcdab2
restructure some aspects to be more inline with SPEC and add addition…
Jan 5, 2016
d4fded2
additional tests: WIP
Jan 5, 2016
6cc4a58
added many many more tests, added getIdFromStream
Jan 6, 2016
1171293
Stream registering is now totally transparent to the user
Jan 6, 2016
60d5e88
added 'Abort' to clean up according to spec
Jan 6, 2016
21fb2c9
Rename function added to bucket
Jan 6, 2016
65fde2b
Straighten out abort method
Jan 7, 2016
ece9a00
Added corrupt chunk tests
Jan 7, 2016
a81bb85
removed all of the unused 'use' files
Jan 7, 2016
ead7cf1
uncommented argument check
Jan 7, 2016
e534225
BucketReadWriter and GridFsStream are obsolete
jmikola Jan 11, 2016
73a129e
Apply CS fixes and refactor GridFS classes
jmikola Jan 11, 2016
1706d0a
Replace public $id property with getter method
jmikola Jan 11, 2016
59e7ebd
Replace large GridFS fixture with generated temp file
jmikola Jan 11, 2016
8277ec3
Merge pull request #1 from jmikola/phplib-114-review
williambanfield Jan 12, 2016
4b947a3
PHPLIB-158: delete removes files document first
Jan 12, 2016
cb2a7ee
PHPLIB-159: rename uses UpdateOne() per the spec
Jan 12, 2016
87d08a6
PHPLIB-160: implement drop() on bucket
Jan 12, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Exception/GridFSCorruptFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace MongoDB\Exception;

class GridFSCorruptFileException extends \MongoDB\Driver\Exception\RuntimeException implements Exception
{
}
10 changes: 10 additions & 0 deletions src/Exception/GridFSFileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace MongoDB\Exception;

class GridFSFileNotFoundException extends \MongoDB\Driver\Exception\RuntimeException implements Exception
{
public function __construct($fname, $bucketName, $databaseName){
parent::__construct(sprintf('Unable to find file by: %s in %s.%s', $fname,$databaseName, $bucketName));
}
}
183 changes: 183 additions & 0 deletions src/GridFS/Bucket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php
namespace MongoDB\GridFS;

use MongoDB\Collection;
use MongoDB\Database;
use MongoDB\BSON\ObjectId;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Manager;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\InvalidArgumentTypeException;
use MongoDB\Exception\RuntimeException;
use MongoDB\Exception\UnexpectedValueException;
/**
* Bucket abstracts the GridFS files and chunks collections.
*
* @api
*/
class Bucket
{
private $databaseName;
private $options;
private $filesCollection;
private $chunksCollection;
private $ensuredIndexes = false;
/**
* Constructs a GridFS bucket.
*
* Supported options:
*
* * bucketName (string): The bucket name, which will be used as a prefix
* for the files and chunks collections. Defaults to "fs".
*
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to
* 261120 (i.e. 255 KiB).
*
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* @param Manager $manager Manager instance from the driver
* @param string $databaseName Database name
* @param array $options Bucket options
* @throws InvalidArgumentException
*/
public function __construct(Manager $manager, $databaseName, array $options = [])
{
$collectionOptions = [];
$options += [
'bucketName' => 'fs',
'chunkSizeBytes' => 261120,
];
if (isset($options['bucketName']) && ! is_string($options['bucketName'])) {
throw new InvalidArgumentTypeException('"bucketName" option', $options['bucketName'], 'string');
}
if (isset($options['chunkSizeBytes']) && ! is_integer($options['chunkSizeBytes'])) {
throw new InvalidArgumentTypeException('"chunkSizeBytes" option', $options['chunkSizeBytes'], 'integer');
}
if (isset($options['readPreference'])) {
if (! $options['readPreference'] instanceof ReadPreference) {
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
} else {
$collectionOptions['readPreference'] = $options['readPreference'];
}
}
if (isset($options['writeConcern'])) {
if (! $options['writeConcern'] instanceof WriteConcern) {
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
} else {
$collectionOptions['writeConcern'] = $options['writeConcern'];
}
}
$this->databaseName = (string) $databaseName;
$this->options = $options;

$this->filesCollection = new Collection(
$manager,
sprintf('%s.%s.files', $this->databaseName, $options['bucketName']),
$collectionOptions
);
$this->chunksCollection = new Collection(
$manager,
sprintf('%s.%s.chunks', $this->databaseName, $options['bucketName']),
$collectionOptions
);
}
/**
* Return the chunkSizeBytes option for this Bucket.
*
* @return integer
*/
public function getChunkSizeBytes()
{
return $this->options['chunkSizeBytes'];
}

public function getDatabaseName()
{
return $this->databaseName;
}
public function getFilesCollection()
{
return $this->filesCollection;
}

public function getChunksCollection()
{
return $this->chunksCollection;
}
public function getBucketName()
{
return $this->options['bucketName'];
}
public function getReadConcern()
{
if(isset($this->options['readPreference'])) {
return $this->options['readPreference'];
} else{
return null;
}
}
public function getWriteConcern()
{
if(isset($this->options['writeConcern'])) {
return $this->options['writeConcern'];
} else{
return null;
}
}

private function ensureIndexes()
{
if ($this->ensuredIndexes) {
return;
}
if ( ! $this->isFilesCollectionEmpty()) {
return;
}
$this->ensureFilesIndex();
$this->ensureChunksIndex();
$this->ensuredIndexes = true;
}
private function ensureChunksIndex()
{
foreach ($this->chunksCollection->listIndexes() as $index) {
if ($index->isUnique() && $index->getKey() === ['files_id' => 1, 'n' => 1]) {
return;
}
}
$this->chunksCollection->createIndex(['files_id' => 1, 'n' => 1], ['unique' => true]);
}
private function ensureFilesIndex()
{
foreach ($this->filesCollection->listIndexes() as $index) {
if ($index->getKey() === ['filename' => 1, 'uploadDate' => 1]) {
return;
}
}
$this->filesCollection->createIndex(['filename' => 1, 'uploadDate' => 1]);
}
private function isFilesCollectionEmpty()
{
return null === $this->filesCollection->findOne([], [
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY),
'projection' => ['_id' => 1],
]);
}
public function findFileRevision($filename, $revision)
{
if ($revision < 0) {
$skip = abs($revision) -1;
$sortOrder = -1;
} else {
$skip = $revision;
$sortOrder = 1;
}
$file = $this->filesCollection->findOne(["filename"=> $filename], ["sort" => ["uploadDate"=> $sortOrder], "limit"=>1, "skip" => $skip]);
if(is_null($file)) {
throw new \MongoDB\Exception\GridFSFileNotFoundException($filename, $this->getBucketName(), $this->getDatabaseName());;
}
return $file;
}
}
125 changes: 125 additions & 0 deletions src/GridFS/BucketReadWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
namespace MongoDB\GridFS;

class BucketReadWriter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's fold this class and its methods into the Bucket class, since that's where the spec expects to find these methods.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class still exists, but is no longer called from anywhere. Just keeping it around until we're sure we're done with it.

{
private $bucket;

public function __construct(Bucket $bucket)
{
$this->bucket = $bucket;
}
/**
* Opens a Stream for writing the contents of a file.
*
* @param string $filename file to upload
* @param array $options Stream Options
* @return Stream uploadStream
*/
public function openUploadStream($filename, array $options = [])
{
$options = [
'bucket' => $this->bucket,
'uploadOptions' => $options
];
$context = stream_context_create(['gridfs' => $options]);
return fopen(sprintf('gridfs://%s/%s', $this->bucket->getDatabaseName(), $filename), 'w', false, $context);
}
/**
* Upload a file to this bucket by specifying the source stream file
*
* @param String $filename Filename To Insert
* @param Stream $source Source Stream
* @param array $options Stream Options
* @return ObjectId
*/
public function uploadFromStream($filename, $source, array $options = [])
{
$gridFsStream = new GridFsUpload($this->bucket, $filename, $options);
return $gridFsStream->uploadFromStream($source);
}
/**
* Opens a Stream for reading the contents of a file specified by ID.
*
* @param ObjectId $id
* @return Stream
*/
public function openDownloadStream(\MongoDB\BSON\ObjectId $id)
{
$options = [
'bucket' => $this->bucket
];
$context = stream_context_create(['gridfs' => $options]);
return fopen(sprintf('gridfs://%s/%s', $this->bucket->getDatabaseName(), $id), 'r', false, $context);
}
/**
* Downloads the contents of the stored file specified by id and writes
* the contents to the destination Stream.
* @param ObjectId $id GridFS File Id
* @param Stream $destination Destination Stream
*/
public function downloadToStream(\MongoDB\BSON\ObjectId $id, $destination)
{
$gridFsStream = new GridFsDownload($this->bucket, $id);
$gridFsStream->downloadToStream($destination);
}
/**
* Delete a file from the GridFS bucket. If the file collection entry is not found, still attempts to delete orphaned chunks
*
* @param ObjectId $id file id
* @throws GridFSFileNotFoundException
*/
public function delete(\MongoDB\BSON\ObjectId $id)
{
$options =[];
$writeConcern = $this->bucket->getWriteConcern();
if(!is_null($writeConcern)) {
$options['writeConcern'] = $writeConcern;
}
$file = $this->bucket->getFilesCollection()->findOne(['_id' => $id]);
$this->bucket->getChunksCollection()->deleteMany(['files_id' => $id], $options);
if (is_null($file)) {
throw new \MongoDB\Exception\GridFSFileNotFoundException($id, $this->bucket->getDatabaseName(), $this->bucket->getBucketName());
}

$this->bucket->getFilesCollection()->deleteOne(['_id' => $id], $options);
}
/**
* Open a stream to download a file from the GridFS bucket. Searches for the file by the specified name then returns a stream to the specified file
* @param string $filename name of the file to download
* @param int $revision the revision of the file to download
* @throws GridFSFileNotFoundException
*/
public function openDownloadStreamByName($filename, $revision = -1)
{
$file = $this->bucket->findFileRevision($filename, $revision);
$options = ['bucket' => $this->bucket,
'file' => $file
];
$context = stream_context_create(['gridfs' => $options]);
return fopen(sprintf('gridfs://%s/%s', $this->bucket->getDatabaseName(), $filename), 'r', false, $context);
}
/**
* Download a file from the GridFS bucket by name. Searches for the file by the specified name then loads data into the stream
*
* @param string $filename name of the file to download
* @param int $revision the revision of the file to download
* @throws GridFSFileNotFoundException
*/
public function downloadToStreamByName($filename, $destination, $revision=-1)
{
$file = $this->bucket->findFileRevision($filename, $revision);
$gridFsStream = new GridFsDownload($this->bucket, null, $file);
$gridFsStream->downloadToStream($destination);
}
/**
* Find files from the GridFS bucket files collection.
*
* @param array $filter filter to find by
* @param array $options options to
*/
public function find($filter, array $options =[])
{
return $this->bucket->getFilesCollection()->find($filter, $options);
}
}
Loading