|
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\Mapping\ClassMetadata; |
|
9
|
|
|
use Doctrine\ODM\MongoDB\MongoDBException; |
|
10
|
|
|
use MongoDB\GridFS\Bucket; |
|
11
|
|
|
use MongoDB\GridFS\Exception\FileNotFoundException; |
|
12
|
|
|
use const PATHINFO_BASENAME; |
|
13
|
|
|
use function fclose; |
|
14
|
|
|
use function fopen; |
|
15
|
|
|
use function pathinfo; |
|
16
|
|
|
|
|
17
|
|
|
class DefaultGridFSRepository extends DocumentRepository implements GridFSRepository |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @see Bucket::downloadToStream |
|
21
|
|
|
*/ |
|
22
|
2 |
|
public function downloadToStream($id, $destination): void |
|
23
|
|
|
{ |
|
24
|
|
|
try { |
|
25
|
2 |
|
$this->getDocumentBucket()->downloadToStream($this->class->getDatabaseIdentifierValue($id), $destination); |
|
26
|
|
|
} catch (FileNotFoundException $e) { |
|
27
|
|
|
throw DocumentNotFoundException::documentNotFound($this->getClassName(), $id); |
|
28
|
|
|
} |
|
29
|
2 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @see Bucket::openUploadStream |
|
33
|
|
|
*/ |
|
34
|
2 |
|
public function openUploadStream(string $filename, $metadata = null, ?int $chunkSizeBytes = null) |
|
35
|
|
|
{ |
|
36
|
2 |
|
$options = $this->prepareOptions($metadata, $chunkSizeBytes); |
|
37
|
|
|
|
|
38
|
2 |
|
return $this->getDocumentBucket()->openUploadStream($filename, $options); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @see Bucket::uploadFromStream |
|
43
|
|
|
*/ |
|
44
|
7 |
|
public function uploadFromStream(string $filename, $source, $metadata = null, ?int $chunkSizeBytes = null) |
|
45
|
|
|
{ |
|
46
|
7 |
|
$options = $this->prepareOptions($metadata, $chunkSizeBytes); |
|
47
|
|
|
|
|
48
|
7 |
|
$databaseIdentifier = $this->getDocumentBucket()->uploadFromStream($filename, $source, $options); |
|
49
|
7 |
|
$documentIdentifier = $this->class->getPHPIdentifierValue($databaseIdentifier); |
|
50
|
|
|
|
|
51
|
7 |
|
return $this->dm->getReference($this->getClassName(), $documentIdentifier); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
3 |
|
public function uploadFromFile(string $source, ?string $filename = null, $metadata = null, ?int $chunkSizeBytes = null) |
|
55
|
|
|
{ |
|
56
|
3 |
|
$resource = fopen($source, 'r'); |
|
57
|
3 |
|
if ($resource === false) { |
|
58
|
|
|
throw MongoDBException::cannotReadGridFSSourceFile($source); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
3 |
|
if ($filename === null) { |
|
62
|
2 |
|
$filename = pathinfo($source, PATHINFO_BASENAME); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
try { |
|
66
|
3 |
|
return $this->uploadFromStream($filename, $resource, $metadata, $chunkSizeBytes); |
|
67
|
|
|
} finally { |
|
68
|
3 |
|
fclose($resource); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
9 |
|
private function getDocumentBucket(): Bucket |
|
73
|
|
|
{ |
|
74
|
9 |
|
return $this->dm->getDocumentBucket($this->documentName); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param object|null $metadata |
|
79
|
|
|
*/ |
|
80
|
9 |
|
private function prepareOptions($metadata = null, ?int $chunkSizeBytes = null): array |
|
81
|
|
|
{ |
|
82
|
|
|
$options = [ |
|
83
|
9 |
|
'chunkSizeBytes' => $chunkSizeBytes ?: $this->class->getChunkSizeBytes(), |
|
84
|
|
|
]; |
|
85
|
|
|
|
|
86
|
9 |
|
if ($metadata) { |
|
87
|
3 |
|
$options += ['metadata' => (object) $this->uow->getPersistenceBuilder()->prepareInsertData($metadata)]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
9 |
|
return $options; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|