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
|
|
|
use Alcaeus\MongoDbAdapter\TypeConverter; |
17
|
|
|
|
18
|
|
|
class MongoGridFSFile |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @link http://php.net/manual/en/class.mongogridfsfile.php#mongogridfsfile.props.file |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
public $file; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @link http://php.net/manual/en/class.mongogridfsfile.php#mongogridfsfile.props.gridfs |
28
|
|
|
* @var $gridfs |
29
|
|
|
*/ |
30
|
|
|
protected $gridfs; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @link http://php.net/manual/en/mongogridfsfile.construct.php |
34
|
|
|
* @param MongoGridFS $gridfs The parent MongoGridFS instance |
35
|
|
|
* @param array $file A file from the database |
36
|
|
|
* @return MongoGridFSFile Returns a new MongoGridFSFile |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public function __construct(MongoGridFS $gridfs, array $file) |
39
|
|
|
{ |
40
|
|
|
$this->gridfs = $gridfs; |
41
|
|
|
$this->file = $file; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns this file's filename |
46
|
|
|
* @link http://php.net/manual/en/mongogridfsfile.getfilename.php |
47
|
|
|
* @return string Returns the filename |
48
|
|
|
*/ |
49
|
|
|
public function getFilename() |
50
|
|
|
{ |
51
|
|
|
if (isset($this->file['filename'])) { |
52
|
|
|
return $this->file['filename']; |
53
|
|
|
} |
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns this file's size |
60
|
|
|
* @link http://php.net/manual/en/mongogridfsfile.getsize.php |
61
|
|
|
* @return int Returns this file's size |
62
|
|
|
*/ |
63
|
|
|
public function getSize() |
64
|
|
|
{ |
65
|
|
|
return $this->file['length']; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Writes this file to the filesystem |
70
|
|
|
* @link http://php.net/manual/en/mongogridfsfile.write.php |
71
|
|
|
* @param string $filename The location to which to write the file (path+filename+extension). If none is given, the stored filename will be used. |
72
|
|
|
* @return int Returns the number of bytes written |
73
|
|
|
*/ |
74
|
|
|
public function write($filename = null) |
75
|
|
|
{ |
76
|
|
|
if ($filename === null) { |
77
|
|
|
$filename = $this->getFilename(); |
78
|
|
|
} |
79
|
|
|
if (empty($filename)) { |
80
|
|
|
$filename = 'file'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$handle = fopen($filename, 'w'); |
84
|
|
|
$written = $this->writeFromRessource($handle); |
85
|
|
|
fclose($handle); |
86
|
|
|
return $written; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* This will load the file into memory. If the file is bigger than your memory, this will cause problems! |
92
|
|
|
* @link http://php.net/manual/en/mongogridfsfile.getbytes.php |
93
|
|
|
* @return string Returns a string of the bytes in the file |
94
|
|
|
*/ |
95
|
|
|
public function getBytes() |
96
|
|
|
{ |
97
|
|
|
$result = ''; |
98
|
|
|
$chunks = $this->getChunks(); |
99
|
|
|
foreach ($chunks as $chunk) { |
100
|
|
|
$result .= $chunk['data']->bin; |
101
|
|
|
} |
102
|
|
|
return $result; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* This method returns a stream resource that can be used to read the stored file with all file functions in PHP. |
107
|
|
|
* The contents of the file are pulled out of MongoDB on the fly, so that the whole file does not have to be loaded into memory first. |
108
|
|
|
* At most two GridFSFile chunks will be loaded in memory. |
109
|
|
|
* |
110
|
|
|
* @link http://php.net/manual/en/mongogridfsfile.getresource.php |
111
|
|
|
* @return resource Returns a resource that can be used to read the file with |
112
|
|
|
*/ |
113
|
|
|
public function getResource() |
114
|
|
|
{ |
115
|
|
|
$handle = tmpfile(); |
116
|
|
|
$this->writeFromRessource($handle); |
117
|
|
|
fseek($handle, 0); |
118
|
|
|
return $handle; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
private function writeFromRessource($handle) |
123
|
|
|
{ |
124
|
|
|
|
125
|
|
|
if (! $handle) { |
126
|
|
|
trigger_error(E_ERROR, 'can not open the destination file'); |
127
|
|
|
} |
128
|
|
|
$written = 0; |
129
|
|
|
$chunks = $this->getChunks(); |
130
|
|
|
foreach ($chunks as $chunk) { |
131
|
|
|
$written += fwrite($handle, $chunk['data']->bin); |
132
|
|
|
} |
133
|
|
|
return $written; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private function getChunks() |
137
|
|
|
{ |
138
|
|
|
return $chunks = $this->gridfs->chunks->find( |
|
|
|
|
139
|
|
|
['files_id' => new \MongoDB\BSON\ObjectID((string) $this->file['_id'])], |
140
|
|
|
['data' => 1], |
141
|
|
|
['n' => 1] |
|
|
|
|
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
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.