1 | <?php |
||
33 | class MongoFileService implements \Minotaur\Io\FileService |
||
34 | { |
||
35 | use MongoHelper; |
||
36 | |||
37 | /** |
||
38 | * @var \MongoDB\GridFS\Bucket |
||
39 | */ |
||
40 | private $bucket; |
||
41 | |||
42 | /** |
||
43 | * Creates a new MongoFileService |
||
44 | * |
||
45 | * @param $bucket - The GridFS Bucket |
||
46 | */ |
||
47 | 2 | public function __construct(Bucket $bucket) |
|
51 | |||
52 | /** |
||
53 | * Stores an uploaded file. |
||
54 | * |
||
55 | * You should specify `contentType` in the `metadata` Map. |
||
56 | * |
||
57 | * @param \Psr\Http\Message\UploadedFileInterface $file The uploaded file |
||
58 | * @param array<string,mixed> $metadata Any additional fields to persist. At the very least, try to supply `contentType`. |
||
59 | * @return ObjectID The document ID of the stored file |
||
60 | * @throws \Caridea\Dao\Exception\Unreachable If the connection fails |
||
61 | * @throws \Caridea\Dao\Exception\Violating If a constraint is violated |
||
62 | * @throws \Caridea\Dao\Exception\Generic If any other database problem occurs |
||
63 | */ |
||
64 | 1 | public function store(UploadedFileInterface $file, array $metadata): ObjectID |
|
65 | { |
||
66 | $meta = [ |
||
67 | 1 | "contentType" => $metadata['contentType'] ?? $file->getClientMediaType(), |
|
68 | 1 | 'metadata' => $metadata |
|
69 | ]; |
||
70 | 1 | return $this->bucket->uploadFromStream( |
|
71 | 1 | $file->getClientFilename(), |
|
72 | 1 | $file->getStream()->detach(), |
|
73 | 1 | $meta |
|
74 | ); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Gets the file as a PSR-7 Stream. |
||
79 | * |
||
80 | * @param $id - The document identifier, either a string or `ObjectID` |
||
81 | * @return \Psr\Http\Message\StreamInterface The readable stream |
||
82 | * @throws \Caridea\Dao\Exception\Unreachable If the connection fails |
||
83 | * @throws \Caridea\Dao\Exception\Unretrievable If the document doesn't exist |
||
84 | * @throws \Caridea\Dao\Exception\Generic If any other database problem occurs |
||
85 | */ |
||
86 | public function messageStream($id): StreamInterface |
||
94 | |||
95 | /** |
||
96 | * Gets a readable stream resource for the given ID. |
||
97 | * |
||
98 | * @param $id - The document identifier, either a string or `ObjectID` |
||
99 | * @return resource The readable stream |
||
100 | * @throws \Caridea\Dao\Exception\Unreachable If the connection fails |
||
101 | * @throws \Caridea\Dao\Exception\Unretrievable If the document doesn't exist |
||
102 | * @throws \Caridea\Dao\Exception\Generic If any other database problem occurs |
||
103 | */ |
||
104 | public function resource($id) |
||
108 | |||
109 | /** |
||
110 | * Efficiently writes the contents of a file to a Stream. |
||
111 | * |
||
112 | * @param \stdClass $file The file |
||
113 | * @param \Psr\Http\Message\StreamInterface $stream The stream |
||
114 | * @throws \Caridea\Dao\Exception\Unreachable If the connection fails |
||
115 | * @throws \Caridea\Dao\Exception\Violating If a constraint is violated |
||
116 | * @throws \Caridea\Dao\Exception\Generic If any other database problem occurs |
||
117 | */ |
||
118 | public function stream($file, StreamInterface $stream): void |
||
128 | |||
129 | /** |
||
130 | * Gets a stored file. |
||
131 | * |
||
132 | * @param mixed $id The document identifier, either a string or `ObjectID` |
||
133 | * @return \stdClass|null The stored file, or `null` |
||
134 | * @throws \Caridea\Dao\Exception\Unreachable If the connection fails |
||
135 | * @throws \Caridea\Dao\Exception\Unretrievable If the result cannot be retrieved |
||
136 | * @throws \Caridea\Dao\Exception\Generic If any other database problem occurs |
||
137 | */ |
||
138 | 1 | public function read($id): ?\stdClass |
|
139 | { |
||
140 | 1 | $mid = $this->toId($id); |
|
141 | 1 | return $this->doExecute(function (Bucket $bucket) use ($mid) { |
|
142 | 1 | return $this->getCollectionWrapper($bucket)->findFileById($mid); |
|
143 | 1 | }); |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * Deletes a stored file. |
||
148 | * |
||
149 | * @param mixed $id The document identifier, either a string or `ObjectID` |
||
150 | * @throws \Caridea\Dao\Exception\Unreachable If the connection fails |
||
151 | * @throws \Caridea\Dao\Exception\Unretrievable If the document doesn't exist |
||
152 | * @throws \Caridea\Dao\Exception\Generic If any other database problem occurs |
||
153 | */ |
||
154 | public function delete($id): void |
||
155 | { |
||
156 | $mid = $this->toId($id); |
||
157 | $this->doExecute(function (Bucket $bucket) use ($mid) { |
||
158 | $bucket->delete($mid); |
||
159 | }); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Finds several files by some arbitrary criteria. |
||
164 | * |
||
165 | * @param array<string,mixed> $criteria Field to value pairs |
||
166 | * @return Traversable<\stdClass> The objects found |
||
167 | * @throws \Caridea\Dao\Exception\Unreachable If the connection fails |
||
168 | * @throws \Caridea\Dao\Exception\Unretrievable If the result cannot be retrieved |
||
169 | * @throws \Caridea\Dao\Exception\Generic If any other database problem occurs |
||
170 | */ |
||
171 | public function readAll(array $criteria): \Traversable |
||
172 | { |
||
173 | return $this->doExecute(function (Bucket $bucket) use ($criteria) { |
||
174 | return $bucket->find( |
||
175 | $criteria, |
||
176 | ['sort' => ['filename' => 1]] |
||
177 | ); |
||
178 | }); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Executes something in the context of the collection. |
||
183 | * |
||
184 | * Exceptions are caught and translated. |
||
185 | * |
||
186 | * @param callable $cb The closure to execute, takes the Bucket. |
||
187 | * @return - Whatever the function returns, this method also returns |
||
188 | * @throws \Caridea\Dao\Exception If a database problem occurs |
||
189 | */ |
||
190 | 1 | protected function doExecute(callable $cb) |
|
198 | |||
199 | /** |
||
200 | * @return \MongoDB\GridFS\CollectionWrapper |
||
201 | */ |
||
202 | 1 | private function getCollectionWrapper(Bucket $b) |
|
208 | } |
||
209 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: