-
Notifications
You must be signed in to change notification settings - Fork 12
/
File.php
278 lines (246 loc) · 7.47 KB
/
File.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
namespace sammaye\mongoyii;
use MongoDB\BSON\ObjectID;
use MongoDB\GridFS\GridFSDownload;
use Yii;
use CUploadedFile;
use sammaye\mongoyii\Document;
use sammaye\mongoyii\Exception;
/**
* The MongoYii representation of a helper for uploading files to GridFS.
*
* It can accept an input file from $_FILES via ::populate and can also do find() and findOne() on the files collection.
* This file is specifically designed for uploading files from a form to GridFS and is merely a helper, IT IS IN NO WAY REQUIRED.
*/
class File extends Document
{
/**
* Our file object, can be either the MongoGridFSFile or CUploadFile
*/
private $_file;
// Helper functions to get some common functionality on this class
/**
* @return string|bool
*/
public function getFilename()
{
if($this->getFile() instanceof GridFSDownload){
return $this->getFile()->getFilename();
}
if($this->getFile() instanceof CUploadedFile){
return $this->getFile()->getTempName();
}
if(is_string($this->getFile()) && is_file($this->getFile())){
return $this->getFile();
}
return false;
}
/**
* @return int|bool
*/
public function getSize()
{
if($this->getFile() instanceof GridFSDownload || $this->getFile() instanceof CUploadedFile){
return $this->getFile()->getSize();
}
if(is_file($this->getFile())){
return filesize($this->getFile());
}
return false;
}
/**
* @return string|bool
*/
public function getBytes()
{
if($this->getFile() instanceof GridFSDownload){
return $this->getFile()->getBytes();
}
if($this->getFile() instanceof CUploadedFile || (is_file($this->getFile()) && is_readable($this->getFile()))){
return file_get_contents($this->getFilename());
}
return false;
}
/**
* Gets the file object
*/
public function getFile()
{
// This if statement allows for you to continue using this class AFTER insert
// basically it will only get the file if you plan on using it further which means that
// otherwise it omits at least one database call each time
if($this->_id instanceof ObjectID && !$this->_file instanceof GridFSDownload){
return $this->_file = $this->getCollection()->get($this->_id);
}
return $this->_file;
}
/**
* Sets the file object
*/
public function setFile($v)
{
$this->_file = $v;
}
/**
* This denotes the prefix to all gridfs collections set by this class
* @return string
*/
public function collectionPrefix()
{
return 'fs';
}
/**
* Returns the static model of the specified AR class.
* @param string $className
* @return EMongoDocument - User the static model class
*/
public static function model($className = __CLASS__)
{
return parent::model($className);
}
/**
* Magic will either call a function on the file if it exists or bubble to parent
* @see EMongoDocument::__call()
*/
public function __call($name, $parameters)
{
if($this->getFile() instanceof GridFSDownload && method_exists($this->getFile(), $name)){
return call_user_func_array(array($this->getFile(), $name), $parameters);
}
return parent::__call($name, $parameters);
}
/**
* This can populate from a $_FILES instance
* @param CModel $model
* @param string $attribute
* @return boolean|EMongoFile|null
*/
public static function populate($model, $attribute)
{
if($file = CUploadedFile::getInstance($model, $attribute)){
$model=new static;
$model->setFile($file);
return $model;
}
return null;
}
/**
* This function populates from a stream
*
* You must unlink the tempfile yourself by calling unlink($file->getFilename())
* @param string $stream
* @return EMongoFile the new file generated from the stream
public static function stream($stream){
$tempFile = tempnam(null, 'tmp'); // returns a temporary filename
$fp = fopen($tempFile, 'wb'); // open temporary file
$putData = fopen($stream, 'rb'); // open input stream
stream_copy_to_stream($putData, $fp); // write input stream directly into file
fclose($putData);
fclose($fp);
$file = new EMongoFile();
$file->setFile($tempFile);
return $file;
}
*/
/**
* Replaces the normal populateRecord specfically for GridFS by setting the attributes from the
* MongoGridFsFile object correctly and other file details like size and name.
* @see EMongoDocument::populateRecord()
* @param array $attributes
* @param bool $callAfterFind
* @param bool $partial
* @return EMongoDocument|null
*/
public function populateRecord($attributes, $callAfterFind = true, $partial = false)
{
if($attributes === false){
return null;
}
// the cursor will actually input a MongoGridFSFile object as the "document"
// so what we wanna do is get the attributes or metadata attached to the file object
// set it as our attributes and then set this classes file as the first param we got
$file = $attributes;
$attributes = $file->file;
$record = $this->instantiate($attributes);
$record->setFile($file);
$record->setScenario('update');
$record->setIsNewRecord(false);
$record->init();
$labels = array();
foreach($attributes as $name => $value){
$labels[$name] = 1;
$record->$name = $value;
}
if($partial){
$record->setIsPartial(true);
$record->setProjectedFields($labels);
}
//$record->_pk=$record->primaryKey();
$record->attachBehaviors($record->behaviors());
if($callAfterFind){
$record->afterFind();
}
return $record;
}
/**
* Inserts the file.
*
* The only difference between the normal insert is that this uses the storeFile function on the GridFS object
* @see EMongoDocument::insert()
* @param array $attributes
* @return bool
* @throws EMongoException
*/
public function insert($attributes = null)
{
if(!$this->getIsNewRecord()){
throw new Exception(Yii::t('yii','The active record cannot be inserted to database because it is not new.'));
}
if(!$this->beforeSave()){
return false;
}
$this->trace(__FUNCTION__);
if($attributes === null){
$document=$this->getRawDocument();
}else{
$document=$this->filterRawDocument($this->getAttributes($attributes));
}
if(YII_DEBUG){
// we're actually physically testing for Yii debug mode here to stop us from
// having to do the serialisation on the update doc normally.
Yii::trace('Executing storeFile: {$document:' . json_encode($document) . '}', 'extensions.MongoYii.EMongoDocument');
}
if($this->getDbConnection()->enableProfiling){
$this->profile('extensions.MongoYii.EMongoFile.insert({$document:' . json_encode($document) . '})', 'extensions.MongoYii.EMongoFile.insert');
}
if($_id = $this->getCollection()->storeFile($this->getFilename(), $document)){ // The key change
$this->_id = $_id;
$this->afterSave();
$this->setIsNewRecord(false);
$this->setScenario('update');
return true;
}
return false;
}
/**
* Get collection will now return the GridFS object from the driver
* @see EMongoDocument::getCollection()
*/
public function getCollection()
{
return $this
->getDbConnection()
->selectDatabase()
->getGridFS([
'bucketName' => $this->collectionPrefix()
]);
}
/**
* Produces a trace message for functions in this class
* @param string $func
*/
public function trace($func)
{
Yii::trace(get_class($this) . '.' . $func.'()', 'extensions.MongoYii.EMongoFile');
}
}