Skip to content

Commit

Permalink
Replace GridFS option arguments with value object
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Aug 2, 2018
1 parent c943ec4 commit 3b04df8
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 28 deletions.
20 changes: 16 additions & 4 deletions docs/en/reference/storing-files-with-mongogridfs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,23 @@ When using the default GridFS repository implementation, the ``uploadFromFile``
and ``uploadFromStream`` methods return a proxy object of the file you just
uploaded.

If you want to pass options, such as a metadata object to the uploaded file, you
If you want to add metadata to the uploaded file, you can pass it as the last
argument to the ``uploadFromFile``, ``uploadFromStream`` or ``openUploadStream``
method call:
can pass an ``UploadOptions`` object as the last argument to the
``uploadFromFile``, ``uploadFromStream`` or ``openUploadStream`` method call:

.. code-block:: php
<?php
use Doctrine\ODM\MongoDB\Repository\UploadOptions;
$uploadOptions = new UploadOptions();
$uploadOptions->metadata = new Documents\ImageMetadata('image/jpeg');
$uploadOptions->chunkSizeBytes = 1024 * 1024;
$repository = $documentManager->getRepository(Documents\Image::class);
$file = $repository->uploadFromFile('image.jpg', '/tmp/path/to/image', new Documents\ImageMetadata('image/jpeg'));
$file = $repository->uploadFromFile('image.jpg', '/tmp/path/to/image', $uploadOptions);
Reading files from GridFS buckets
---------------------------------
Expand Down Expand Up @@ -224,8 +231,13 @@ a stream from where you can read file contents:
<?php
use Doctrine\ODM\MongoDB\Repository\UploadOptions;
$uploadOptions = new UploadOptions();
$uploadOptions->metadata = new Documents\ImageMetadata('image/jpeg');
$repository = $documentManager->getRepository(Documents\Image::class);
$file = $repository->uploadFromFile('image.jpg', '/tmp/path/to/image', new Documents\ImageMetadata('image/jpeg'));
$file = $repository->uploadFromFile('image.jpg', '/tmp/path/to/image', $uploadOptions);
$stream = $repository->openDownloadStream($file->getId());
try {
Expand Down
27 changes: 14 additions & 13 deletions lib/Doctrine/ODM/MongoDB/Repository/DefaultGridFSRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,27 @@ public function downloadToStream($id, $destination): void
/**
* @see Bucket::openUploadStream
*/
public function openUploadStream(string $filename, $metadata = null, ?int $chunkSizeBytes = null)
public function openUploadStream(string $filename, ?UploadOptions $uploadOptions = null)
{
$options = $this->prepareOptions($metadata, $chunkSizeBytes);
$options = $this->prepareOptions($uploadOptions);

return $this->getDocumentBucket()->openUploadStream($filename, $options);
}

/**
* @see Bucket::uploadFromStream
*/
public function uploadFromStream(string $filename, $source, $metadata = null, ?int $chunkSizeBytes = null)
public function uploadFromStream(string $filename, $source, ?UploadOptions $uploadOptions = null)
{
$options = $this->prepareOptions($metadata, $chunkSizeBytes);
$options = $this->prepareOptions($uploadOptions);

$databaseIdentifier = $this->getDocumentBucket()->uploadFromStream($filename, $source, $options);
$documentIdentifier = $this->class->getPHPIdentifierValue($databaseIdentifier);

return $this->dm->getReference($this->getClassName(), $documentIdentifier);
}

public function uploadFromFile(string $source, ?string $filename = null, $metadata = null, ?int $chunkSizeBytes = null)
public function uploadFromFile(string $source, ?string $filename = null, ?UploadOptions $uploadOptions = null)
{
$resource = fopen($source, 'r');
if ($resource === false) {
Expand All @@ -74,7 +74,7 @@ public function uploadFromFile(string $source, ?string $filename = null, $metada
}

try {
return $this->uploadFromStream($filename, $resource, $metadata, $chunkSizeBytes);
return $this->uploadFromStream($filename, $resource, $uploadOptions);
} finally {
fclose($resource);
}
Expand All @@ -85,17 +85,18 @@ private function getDocumentBucket(): Bucket
return $this->dm->getDocumentBucket($this->documentName);
}

/**
* @param object|null $metadata
*/
private function prepareOptions($metadata = null, ?int $chunkSizeBytes = null): array
private function prepareOptions(?UploadOptions $uploadOptions = null): array
{
if ($uploadOptions === null) {
$uploadOptions = new UploadOptions();
}

$options = [
'chunkSizeBytes' => $chunkSizeBytes ?: $this->class->getChunkSizeBytes(),
'chunkSizeBytes' => $uploadOptions->chunkSizeBytes ?: $this->class->getChunkSizeBytes(),
];

if ($metadata) {
$options += ['metadata' => (object) $this->uow->getPersistenceBuilder()->prepareInsertData($metadata)];
if (is_object($uploadOptions->metadata)) {
$options += ['metadata' => (object) $this->uow->getPersistenceBuilder()->prepareInsertData($uploadOptions->metadata)];
}

return $options;
Expand Down
7 changes: 3 additions & 4 deletions lib/Doctrine/ODM/MongoDB/Repository/GridFSRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ public function downloadToStream($id, $destination): void;
/**
* Opens a writable stream for writing a GridFS file.
*
* @param object|null $metadata
* @return resource
*/
public function openUploadStream(string $filename, $metadata = null, ?int $chunkSizeBytes = null);
public function openUploadStream(string $filename, ?UploadOptions $uploadOptions = null);

/**
* Writes the contents of a readable stream to a GridFS file.
Expand All @@ -39,7 +38,7 @@ public function openUploadStream(string $filename, $metadata = null, ?int $chunk
* @param object|null $metadata
* @return object The newly created GridFS file
*/
public function uploadFromStream(string $filename, $source, $metadata = null, ?int $chunkSizeBytes = null);
public function uploadFromStream(string $filename, $source, ?UploadOptions $uploadOptions = null);

/**
* Writes the contents of a file to a GridFS file.
Expand All @@ -49,5 +48,5 @@ public function uploadFromStream(string $filename, $source, $metadata = null, ?i
* @param object|null $metadata
* @return object The newly created GridFS file
*/
public function uploadFromFile(string $source, ?string $filename = null, $metadata = null, ?int $chunkSizeBytes = null);
public function uploadFromFile(string $source, ?string $filename = null, ?UploadOptions $uploadOptions = null);
}
18 changes: 18 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Repository/UploadOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types = 1);

namespace Doctrine\ODM\MongoDB\Repository;

final class UploadOptions
{
/**
* @var object|null
*/
public $metadata;

/**
* @var int|null
*/
public $chunkSizeBytes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Tests\Repos;

use Doctrine\ODM\MongoDB\Repository\GridFSRepository;
use Doctrine\ODM\MongoDB\Repository\UploadOptions;
use Doctrine\ODM\MongoDB\Tests\BaseTest;
use Documents\File;
use Documents\FileMetadata;
Expand Down Expand Up @@ -40,7 +41,10 @@ public function testOpenUploadStreamReturnsWritableResource(): void

public function testOpenUploadStreamUsesChunkSizeFromOptions(): void
{
$uploadStream = $this->getRepository()->openUploadStream('somefile.txt', null, 1234);
$uploadOptions = new UploadOptions();
$uploadOptions->chunkSizeBytes = 1234;

$uploadStream = $this->getRepository()->openUploadStream('somefile.txt', $uploadOptions);
self::assertInternalType('resource', $uploadStream);

fwrite($uploadStream, 'contents');
Expand All @@ -59,11 +63,14 @@ public function testOpenUploadStreamUsesChunkSizeFromOptions(): void

public function testUploadFromStreamStoresFile(): void
{
$uploadOptions = new UploadOptions();
$uploadOptions->metadata = new FileMetadata();

$fileResource = fopen(__FILE__, 'r');

try {
/** @var File $file */
$file = $this->getRepository()->uploadFromStream('somefile.txt', $fileResource, new FileMetadata());
$file = $this->getRepository()->uploadFromStream('somefile.txt', $fileResource, $uploadOptions);
} finally {
fclose($fileResource);
}
Expand Down Expand Up @@ -108,11 +115,15 @@ public function testOpenDownloadStreamAllowsReadingFile(): void

public function testUploadFromStreamPassesChunkSize(): void
{
$uploadOptions = new UploadOptions();
$uploadOptions->metadata = new FileMetadata();
$uploadOptions->chunkSizeBytes = 1234;

$fileResource = fopen(__FILE__, 'r');

try {
/** @var File $file */
$file = $this->getRepository()->uploadFromStream('somefile.txt', $fileResource, new FileMetadata(), 1234);
$file = $this->getRepository()->uploadFromStream('somefile.txt', $fileResource, $uploadOptions);
} finally {
fclose($fileResource);
}
Expand Down Expand Up @@ -156,15 +167,21 @@ public function testUploadFromFileWithoutFilenamePicksAFilename(): void

public function testUploadFromFileUsesProvidedFilename(): void
{
$uploadOptions = new UploadOptions();
$uploadOptions->chunkSizeBytes = 1234;

/** @var File $file */
$file = $this->getRepository()->uploadFromFile(__FILE__, 'test.php', null, 1234);
$file = $this->getRepository()->uploadFromFile(__FILE__, 'test.php', $uploadOptions);
self::assertSame('test.php', $file->getFilename());
self::assertSame(1234, $file->getChunkSize());
}

public function testReadingFileAllowsUpdatingMetadata(): void
{
$file = $this->uploadFile(__FILE__, new FileMetadata());
$uploadOptions = new UploadOptions();
$uploadOptions->metadata = new FileMetadata();

$file = $this->uploadFile(__FILE__, $uploadOptions);
$file->getMetadata()->setOwner(new User());

$this->dm->persist($file);
Expand Down Expand Up @@ -196,13 +213,13 @@ private function getRepository(): GridFSRepository
return $this->dm->getRepository(File::class);
}

private function uploadFile($filename, $metadata = null): File
private function uploadFile($filename, ?UploadOptions $uploadOptions = null): File
{
$fileResource = fopen($filename, 'r');

try {
/** @var File $file */
$file = $this->getRepository()->uploadFromStream('somefile.txt', $fileResource, $metadata);
$file = $this->getRepository()->uploadFromStream('somefile.txt', $fileResource, $uploadOptions);
} finally {
fclose($fileResource);
}
Expand Down

0 comments on commit 3b04df8

Please sign in to comment.