|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gaufrette\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use Gaufrette\Adapter; |
|
6
|
|
|
use MongoDB\BSON\Regex; |
|
7
|
|
|
use MongoDB\GridFS\Bucket; |
|
8
|
|
|
use MongoDB\GridFS\Exception\FileNotFoundException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Adapter for the GridFS filesystem on MongoDB database. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Tomi Saarinen <[email protected]> |
|
14
|
|
|
* @author Antoine Hérault <[email protected]> |
|
15
|
|
|
* @author Leszek Prabucki <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class GridFS implements Adapter, |
|
|
|
|
|
|
18
|
|
|
ChecksumCalculator, |
|
|
|
|
|
|
19
|
|
|
MetadataSupporter, |
|
|
|
|
|
|
20
|
|
|
ListKeysAware |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
/** @var array */ |
|
23
|
|
|
private $metadata = []; |
|
24
|
|
|
|
|
25
|
|
|
/** @var Bucket */ |
|
26
|
|
|
private $bucket; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Bucket $bucket |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Bucket $bucket) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->bucket = $bucket; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function read($key) |
|
40
|
|
|
{ |
|
41
|
|
|
try { |
|
42
|
|
|
$stream = $this->bucket->openDownloadStreamByName($key); |
|
43
|
|
|
} catch (FileNotFoundException $e) { |
|
44
|
|
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
try { |
|
48
|
|
|
return stream_get_contents($stream); |
|
49
|
|
|
} finally { |
|
50
|
|
|
fclose($stream); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public function write($key, $content) |
|
58
|
|
|
{ |
|
59
|
|
|
$stream = $this->bucket->openUploadStream($key, ['metadata' => $this->getMetadata($key)]); |
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
|
|
fwrite($stream, $content); |
|
63
|
|
|
} finally { |
|
64
|
|
|
fclose($stream); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return strlen($content); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function isDirectory($key) |
|
74
|
|
|
{ |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
public function rename($sourceKey, $targetKey) |
|
82
|
|
|
{ |
|
83
|
|
|
$metadata = $this->getMetadata($sourceKey); |
|
84
|
|
|
$writable = $this->bucket->openUploadStream($targetKey, ['metadata' => $metadata]); |
|
85
|
|
|
|
|
86
|
|
|
try { |
|
87
|
|
|
$this->bucket->downloadToStreamByName($sourceKey, $writable); |
|
88
|
|
|
$this->setMetadata($targetKey, $metadata); |
|
89
|
|
|
$this->delete($sourceKey); |
|
90
|
|
|
} catch (FileNotFoundException $e) { |
|
91
|
|
|
return false; |
|
92
|
|
|
} finally { |
|
93
|
|
|
fclose($writable); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function exists($key) |
|
103
|
|
|
{ |
|
104
|
|
|
return (boolean) $this->bucket->findOne(['filename' => $key]); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
|
|
public function keys() |
|
111
|
|
|
{ |
|
112
|
|
|
$keys = []; |
|
113
|
|
|
$cursor = $this->bucket->find([], ['projection' => ['filename' => 1]]); |
|
114
|
|
|
|
|
115
|
|
|
foreach ($cursor as $file) { |
|
116
|
|
|
$keys[] = $file['filename']; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $keys; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
|
|
public function mtime($key) |
|
126
|
|
|
{ |
|
127
|
|
|
$file = $this->bucket->findOne(['filename' => $key], ['projection' => ['uploadDate' => 1]]); |
|
128
|
|
|
|
|
129
|
|
|
return $file ? (int) $file['uploadDate']->toDateTime()->format('U') : false; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
|
|
public function checksum($key) |
|
136
|
|
|
{ |
|
137
|
|
|
$file = $this->bucket->findOne(['filename' => $key], ['projection' => ['md5' => 1]]); |
|
138
|
|
|
|
|
139
|
|
|
return $file ? $file['md5'] : false; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* {@inheritdoc} |
|
144
|
|
|
*/ |
|
145
|
|
|
public function delete($key) |
|
146
|
|
|
{ |
|
147
|
|
|
if (null === $file = $this->bucket->findOne(['filename' => $key], ['projection' => ['_id' => 1]])) { |
|
148
|
|
|
return false; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$this->bucket->delete($file['_id']); |
|
152
|
|
|
|
|
153
|
|
|
return true; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* {@inheritdoc} |
|
158
|
|
|
*/ |
|
159
|
|
|
public function setMetadata($key, $metadata) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->metadata[$key] = $metadata; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* {@inheritdoc} |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getMetadata($key) |
|
168
|
|
|
{ |
|
169
|
|
|
return isset($this->metadata[$key]) ? $this->metadata[$key] : array(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* {@inheritdoc} |
|
174
|
|
|
*/ |
|
175
|
|
|
public function listKeys($prefix = '') |
|
176
|
|
|
{ |
|
177
|
|
|
$prefix = trim($prefix); |
|
178
|
|
|
|
|
179
|
|
|
if ($prefix === '') { |
|
180
|
|
|
return [ |
|
181
|
|
|
'dirs' => [], |
|
182
|
|
|
'keys' => $this->keys(), |
|
183
|
|
|
]; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
$regex = new Regex(sprintf('^%s', $prefix), ''); |
|
187
|
|
|
$files = $this->bucket->find(['filename' => $regex], ['projection' => ['filename' => 1]]); |
|
188
|
|
|
$result = [ |
|
189
|
|
|
'dirs' => [], |
|
190
|
|
|
'keys' => [], |
|
191
|
|
|
]; |
|
192
|
|
|
|
|
193
|
|
|
foreach ($files as $file) { |
|
194
|
|
|
$result['keys'][] = $file['filename']; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $result; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|