|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
class MongoGridFS extends MongoCollection |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
const DEFAULT_CHUNK_SIZE = 262144; // 256 kb |
|
19
|
|
|
|
|
20
|
|
|
const ASCENDING = 1; |
|
21
|
|
|
const DESCENDING = -1; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.chunks |
|
25
|
|
|
* @var $chunks MongoCollection |
|
26
|
|
|
*/ |
|
27
|
|
|
public $chunks; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.filesname |
|
31
|
|
|
* @var $filesName string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $filesName; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.chunksname |
|
37
|
|
|
* @var $chunksName string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $chunksName; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var MongoDB |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $database; |
|
45
|
|
|
|
|
46
|
|
|
protected $ensureIndexes = false; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Files as stored across two collections, the first containing file meta |
|
50
|
|
|
* information, the second containing chunks of the actual file. By default, |
|
51
|
|
|
* fs.files and fs.chunks are the collection names used. |
|
52
|
|
|
* |
|
53
|
|
|
* @link http://php.net/manual/en/mongogridfs.construct.php |
|
54
|
|
|
* @param MongoDB $db Database |
|
55
|
|
|
* @param string $prefix [optional] <p>Optional collection name prefix.</p> |
|
56
|
|
|
* @param mixed $chunks [optional] |
|
57
|
|
|
* @return MongoGridFS |
|
|
|
|
|
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct(MongoDB $db, $prefix = "fs", $chunks = null) |
|
60
|
|
|
{ |
|
61
|
|
|
if ($chunks) { |
|
62
|
|
|
trigger_error(E_DEPRECATED, "The 'chunks' argument is deprecated and ignored"); |
|
63
|
|
|
} |
|
64
|
|
|
if (empty($prefix)) { |
|
65
|
|
|
throw new \InvalidArgumentException('prefix can not be empty'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->database = $db; |
|
69
|
|
|
$this->filesName = $prefix . '.files'; |
|
70
|
|
|
$this->chunksName = $prefix . '.chunks'; |
|
71
|
|
|
|
|
72
|
|
|
$this->chunks = $db->selectCollection($this->chunksName); |
|
73
|
|
|
|
|
74
|
|
|
parent::__construct($db, $this->filesName); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Drops the files and chunks collections |
|
79
|
|
|
* @link http://php.net/manual/en/mongogridfs.drop.php |
|
80
|
|
|
* @return array The database response |
|
81
|
|
|
*/ |
|
82
|
|
|
public function drop() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->chunks->drop(); |
|
85
|
|
|
parent::drop(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @link http://php.net/manual/en/mongogridfs.find.php |
|
90
|
|
|
* @param array $query The query |
|
91
|
|
|
* @param array $fields Fields to return |
|
92
|
|
|
* @return MongoGridFSCursor A MongoGridFSCursor |
|
93
|
|
|
*/ |
|
94
|
|
View Code Duplication |
public function find(array $query = array(), array $fields = array()) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
$cursor = new MongoGridFSCursor($this, $this->db->getConnection(), (string)$this, $query, $fields); |
|
|
|
|
|
|
97
|
|
|
$cursor->setReadPreference($this->getReadPreference()); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
return $cursor; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Stores a file in the database |
|
104
|
|
|
* @link http://php.net/manual/en/mongogridfs.storefile.php |
|
105
|
|
|
* @param string $filename The name of the file |
|
106
|
|
|
* @param array $extra Other metadata to add to the file saved |
|
107
|
|
|
* @param array $options Options for the store. "safe": Check that this store succeeded |
|
108
|
|
|
* @return mixed Returns the _id of the saved object |
|
109
|
|
|
*/ |
|
110
|
|
|
public function storeFile($filename, array $extra = array(), array $options = array()) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
if (is_string($filename)) { |
|
113
|
|
|
$md5 = md5_file($filename); |
|
114
|
|
|
$shortName = basename($filename); |
|
115
|
|
|
$filename = fopen($filename, 'r'); |
|
116
|
|
|
} |
|
117
|
|
|
if (! is_resource($filename)) { |
|
118
|
|
|
throw new \InvalidArgumentException(); |
|
119
|
|
|
} |
|
120
|
|
|
$length = fstat($filename)['size']; |
|
121
|
|
|
$extra['chunkSize'] = isset($extra['chunkSize']) ? $extra['chunkSize']: self::DEFAULT_CHUNK_SIZE; |
|
122
|
|
|
$extra['_id'] = isset($extra['_id']) ?: new MongoId(); |
|
123
|
|
|
$extra['length'] = $length; |
|
124
|
|
|
$extra['md5'] = isset($md5) ? $md5 : $this->calculateMD5($filename); |
|
125
|
|
|
$extra['filename'] = isset($extra['filename']) ? $extra['filename'] : $shortName; |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
$fileDocument = $this->insertFile($extra); |
|
128
|
|
|
$this->insertChunksFromFile($filename, $fileDocument); |
|
129
|
|
|
|
|
130
|
|
|
return $fileDocument['_id']; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Chunkifies and stores bytes in the database |
|
135
|
|
|
* @link http://php.net/manual/en/mongogridfs.storebytes.php |
|
136
|
|
|
* @param string $bytes A string of bytes to store |
|
137
|
|
|
* @param array $extra Other metadata to add to the file saved |
|
138
|
|
|
* @param array $options Options for the store. "safe": Check that this store succeeded |
|
139
|
|
|
* @return mixed The _id of the object saved |
|
140
|
|
|
*/ |
|
141
|
|
|
public function storeBytes($bytes, array $extra = array(), array $options = array()) |
|
|
|
|
|
|
142
|
|
|
{ |
|
143
|
|
|
$length = mb_strlen($bytes, '8bit'); |
|
144
|
|
|
$extra['chunkSize'] = isset($extra['chunkSize']) ? $extra['chunkSize'] : self::DEFAULT_CHUNK_SIZE; |
|
145
|
|
|
$extra['_id'] = isset($extra['_id']) ?: new MongoId(); |
|
146
|
|
|
$extra['length'] = $length; |
|
147
|
|
|
$extra['md5'] = md5($bytes); |
|
148
|
|
|
|
|
149
|
|
|
$file = $this->insertFile($extra); |
|
150
|
|
|
$this->insertChunksFromBytes($bytes, $file); |
|
151
|
|
|
|
|
152
|
|
|
return $file['_id']; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Returns a single file matching the criteria |
|
157
|
|
|
* @link http://www.php.net/manual/en/mongogridfs.findone.php |
|
158
|
|
|
* @param array $query The fields for which to search. |
|
159
|
|
|
* @param array $fields Fields of the results to return. |
|
160
|
|
|
* @return MongoGridFSFile|null |
|
161
|
|
|
*/ |
|
162
|
|
|
public function findOne(array $query = [], array $fields = [], array $options = []) |
|
163
|
|
|
{ |
|
164
|
|
|
$file = parent::findOne($query, $fields); |
|
165
|
|
|
if (! $file) { |
|
166
|
|
|
return; |
|
167
|
|
|
} |
|
168
|
|
|
return new MongoGridFSFile($this, $file); |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Removes files from the collections |
|
173
|
|
|
* @link http://www.php.net/manual/en/mongogridfs.remove.php |
|
174
|
|
|
* @param array $criteria Description of records to remove. |
|
175
|
|
|
* @param array $options Options for remove. Valid options are: "safe"- Check that the remove succeeded. |
|
176
|
|
|
* @throws MongoCursorException |
|
177
|
|
|
* @return boolean |
|
178
|
|
|
*/ |
|
179
|
|
|
public function remove(array $criteria = [], array $options = []) |
|
180
|
|
|
{ |
|
181
|
|
|
$matchingFiles = parent::find($criteria, ['_id' => 1]); |
|
|
|
|
|
|
182
|
|
|
$ids = []; |
|
183
|
|
|
foreach ($matchingFiles as $file) { |
|
184
|
|
|
$ids[] = $file['_id']; |
|
185
|
|
|
} |
|
186
|
|
|
$this->chunks->remove(['files_id' => ['$in' => $ids]], ['justOne' => false]); |
|
187
|
|
|
return parent::remove($criteria, ['justOne' => false] + $options); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Delete a file from the database |
|
192
|
|
|
* @link http://php.net/manual/en/mongogridfs.delete.php |
|
193
|
|
|
* @param mixed $id _id of the file to remove |
|
194
|
|
|
* @return boolean Returns true if the remove was successfully sent to the database. |
|
195
|
|
|
*/ |
|
196
|
|
|
public function delete($id) |
|
197
|
|
|
{ |
|
198
|
|
|
if (is_string($id)) { |
|
199
|
|
|
$id = new MongoId($id); |
|
200
|
|
|
} |
|
201
|
|
|
if (! $id instanceof MongoId) { |
|
202
|
|
|
return false; |
|
203
|
|
|
} |
|
204
|
|
|
$this->chunks->remove(['files_id' => $id], ['justOne' => false]); |
|
205
|
|
|
return parent::remove(['_id' => $id]); |
|
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Saves an uploaded file directly from a POST to the database |
|
210
|
|
|
* @link http://www.php.net/manual/en/mongogridfs.storeupload.php |
|
211
|
|
|
* @param string $name The name attribute of the uploaded file, from <input type="file" name="something"/>. |
|
212
|
|
|
* @param array $metadata An array of extra fields for the uploaded file. |
|
213
|
|
|
* @return mixed Returns the _id of the uploaded file. |
|
214
|
|
|
*/ |
|
215
|
|
|
public function storeUpload($name, array $metadata = array()) |
|
|
|
|
|
|
216
|
|
|
{ |
|
217
|
|
|
if (! isset($_FILES[$name]) || $_FILES[$name]['error'] !== UPLOAD_ERR_OK) { |
|
218
|
|
|
throw new \InvalidArgumentException(); |
|
219
|
|
|
} |
|
220
|
|
|
$metadata += ['filename' => $_FILES[$name]['name']]; |
|
221
|
|
|
return $this->storeFile($_FILES[$name]['tmp_name'], $metadata); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Retrieve a file from the database |
|
226
|
|
|
* @link http://www.php.net/manual/en/mongogridfs.get.php |
|
227
|
|
|
* @param mixed $id _id of the file to find. |
|
228
|
|
|
* @return MongoGridFSFile|null Returns the file, if found, or NULL. |
|
229
|
|
|
*/ |
|
230
|
|
|
public function __get($id) |
|
231
|
|
|
{ |
|
232
|
|
|
if (is_string($id)) { |
|
233
|
|
|
$id = new MongoId($id); |
|
234
|
|
|
} |
|
235
|
|
|
if (! $id instanceof MongoId) { |
|
236
|
|
|
return false; |
|
|
|
|
|
|
237
|
|
|
} |
|
238
|
|
|
return $this->findOne(['_id' => $id]); |
|
|
|
|
|
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Stores a file in the database |
|
243
|
|
|
* @link http://php.net/manual/en/mongogridfs.put.php |
|
244
|
|
|
* @param string $filename The name of the file |
|
245
|
|
|
* @param array $extra Other metadata to add to the file saved |
|
246
|
|
|
* @return mixed Returns the _id of the saved object |
|
247
|
|
|
*/ |
|
248
|
|
|
public function put($filename, array $extra = array()) |
|
249
|
|
|
{ |
|
250
|
|
|
return $this->storeFile($filename, $extra); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
private function ensureIndexes() |
|
254
|
|
|
{ |
|
255
|
|
|
if ($this->ensureIndexes) { |
|
256
|
|
|
return; |
|
257
|
|
|
} |
|
258
|
|
|
$this->ensureFilesIndex(); |
|
259
|
|
|
$this->ensureChunksIndex(); |
|
260
|
|
|
$this->ensuredIndexes = true; |
|
|
|
|
|
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
private function ensureChunksIndex() |
|
264
|
|
|
{ |
|
265
|
|
|
foreach ($this->chunks->getIndexInfo() as $index) { |
|
266
|
|
|
if (isset($index['unique']) && $index['unique'] && $index['key'] === ['files_id' => 1, 'n' => 1]) { |
|
267
|
|
|
return; |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
$this->chunks->createIndex(['files_id' => 1, 'n' => 1], ['unique' => true]); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
private function ensureFilesIndex() |
|
274
|
|
|
{ |
|
275
|
|
|
foreach ($this->getIndexInfo() as $index) { |
|
276
|
|
|
if ($index['key'] === ['filename' => 1, 'uploadDate' => 1]) { |
|
277
|
|
|
return; |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
$this->createIndex(['filename' => 1, 'uploadDate' => 1]); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
private function insertChunksFromFile($file, $fileInfo) |
|
284
|
|
|
{ |
|
285
|
|
|
$length = $fileInfo['length']; |
|
286
|
|
|
$chunkSize = $fileInfo['chunkSize']; |
|
287
|
|
|
$fileId = $fileInfo['_id']; |
|
288
|
|
|
$offset = 0; |
|
289
|
|
|
$i = 0; |
|
290
|
|
|
|
|
291
|
|
|
rewind($file); |
|
292
|
|
|
|
|
293
|
|
|
while ($offset < $length) { |
|
294
|
|
|
$data = stream_get_contents($file, $chunkSize); |
|
295
|
|
|
$this->insertChunk($fileId, $data, $i++); |
|
296
|
|
|
$offset += $chunkSize; |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
private function calculateMD5($file) |
|
301
|
|
|
{ |
|
302
|
|
|
// XXX: this could be really a bad idea with big files... |
|
303
|
|
|
rewind($file); |
|
304
|
|
|
$data = stream_get_contents($file); |
|
305
|
|
|
|
|
306
|
|
|
return md5($data); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
private function insertChunksFromBytes($bytes, $fileInfo) |
|
310
|
|
|
{ |
|
311
|
|
|
$length = $fileInfo['length']; |
|
|
|
|
|
|
312
|
|
|
$chunkSize = $fileInfo['chunkSize']; |
|
313
|
|
|
$fileId = $fileInfo['_id']; |
|
314
|
|
|
$i = 0; |
|
315
|
|
|
|
|
316
|
|
|
$chunks = str_split($bytes, $chunkSize); |
|
317
|
|
|
foreach ($chunks as $chunk) { |
|
318
|
|
|
$this->insertChunk($fileId, $chunk, $i++); |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
private function insertChunk($id, $data, $chunkNumber) |
|
323
|
|
|
{ |
|
324
|
|
|
$chunk = [ |
|
325
|
|
|
'files_id' => $id, |
|
326
|
|
|
'n' => $chunkNumber, |
|
327
|
|
|
'data' => new MongoBinData($data), |
|
328
|
|
|
]; |
|
329
|
|
|
return $this->chunks->insert($chunk); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
private function insertFile($metadata) |
|
333
|
|
|
{ |
|
334
|
|
|
$this->ensureIndexes(); |
|
335
|
|
|
$metadata['uploadDate'] = new MongoDate(); |
|
336
|
|
|
$this->insert($metadata); |
|
337
|
|
|
return $metadata; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
} |
|
341
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.