This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathelFinderVolumeMongoGridFS.class.php
530 lines (492 loc) · 12.9 KB
/
elFinderVolumeMongoGridFS.class.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<?php
/**
* MongoDB GridFS driver for elFinder
*
* Supply your own MongoGridFS instance to 'db' option
*
* @author Manatsawin Hanmongkolchai
* @license Apache license 2.0
* This driver is developed under Kyou project.
* Kyou project's development is sponsored by NECTEC under NSC2013 program.
* NECTEC does not provides support for this driver
*/
class elFinderVolumeMongoGridFS extends elFinderVolumeDriver {
/**
* @var string
*/
protected $driverId = 'm';
/**
* @var MongoGridFS
*/
protected $db;
/**
* File used to hold a directory metadata
* Should not be changed
*/
const PLACEHOLDER = "__placeholder";
public function __construct(){
// Actually MongoClient only exists in v1.3.0 so you will get class not found anyway
// \m/
if(version_compare(MongoClient::VERSION, '1.3.0', '<')){
throw new RuntimeException("elFinderVolumeMongoGridFS only supports MongoClient version 1.3.0 and above");
}
$this->options['mimeDetect'] = 'internal';
}
/**
* Check for connection
* @return bool
*/
protected function init(){
if($this->options['db'] instanceof MongoDB){
$this->options['db'] = $this->options['db']->getGridFS();
}else if(!$this->options['db'] instanceof MongoGridFS){
throw new InvalidArgumentException("db option is not instance of MongoDB or MongoGridFS");
}
$this->db = $this->options['db'];
return true;
}
protected function format_stat(MongoGridFSFile $file){
return array(
"name" => $file->getFilename(),
"size" => $file->getSize(),
"ts" => $file->file['uploadDate']->sec,
"mime" => $file->file['metadata']['mime'],
"read" => true,
"write" => true,
"width" => $file->file['metadata']['width'],
"height" => $file->file['metadata']['height'],
);
}
protected function subdirs($path){
$subdirs = $this->db->find(array(
"metadata.path" => new MongoRegex('/^'.preg_quote($path, '/').'\/[^\/]+$/')
), array("metadata.path" => true));
$subdirsName = array();
foreach($subdirs as $item){
$subdirsName[] = $item->file['metadata']['path'];
}
return array_unique($subdirsName);
}
/**
* Scan directory
*/
protected function cacheDir($path) {
$this->dirsCache[$path] = array();
$path = preg_replace('~/$~', '', $path);
// subdirs in the directory
foreach($this->subdirs($path) as $subdir){
$data = array(
//"id" => $subdir,
"name" => $subdir,
"size" => 0,
"mime" => "directory",
"read" => true,
"write" => true,
"parent_id" => $path,
);
if ($stat = $this->updateCache($this->_joinPath($path, $data['name']), $data)) {
$this->dirsCache[$path][] = $data['name'];
}
}
// files in the directory
$files = $this->db->find(array(
"metadata.path" => $path
));
foreach($files as $file){
if($file->getFilename() == $this::PLACEHOLDER){ // for mkdir()
continue;
}
$data = $this->format_stat($file);
$fullpath = $this->_joinPath($path, $data['name']);
if ($stat = $this->updateCache($fullpath, $data)) {
$this->dirsCache[$path][] = $fullpath;
}
}
return $this->dirsCache[$path];
}
protected function make($path, $name, $mime){
if($mime == "directory"){
$path = $this->_joinPath($path, $name);
$name = $this::PLACEHOLDER;
}
return $this->db->storeBytes("", array(
"filename" => $name,
"metadata" => array(
"path" => $path,
"mime" => $mime,
)
));
}
protected function getFile($path){
$file = $this->db->findOne(array(
"filename" => $this->_basename($path),
"metadata.path" => $this->_dirname($path)
));
return $file;
}
/**
* Join dir name and file name
*/
protected function _joinPath($path, $name) {
$path = preg_replace('~/$~', '', $path);
return $path.$this->separator.$name;
}
/**
* Return stat for given path.
* Stat contains following fields:
* - (int) size file size in b. required
* - (string) name file name. required
* - (int) ts file modification time in unix time. required
* - (string) mime mimetype. required for folders, others - optionally
* - (bool) read read permissions. required
* - (bool) write write permissions. required
* - (bool) locked is object locked. optionally
* - (bool) hidden is object hidden. optionally
* - (string) alias for symlinks - link target path relative to root path. optionally
* - (string) target for symlinks - link target path. optionally
*
* If file does not exists - returns empty array or false.
*
* @param string $path file path
* @return array|false
**/
protected function _stat($path) {
$file = $this->getFile($path);
if(!$file){
// It could be a directory...
$path = preg_replace('~/$~', '', $path);
$file = $this->db->find(array(
"metadata.path" => $path
));
$file->sort(array(
"uploadDate" => -1
));
if($file->count() == 0 && $path != $this->root){
return false;
}
$parent_dir = preg_replace('~[/]{0,1}[^\/]+$~', '', $path);
$subdir = count($this->subdirs($path));
$out = array(
//"id" => $this->_basename($path),
"name" => $this->_basename($path),
"size" => 0,
"ts" => $file->current()->file['uploadDate']->sec,
"mime" => "directory",
"dirs" => $subdir,
"read" => true,
"write" => true,
"parent_id" => $parent_dir,
);
if($path == $this->root){
unset($out['parent_id']);
}
return $out;
}
return $this->format_stat($file);
}
/**
* Open file and return file pointer (read only)
*
* @param string $path file path
* @param string $mode open file mode (ignored in this driver)
* @return resource|false
**/
protected function _fopen($path, $mode='rb'){
$file = $this->getFile($path);
if(!$file){
return false;
}
return $file->getResource();
}
/**
* Close opened file
**/
protected function _fclose($fp, $path='') {
@fclose($fp);
}
/**
* Copy file into another file
* WARNING: This function loads the entire file into memory
*
* @param string $source source file path
* @param string $targetDir target directory path
* @param string $name new file name
* @return string
**/
protected function _copy($source, $targetDir, $name) {
$this->clearcache();
$src = $this->getFile($source);
$targetDir = preg_replace('~/$~', '', $targetDir);
return $this->db->storeBytes($src->getBytes(), array(
"filename" => $name,
"metadata" => array(
"path" => $targetDir,
"mime" => $src->file['metadata']['mime'],
)
));
}
/**
* Move file into another parent dir.
* Return new file path or false.
*
* @param string $source source file path
* @param string $target target dir path
* @param string $name file name
* @return string
**/
protected function _move($source, $targetDir, $name) {
$targetDir = preg_replace('~/$~', '', $targetDir);
$this->db->update(array(
'filename' => $this->_basename($source),
'metadata.path' => $this->_dirname($source),
), array(
'$set' => array(
'filename' => (string) $name,
'metadata.path' => $targetDir
)
));
return $targetDir;
}
/**
* Remove file
*
* @param string $path file path
* @return bool
**/
protected function _unlink($path) {
$file = $this->getFile($path);
if(!$file){
return false;
}
return $this->db->delete($file->file['_id']);
}
/**
* Remove dir. Will fail if the directory is not empty
*
* @param string $path dir path
* @return bool
**/
protected function _rmdir($path) {
if(count($this->_scandir($path)) > 0){
return false;
}
return $this->_unlink($this->_joinPath($path, $this::PLACEHOLDER));
}
/**
* Create new file and write into it from file pointer.
* Return new file path or false on error.
*
* @param resource $fp file pointer
* @param string $dir target dir path
* @param string $name file name
* @return bool|string
**/
protected function _save($fp, $dir, $name, $mime, $w, $h) {
$dir = preg_replace('~/$~', '', $dir);
$content = '';
while (!feof($fp)) {
$content .= fread($fp, 8192);
}
$this->db->storeBytes($content, array(
"filename" => $name,
"metadata" => array(
"path" => $dir,
"mime" => $mime,
"width" => $w,
"height" => $h
)
));
return $this->_joinPath($dir, $name);
}
/**
* Get file contents
*
* @param string $path file path
* @return string|false
**/
protected function _getContents($path) {
$file = $this->getFile($path);
if(!$file){
return false;
}
return $file->getBytes();
}
/**
* Write a string to a file
*
* @param string $path file path
* @param string $content new file content
* @return bool
**/
protected function _filePutContents($path, $content) {
$oldFile = $this->getFile($path);
if(!$oldFile){
return false;
}
return $this->db->storeBytes($content, array(
"filename" => $oldFile->file['filename'],
"metadata" => $oldFile->file['metadata']
));
}
// ************ copy pasted methods *********
/**
* Return array of parents paths (ids)
*
* @param int $path file path (id)
* @return array
* @author Dmitry (dio) Levashov
**/
protected function getParents($path) {
$parents = array();
while ($path) {
if ($file = $this->stat($path)) {
array_unshift($parents, $path);
$path = isset($file['phash']) ? $this->decode($file['phash']) : false;
}
}
if (count($parents)) {
array_pop($parents);
}
return $parents;
}
/**
* Return parent directory path
*/
protected function _dirname($path){
return dirname($path);
}
/**
* Return file name
*/
protected function _basename($path) {
return basename($path);
}
/**
* Return normalized path, this works the same as os.path.normpath() in Python
**/
protected function _normpath($path) {
if (empty($path)) {
return '.';
}
if (strpos($path, '/') === 0) {
$initial_slashes = true;
} else {
$initial_slashes = false;
}
if (($initial_slashes)
&& (strpos($path, '//') === 0)
&& (strpos($path, '///') === false)) {
$initial_slashes = 2;
}
$initial_slashes = (int) $initial_slashes;
$comps = explode('/', $path);
$new_comps = array();
foreach ($comps as $comp) {
if (in_array($comp, array('', '.'))) {
continue;
}
if (($comp != '..')
|| (!$initial_slashes && !$new_comps)
|| ($new_comps && (end($new_comps) == '..'))) {
array_push($new_comps, $comp);
} elseif ($new_comps) {
array_pop($new_comps);
}
}
$comps = $new_comps;
$path = implode('/', $comps);
if ($initial_slashes) {
$path = str_repeat('/', $initial_slashes) . $path;
}
return $path ? $path : '.';
}
/**
* Return file path related to root dir
*
* @param string $path file path
* @return string
* @author Dmitry (dio) Levashov
**/
protected function _relpath($path) {
return $path == $this->root ? '' : substr($path, strlen($this->root)+1);
}
/**
* Convert path related to root dir into real path
*
* @param string $path file path
* @return string
* @author Dmitry (dio) Levashov
**/
protected function _abspath($path) {
return $path == DIRECTORY_SEPARATOR ? $this->root : $this->root.DIRECTORY_SEPARATOR.$path;
}
/**
* Return fake path started from root dir
*
* @param string $path file path
* @return string
* @author Dmitry (dio) Levashov
**/
protected function _path($path) {
return $this->rootName.($path == $this->root ? '' : $this->separator.$this->_relpath($path));
}
/**
* Return true if $path is children of $parent
**/
protected function _inpath($path, $parent) {
return $path == $parent
? true
: in_array($parent, $this->getParents($path));
}
/**
* Return true if path is dir and has at least one childs directory
**/
protected function _subdirs($path) {
return ($stat = $this->stat($path)) && isset($stat['dirs']) ? $stat['dirs'] : false;
}
/**
* Return object width and height
**/
protected function _dimensions($path, $mime) {
return ($stat = $this->stat($path)) && isset($stat['width']) && isset($stat['height']) ? $stat['width'].'x'.$stat['height'] : '';
}
/**
* Actually implemented by cacheDir
**/
protected function _scandir($path) {
$path = preg_replace('~/$~', '', $path);
return isset($this->dirsCache[$path])
? $this->dirsCache[$path]
: $this->cacheDir($path);
}
/**
* Create dir and return path
*/
protected function _mkdir($path, $name) {
return $this->make($path, $name, 'directory') ? $this->_joinPath($path, $name) : false;
}
/**
* Create file and return path
**/
protected function _mkfile($path, $name) {
return $this->make($path, $name, 'text/plain') ? $this->_joinPath($path, $name) : false;
}
protected function _symlink($target, $path, $name) {
return false;
}
protected function _checkArchivers() {
return;
}
protected function _unpack($path, $arc) {
return;
}
protected function _findSymlinks($path) {
return false;
}
protected function _extract($path, $arc) {
return false;
}
protected function _archive($dir, $files, $name, $arc) {
return false;
}
}