Skip to content
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

[2.0] Add GridFS implementation on top of mongodb/mongodb #1790

Merged
merged 21 commits into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
1b14a44
Remove obsolete File type
alcaeus Mar 9, 2018
cafcf90
Add file marker to ClassMetadata
alcaeus Mar 23, 2018
0924145
Add annotations for GridFS files
alcaeus Mar 23, 2018
51dc2cf
Show off new file annotations in tests
alcaeus Mar 23, 2018
62c80e0
Add XML mapping for GridFS files
alcaeus Apr 20, 2018
3d4742e
Expose GridFS bucket classes in DocumentManager
alcaeus Apr 20, 2018
193ebec
Only allow special fields in GridFS mappings
alcaeus May 11, 2018
39d31c8
Add tests for GridFS file mappings
alcaeus May 11, 2018
fb19ba7
Update SchemaManager to handle GridFS collections
alcaeus May 12, 2018
f54dbd5
Handle persisting and updating GridFS files in UnitOfWork
alcaeus May 12, 2018
682aa79
Move DocumentRepository to Repository namespace
alcaeus May 12, 2018
119ff63
Introduce separate repository for GridFS
alcaeus May 13, 2018
b01b8f1
Ensure file deletion also deletes chunks
alcaeus May 13, 2018
4eecb1f
Bump dependency to doctrine/common to fix proxy generation with retur…
alcaeus May 13, 2018
c048992
Extract GridFS bucket name to separate property
alcaeus May 18, 2018
aca68a8
Add annotation reference for GridFS annotations
alcaeus Jun 8, 2018
2521ef7
Add usage documentation for GridFS
alcaeus Jun 8, 2018
49bd3c5
Consistently use inline code for identifiers and options
alcaeus Jun 15, 2018
6f44159
Add chunkSizeBytes option to GridFS file mapping
alcaeus Jul 24, 2018
c943ec4
Add openDownloadStream method to GridFSRepository
alcaeus Aug 1, 2018
f00a3c9
Replace GridFS option arguments with value object
alcaeus Aug 2, 2018
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
Prev Previous commit
Next Next commit
Add openDownloadStream method to GridFSRepository
  • Loading branch information
alcaeus committed Aug 1, 2018
commit c943ec411fff09ed7253f2037224dec6d7f5d6fa
18 changes: 18 additions & 0 deletions docs/en/reference/storing-files-with-mongogridfs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,21 @@ The ``downloadToStream`` method takes the identifier of a file as first argument
and a writable stream as the second arguments. If you need to manipulate the
file contents before writing it to disk or sending it to the client, consider
using a memory stream using the ``php://memory`` stream wrapper.
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to expose wrappers for openDownloadStream()?

Copy link
Member Author

Choose a reason for hiding this comment

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

We can certainly expose that wrapper as well, I haven't considered it until now


Alternatively, you can also use the ``openDownloadStream`` method which returns
a stream from where you can read file contents:

.. code-block:: php

<?php

$repository = $documentManager->getRepository(Documents\Image::class);
$file = $repository->uploadFromFile('image.jpg', '/tmp/path/to/image', new Documents\ImageMetadata('image/jpeg'));

$stream = $repository->openDownloadStream($file->getId());
try {
$contents = stream_get_contents($stream);
finally {
fclose($stream);
}

12 changes: 12 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Repository/DefaultGridFSRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@

class DefaultGridFSRepository extends DocumentRepository implements GridFSRepository
{
/**
* @see Bucket::openDownloadStream()
*/
public function openDownloadStream($id)
{
try {
return $this->getDocumentBucket()->openDownloadStream($this->class->getDatabaseIdentifierValue($id));
} catch (FileNotFoundException $e) {
throw DocumentNotFoundException::documentNotFound($this->getClassName(), $id);
}
}

/**
* @see Bucket::downloadToStream
*/
Expand Down
8 changes: 8 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Repository/GridFSRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

interface GridFSRepository extends ObjectRepository
{
/**
* Opens a readable stream for reading a GridFS file.
*
* @param mixed $id File ID
* @return resource
*/
public function openDownloadStream($id);

/**
* Writes the contents of a GridFS file to a writable stream.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ public function testUploadFromStreamStoresFile(): void
fclose($stream);
}

public function testOpenDownloadStreamAllowsReadingFile(): void
{
/** @var File $file */
$file = $this->getRepository()->uploadFromFile(__FILE__);
self::assertInstanceOf(File::class, $file);

$expectedSize = filesize(__FILE__);

$stream = $this->getRepository()->openDownloadStream($file->getId());

fseek($stream, 0);
$stat = fstat($stream);
self::assertSame($expectedSize, $stat['size']);
fclose($stream);
}

public function testUploadFromStreamPassesChunkSize(): void
{
$fileResource = fopen(__FILE__, 'r');
Expand Down