-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathFiles.php
More file actions
177 lines (144 loc) · 3.69 KB
/
Files.php
File metadata and controls
177 lines (144 loc) · 3.69 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
<?php
namespace Utopia\Http;
use Exception;
class Files
{
/**
* @var array<string, mixed>
*/
protected array $loaded = [];
protected int $count = 0;
/**
* @var array<string, mixed>
*/
protected array $mimeTypes = [];
/**
* @var array<string, mixed>
*/
public const array EXTENSIONS = [
'css' => 'text/css',
'js' => 'text/javascript',
'svg' => 'image/svg+xml',
];
/**
* Add MIME type.
*/
public function addMimeType(string $mimeType): void
{
$this->mimeTypes[$mimeType] = true;
}
/**
* Remove MIME type.
*/
public function removeMimeType(string $mimeType): void
{
if (isset($this->mimeTypes[$mimeType])) {
unset($this->mimeTypes[$mimeType]);
}
}
/**
* Get MimeType List
*
* @return array<string, mixed>
*/
public function getMimeTypes(): array
{
return $this->mimeTypes;
}
/**
* Get Files Loaded Count
*/
public function getCount(): int
{
return $this->count;
}
/**
* Load directory.
*
*
* @throws \Exception
*/
public function load(string $directory, ?string $root = null): void
{
if (!is_readable($directory)) {
throw new Exception("Failed to load directory: {$directory}");
}
$directory = realpath($directory);
$root ??= $directory;
$handle = opendir(\strval($directory));
if ($handle === false) {
throw new Exception("Failed to open directory: {$directory}");
}
while ($path = readdir($handle)) {
$extension = pathinfo($path, PATHINFO_EXTENSION);
if (\in_array($path, ['.', '..'])) {
continue;
}
if (\in_array($extension, ['php', 'phtml'])) {
continue;
}
if (str_starts_with($path, '.')) {
continue;
}
$dirPath = $directory . '/' . $path;
if (is_dir($dirPath)) {
$this->load($dirPath, \strval($root));
continue;
}
$key = substr($dirPath, \strlen(\strval($root)));
if (\array_key_exists($key, $this->loaded)) {
continue;
}
$this->loaded[$key] = [
'contents' => file_get_contents($dirPath),
'mimeType' => (\array_key_exists($extension, self::EXTENSIONS))
? self::EXTENSIONS[$extension]
: mime_content_type($dirPath),
];
$this->count++;
}
closedir($handle);
}
/**
* Is file loaded.
*/
public function isFileLoaded(string $uri): bool
{
return \array_key_exists($uri, $this->loaded);
}
/**
* Get file contents.
*
* @return string
* @throws \Exception
*/
public function getFileContents(string $uri): mixed
{
if (!\array_key_exists($uri, $this->loaded)) {
throw new Exception('File not found or not loaded: ' . $uri);
}
return $this->loaded[$uri]['contents'];
}
/**
* Get file MIME type.
*
* @return string
* @throws \Exception
*/
public function getFileMimeType(string $uri): mixed
{
if (!\array_key_exists($uri, $this->loaded)) {
throw new Exception('File not found or not loaded: ' . $uri);
}
return $this->loaded[$uri]['mimeType'];
}
/**
* Reset.
*/
public function reset(): void
{
$this->count = 0;
$this->loaded = [];
$this->mimeTypes = [];
}
}