-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService.php
More file actions
123 lines (90 loc) · 3.27 KB
/
Service.php
File metadata and controls
123 lines (90 loc) · 3.27 KB
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
<?php
namespace MerapiPanel\Module\FileManager;
use MerapiPanel\Box\Module\__Fragment;
use MerapiPanel\Box\Module\Entity\Module;
use Symfony\Component\Filesystem\Path;
use Throwable;
class Service extends __Fragment
{
protected string $root;
protected $module;
function onCreate(Module $module)
{
$this->module = $module;
$this->root = Path::join($_ENV['__MP_CWD__'], 'content');
try {
if (!file_exists($this->root)) mkdir($this->root, 0777, true);
$upload = Path::join($this->root, "upload");
if (!file_exists($upload)) mkdir($upload, 0777, true);
} catch (Throwable $t) {
error_log("", $t->getMessage());
}
}
public function getRoot()
{
$root = $_ENV['__MP_CWD__'] . "/content";
if (!file_exists($root))
mkdir($root);
$root .= "/upload";
if (!file_exists($root))
mkdir($root);
return $root;
}
public function getFolder($path = '')
{
$root = $this->getRoot();
$path = rtrim($root, '/') . '/' . ltrim($path, '/');
return $path;
}
public function getAllFile()
{
return $this->scanFolder($this->getRoot());
}
private function scanFolder($dir, &$stack = [])
{
foreach (scandir($dir) as $file) {
if ($file == '.' || $file == '..')
continue;
$absolute_path = strtolower(str_replace('\\', '/', (rtrim($dir, '/') . '/' . $file)));
if (is_dir($absolute_path) && !is_file($absolute_path)) {
$this->scanFolder($absolute_path, $stack);
} elseif (is_file($absolute_path)) {
$server_root = strtolower(str_replace('\\', '/', strtolower($_SERVER['DOCUMENT_ROOT'])));
$relative_path = str_replace($server_root, '', $absolute_path);
$info = pathinfo($absolute_path);
$file = [
'name' => $info['basename'],
'extension' => $info['extension'],
'size' => $info['size'] ?? filesize($absolute_path),
'path' => $relative_path,
'parent' => basename(str_replace("/" . basename($absolute_path), '', $absolute_path)),
"last_modified" => date('Y-m-d H:i:s', filemtime($absolute_path)),
];
$stack[] = $file;
}
}
return $stack;
}
function absoluteToRelativePath($absolute_path)
{
$server_root = strtolower(str_replace('\\', '/', strtolower($_SERVER['DOCUMENT_ROOT'])));
$absolute_path = strtolower(str_replace('\\', '/', $absolute_path));
return str_replace($server_root, '', $absolute_path);
}
function isDirectoryNotEmpty($dir)
{
if (!is_readable($dir))
return null; // Check if directory is readable
$handle = opendir($dir);
if ($handle) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
closedir($handle);
return true; // Directory is not empty
}
}
closedir($handle);
}
return false; // Directory is empty
}
}