|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the PHPMongo package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Dmytro Sokil <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sokil\Mongo; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Representation of GridFS as collection of files |
|
16
|
|
|
* |
|
17
|
|
|
* @property \MongoGridFS $collection MongoGridFS Instance |
|
18
|
|
|
*/ |
|
19
|
|
|
class GridFS extends Collection |
|
20
|
|
|
{ |
|
21
|
|
|
protected $mongoCollectionClassName = '\MongoGridFS'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Factory method to get document object from array of stored document |
|
25
|
|
|
* @param \MongoGridFSFile $data |
|
26
|
|
|
* @return \Sokil\Mongo\GridFsFile |
|
27
|
|
|
*/ |
|
28
|
|
|
public function hydrate($data, $useDocumentPool = true) |
|
29
|
|
|
{ |
|
30
|
|
|
if(($data instanceof \MongoGridFSFile) === false) { |
|
31
|
|
|
throw new Exception('Must be \MongoGridFSFile'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$className = $this->getFileClassName($data); |
|
35
|
|
|
|
|
36
|
|
|
return new $className($this, $data); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Override to define class name of file by file data |
|
41
|
|
|
* |
|
42
|
|
|
* @param \MongoGridFSFile $fileData |
|
43
|
|
|
* @return string Document class data |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getFileClassName(\MongoGridFSFile $fileData = null) |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
return '\Sokil\Mongo\GridFSFile'; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Create file in GridFS from file in filesystem |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $filename name of source file |
|
54
|
|
|
* @param array $metadata metadata stored with file |
|
55
|
|
|
* @return \MongoId Id of stored file |
|
56
|
|
|
*/ |
|
57
|
|
|
public function storeFile($filename, $metadata = array()) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->getMongoCollection()->storeFile($filename, $metadata); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Create file in GridFS from binary data |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $bytes binary data to store in GridFS |
|
66
|
|
|
* @param array $metadata metadata stored with file |
|
67
|
|
|
* @return \MongoId Id of stored file |
|
68
|
|
|
*/ |
|
69
|
|
|
public function storeBytes($bytes, $metadata = array()) |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->getMongoCollection()->storeBytes($bytes, $metadata); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get file instance by id of document |
|
76
|
|
|
* Used \MongoGridFS::findOne() instead of \MongoGridFS::get() due to backward compatibility with old mongo extensions |
|
77
|
|
|
* |
|
78
|
|
|
* @param \MongoId|string|int $id |
|
79
|
|
|
* @return \Sokil\Mongo\GridFSFile|null |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getFileById($id) |
|
82
|
|
|
{ |
|
83
|
|
|
if($id instanceof \MongoId) { |
|
84
|
|
|
$file = $this->getMongoCollection()->findOne(array('_id' => $id)); |
|
85
|
|
|
} else { |
|
86
|
|
|
try { |
|
87
|
|
|
$file = $this->getMongoCollection()->findOne(array('_id' => new \MongoId($id))); |
|
88
|
|
|
} catch (\MongoException $e) { |
|
89
|
|
|
$file = $this->getMongoCollection()->findOne(array('_id' => $id)); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if(!$file) { |
|
|
|
|
|
|
94
|
|
|
return null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$fileClassName = $this->getFileClassName($file); |
|
|
|
|
|
|
98
|
|
|
return new $fileClassName($this, $file); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Delete file by id |
|
103
|
|
|
* |
|
104
|
|
|
* @param string|\MongoId $id id of file's document |
|
105
|
|
|
* @return \Sokil\Mongo\GridFS |
|
106
|
|
|
* @throws Exception |
|
107
|
|
|
*/ |
|
108
|
|
|
public function deleteFileById($id) |
|
109
|
|
|
{ |
|
110
|
|
|
if($id instanceof \MongoId) { |
|
111
|
|
|
$result = $this->getMongoCollection()->delete($id); |
|
|
|
|
|
|
112
|
|
|
} else { |
|
113
|
|
|
try { |
|
114
|
|
|
$result = $this->getMongoCollection()->delete(new \MongoId($id)); |
|
|
|
|
|
|
115
|
|
|
} catch (\MongoException $e) { |
|
116
|
|
|
$result = $this->getMongoCollection()->delete($id); |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
View Code Duplication |
if($result['ok'] !== (double) 1) { |
|
|
|
|
|
|
120
|
|
|
throw new Exception('Error deleting file: ' . $result['err'] . ': ' . $result['errmsg']); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.