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
|
1 |
|
public function downloadToStream($id, $destination): void |
19
|
|
|
{ |
20
|
|
|
try { |
21
|
1 |
|
$this->getDocumentBucket()->downloadToStream($this->class->getDatabaseIdentifierValue($id), $destination); |
22
|
|
|
} catch (FileNotFoundException $e) { |
23
|
|
|
throw DocumentNotFoundException::documentNotFound($this->getClassName(), $id); |
24
|
|
|
} |
25
|
1 |
|
} |
26
|
|
|
|
27
|
1 |
|
public function openUploadStream(string $filename, $metadata = null) |
28
|
|
|
{ |
29
|
1 |
|
$options = $this->prepareMetadataOptions($metadata); |
30
|
|
|
|
31
|
1 |
|
return $this->getDocumentBucket()->openUploadStream($filename, $options); |
32
|
|
|
} |
33
|
|
|
|
34
|
6 |
|
public function uploadFromStream($filename, $source, $metadata = null) |
35
|
|
|
{ |
36
|
6 |
|
$options = $this->prepareMetadataOptions($metadata); |
37
|
|
|
|
38
|
6 |
|
$id = $this->getDocumentBucket()->uploadFromStream($filename, $source, $options); |
39
|
|
|
|
40
|
|
|
// TODO: apply primary read preference |
41
|
|
|
|
42
|
6 |
|
return $this->find($id); |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
public function uploadFromFile(string $source, ?string $filename = null, $metadata = null) |
46
|
|
|
{ |
47
|
3 |
|
$resource = fopen($source, 'r'); |
48
|
3 |
|
if (! $resource) { |
49
|
|
|
throw MongoDBException::cannotReadGridFSSourceFile($source); |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
if (! $filename) { |
|
|
|
|
53
|
2 |
|
$filename = pathinfo($source, PATHINFO_BASENAME); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
try { |
57
|
3 |
|
return $this->uploadFromStream($filename, $resource, $metadata); |
58
|
|
|
} finally { |
59
|
3 |
|
fclose($resource); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
7 |
|
private function getDocumentBucket(): Bucket |
64
|
|
|
{ |
65
|
7 |
|
return $this->dm->getDocumentBucket($this->documentName); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param object|null $metadata |
70
|
|
|
*/ |
71
|
7 |
|
private function prepareMetadataOptions($metadata = null): array |
72
|
|
|
{ |
73
|
7 |
|
if ($metadata === null) { |
74
|
5 |
|
return []; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
return ['metadata' => (object) $this->uow->getPersistenceBuilder()->prepareInsertData($metadata)]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: