1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ODM\MongoDB\Repository; |
6
|
|
|
|
7
|
|
|
use Doctrine\ODM\MongoDB\DocumentNotFoundException; |
8
|
|
|
use Doctrine\ODM\MongoDB\MongoDBException; |
9
|
|
|
use MongoDB\GridFS\Bucket; |
10
|
|
|
use MongoDB\GridFS\Exception\FileNotFoundException; |
11
|
|
|
use const PATHINFO_BASENAME; |
12
|
|
|
use function fclose; |
13
|
|
|
use function fopen; |
14
|
|
|
use function pathinfo; |
15
|
|
|
|
16
|
|
|
class DefaultGridFSRepository extends DocumentRepository implements GridFSRepository |
17
|
|
|
{ |
18
|
|
|
public function downloadToStream($id, $destination): void |
19
|
|
|
{ |
20
|
|
|
try { |
21
|
|
|
$this->getDocumentBucket()->downloadToStream($this->class->getDatabaseIdentifierValue($id), $destination); |
22
|
|
|
} catch (FileNotFoundException $e) { |
|
|
|
|
23
|
|
|
throw DocumentNotFoundException::documentNotFound($this->getClassName(), $id); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function openUploadStream(string $filename, $metadata = null) |
28
|
|
|
{ |
29
|
|
|
$options = $this->prepareMetadataOptions($metadata); |
30
|
|
|
|
31
|
|
|
return $this->getDocumentBucket()->openUploadStream($filename, $options); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function uploadFromStream($filename, $source, $metadata = null) |
35
|
|
|
{ |
36
|
|
|
$options = $this->prepareMetadataOptions($metadata); |
37
|
|
|
|
38
|
|
|
$id = $this->getDocumentBucket()->uploadFromStream($filename, $source, $options); |
39
|
|
|
|
40
|
|
|
// TODO: apply primary read preference |
41
|
|
|
|
42
|
|
|
return $this->find($id); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function uploadFromFile(string $source, ?string $filename = null, $metadata = null) |
46
|
|
|
{ |
47
|
|
|
$resource = fopen($source, 'r'); |
48
|
|
|
if (! $resource) { |
49
|
|
|
throw MongoDBException::cannotReadGridFSSourceFile($source); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (! $filename) { |
|
|
|
|
53
|
|
|
$filename = pathinfo($source, PATHINFO_BASENAME); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
try { |
57
|
|
|
return $this->uploadFromStream($filename, $resource, $metadata); |
58
|
|
|
} finally { |
59
|
|
|
fclose($resource); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function getDocumentBucket(): Bucket |
64
|
|
|
{ |
65
|
|
|
return $this->dm->getDocumentBucket($this->documentName); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param object|null $metadata |
70
|
|
|
*/ |
71
|
|
|
private function prepareMetadataOptions($metadata = null): array |
72
|
|
|
{ |
73
|
|
|
if ($metadata === null) { |
74
|
|
|
return []; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return ['metadata' => (object) $this->uow->getPersistenceBuilder()->prepareInsertData($metadata)]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.