-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.php
More file actions
220 lines (175 loc) · 6.08 KB
/
Copy pathFileManager.php
File metadata and controls
220 lines (175 loc) · 6.08 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
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
<?php
namespace Pet\File;
use Pet\File\Exception\FileException;
class FileManager
{
private static ?self $instance = null;
private array $disks = [];
public function __construct()
{
$this->disks['local'] = new Storage();
}
public static function instance(): self
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public static function disk(?string $name = null): Storage
{
$manager = self::instance();
$name = $name ?? 'local';
if (!isset($manager->disks[$name])) {
$manager->disks[$name] = Storage::disk($name);
}
return $manager->disks[$name];
}
public static function registerDisk(string $name, Storage $storage): void
{
self::instance()->disks[$name] = $storage;
}
public static function temp(string $content = '', ?string $extension = null): File
{
$dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'pet';
$name = bin2hex(random_bytes(8)) . ($extension ? '.' . $extension : '');
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
throw FileException::directoryNotCreated($dir);
}
$path = $dir . DIRECTORY_SEPARATOR . $name;
if ($content !== '') {
file_put_contents($path, $content);
}
return new File($path);
}
public static function ensureDirectory(string $path): string
{
if (!is_dir($path) && !mkdir($path, 0755, true) && !is_dir($path)) {
throw FileException::directoryNotCreated($path);
}
return $path;
}
public static function cleanDirectory(string $path): bool
{
if (!is_dir($path)) {
return false;
}
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($iterator as $item) {
if ($item->isDir()) {
rmdir($item->getPathname());
} else {
unlink($item->getPathname());
}
}
return true;
}
public static function copyDirectory(string $source, string $destination): bool
{
if (!is_dir($source)) {
throw FileException::notFound($source);
}
self::ensureDirectory($destination);
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $item) {
$destPath = $destination . DIRECTORY_SEPARATOR . $iterator->getSubPathname();
if ($item->isDir()) {
self::ensureDirectory($destPath);
} else {
copy($item->getPathname(), $destPath);
}
}
return true;
}
public static function glob(string $pattern, int $flags = 0): array
{
$files = glob($pattern, $flags);
if ($files === false) {
return [];
}
return array_map(fn(string $path) => new File($path), $files);
}
public static function find(string $directory, string $pattern): array
{
if (!is_dir($directory)) {
return [];
}
$files = [];
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS)
);
foreach ($iterator as $fileInfo) {
if ($fileInfo->isFile() && fnmatch($pattern, $fileInfo->getFilename())) {
$files[] = new File($fileInfo->getPathname());
}
}
return $files;
}
public static function humanSize(int $bytes): string
{
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
$i = 0;
while ($bytes >= 1024 && $i < count($units) - 1) {
$bytes /= 1024;
$i++;
}
return round($bytes, 2) . ' ' . $units[$i];
}
public static function sanitizeFilename(string $name): string
{
$name = preg_replace('/[^\w\.\-]/u', '_', $name);
$name = preg_replace('/_{2,}/', '_', $name);
return trim($name, '._-');
}
public static function uniqueFilename(string $directory, string $name): string
{
$path = $directory . DIRECTORY_SEPARATOR . $name;
$info = pathinfo($path);
$i = 1;
while (file_exists($path)) {
$path = $info['dirname'] . DIRECTORY_SEPARATOR
. $info['filename'] . '_' . $i . '.'
. ($info['extension'] ?? '');
$i++;
}
return basename($path);
}
public static function extension(string $mimeType): string
{
$map = [
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/gif' => 'gif',
'image/webp' => 'webp',
'image/svg+xml' => 'svg',
'application/pdf' => 'pdf',
'application/zip' => 'zip',
'application/x-rar-compressed' => 'rar',
'application/gzip' => 'gz',
'text/plain' => 'txt',
'text/html' => 'html',
'text/css' => 'css',
'text/javascript' => 'js',
'application/json' => 'json',
'application/xml' => 'xml',
'application/msword' => 'doc',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
'application/vnd.ms-excel' => 'xls',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
'application/vnd.ms-powerpoint' => 'ppt',
'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
'text/csv' => 'csv',
'audio/mpeg' => 'mp3',
'audio/ogg' => 'ogg',
'video/mp4' => 'mp4',
'video/webm' => 'webm',
];
return $map[$mimeType] ?? 'bin';
}
}