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
|
|
|
/** |
19
|
|
|
* @see Bucket::downloadToStream |
20
|
|
|
*/ |
21
|
1 |
|
public function downloadToStream($id, $destination): void |
22
|
|
|
{ |
23
|
|
|
try { |
24
|
1 |
|
$this->getDocumentBucket()->downloadToStream($this->class->getDatabaseIdentifierValue($id), $destination); |
25
|
|
|
} catch (FileNotFoundException $e) { |
26
|
|
|
throw DocumentNotFoundException::documentNotFound($this->getClassName(), $id); |
27
|
|
|
} |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @see Bucket::openUploadStream |
32
|
|
|
*/ |
33
|
1 |
|
public function openUploadStream(string $filename, $metadata = null) |
34
|
|
|
{ |
35
|
1 |
|
$options = $this->prepareMetadataOptions($metadata); |
36
|
|
|
|
37
|
1 |
|
return $this->getDocumentBucket()->openUploadStream($filename, $options); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @see Bucket::uploadFromStream |
42
|
|
|
*/ |
43
|
6 |
|
public function uploadFromStream(string $filename, $source, $metadata = null) |
44
|
|
|
{ |
45
|
6 |
|
$options = $this->prepareMetadataOptions($metadata); |
46
|
|
|
|
47
|
6 |
|
$databaseIdentifier = $this->getDocumentBucket()->uploadFromStream($filename, $source, $options); |
48
|
6 |
|
$documentIdentifier = $this->class->getPHPIdentifierValue($databaseIdentifier); |
49
|
|
|
|
50
|
6 |
|
return $this->dm->getReference($this->getClassName(), $documentIdentifier); |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
public function uploadFromFile(string $source, ?string $filename = null, $metadata = null) |
54
|
|
|
{ |
55
|
3 |
|
$resource = fopen($source, 'r'); |
56
|
3 |
|
if ($resource === false) { |
57
|
|
|
throw MongoDBException::cannotReadGridFSSourceFile($source); |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
if ($filename === null) { |
61
|
2 |
|
$filename = pathinfo($source, PATHINFO_BASENAME); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
try { |
65
|
3 |
|
return $this->uploadFromStream($filename, $resource, $metadata); |
66
|
|
|
} finally { |
67
|
3 |
|
fclose($resource); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
7 |
|
private function getDocumentBucket(): Bucket |
72
|
|
|
{ |
73
|
7 |
|
return $this->dm->getDocumentBucket($this->documentName); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param object|null $metadata |
78
|
|
|
*/ |
79
|
7 |
|
private function prepareMetadataOptions($metadata = null): array |
80
|
|
|
{ |
81
|
7 |
|
if ($metadata === null) { |
82
|
5 |
|
return []; |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
return ['metadata' => (object) $this->uow->getPersistenceBuilder()->prepareInsertData($metadata)]; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|