From c943ec411fff09ed7253f2037224dec6d7f5d6fa Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 1 Aug 2018 18:10:29 +0200 Subject: [PATCH] Add openDownloadStream method to GridFSRepository --- .../storing-files-with-mongogridfs.rst | 18 ++++++++++++++++++ .../Repository/DefaultGridFSRepository.php | 12 ++++++++++++ .../MongoDB/Repository/GridFSRepository.php | 8 ++++++++ .../Repository/DefaultGridFSRepositoryTest.php | 16 ++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/docs/en/reference/storing-files-with-mongogridfs.rst b/docs/en/reference/storing-files-with-mongogridfs.rst index d1ae09124a..b679c38065 100644 --- a/docs/en/reference/storing-files-with-mongogridfs.rst +++ b/docs/en/reference/storing-files-with-mongogridfs.rst @@ -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 + + 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); + } + diff --git a/lib/Doctrine/ODM/MongoDB/Repository/DefaultGridFSRepository.php b/lib/Doctrine/ODM/MongoDB/Repository/DefaultGridFSRepository.php index a0a62d35af..82d541d0f9 100644 --- a/lib/Doctrine/ODM/MongoDB/Repository/DefaultGridFSRepository.php +++ b/lib/Doctrine/ODM/MongoDB/Repository/DefaultGridFSRepository.php @@ -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 */ diff --git a/lib/Doctrine/ODM/MongoDB/Repository/GridFSRepository.php b/lib/Doctrine/ODM/MongoDB/Repository/GridFSRepository.php index 1f1f0efe3a..dce44c7a9d 100644 --- a/lib/Doctrine/ODM/MongoDB/Repository/GridFSRepository.php +++ b/lib/Doctrine/ODM/MongoDB/Repository/GridFSRepository.php @@ -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. * diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Repository/DefaultGridFSRepositoryTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Repository/DefaultGridFSRepositoryTest.php index 47cf0972b8..146509d5d4 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Repository/DefaultGridFSRepositoryTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Repository/DefaultGridFSRepositoryTest.php @@ -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');