Skip to content

Commit

Permalink
Add openDownloadStream method to GridFSRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Aug 1, 2018
1 parent 6f44159 commit c943ec4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
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.

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

0 comments on commit c943ec4

Please sign in to comment.