diff --git a/docs/en/reference/storing-files-with-mongogridfs.rst b/docs/en/reference/storing-files-with-mongogridfs.rst index b679c38065..17f2d0d80f 100644 --- a/docs/en/reference/storing-files-with-mongogridfs.rst +++ b/docs/en/reference/storing-files-with-mongogridfs.rst @@ -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 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 --------------------------------- @@ -224,8 +231,13 @@ a stream from where you can read file contents: 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 { diff --git a/lib/Doctrine/ODM/MongoDB/Repository/DefaultGridFSRepository.php b/lib/Doctrine/ODM/MongoDB/Repository/DefaultGridFSRepository.php index 82d541d0f9..49c1e880b5 100644 --- a/lib/Doctrine/ODM/MongoDB/Repository/DefaultGridFSRepository.php +++ b/lib/Doctrine/ODM/MongoDB/Repository/DefaultGridFSRepository.php @@ -42,9 +42,9 @@ 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); } @@ -52,9 +52,9 @@ public function openUploadStream(string $filename, $metadata = null, ?int $chunk /** * @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); @@ -62,7 +62,7 @@ public function uploadFromStream(string $filename, $source, $metadata = null, ?i 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) { @@ -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); } @@ -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; diff --git a/lib/Doctrine/ODM/MongoDB/Repository/GridFSRepository.php b/lib/Doctrine/ODM/MongoDB/Repository/GridFSRepository.php index dce44c7a9d..49b956604d 100644 --- a/lib/Doctrine/ODM/MongoDB/Repository/GridFSRepository.php +++ b/lib/Doctrine/ODM/MongoDB/Repository/GridFSRepository.php @@ -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. @@ -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. @@ -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); } diff --git a/lib/Doctrine/ODM/MongoDB/Repository/UploadOptions.php b/lib/Doctrine/ODM/MongoDB/Repository/UploadOptions.php new file mode 100644 index 0000000000..f99d43feb5 --- /dev/null +++ b/lib/Doctrine/ODM/MongoDB/Repository/UploadOptions.php @@ -0,0 +1,18 @@ +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'); @@ -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); } @@ -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); } @@ -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); @@ -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); }