-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathFiles.php
More file actions
200 lines (167 loc) · 4.01 KB
/
Files.php
File metadata and controls
200 lines (167 loc) · 4.01 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
<?php
namespace Utopia\Http;
use Exception;
class Files
{
/**
* @var array<string, mixed>
*/
protected array $loaded = [];
/**
* @var int
*/
protected int $count = 0;
/**
* @var array<string, mixed>
*/
protected array $mimeTypes = [];
/**
* @var array<string, mixed>
*/
public const EXTENSIONS = [
'css' => 'text/css',
'js' => 'text/javascript',
'svg' => 'image/svg+xml',
];
/**
* Add MIME type.
*
* @param string $mimeType
* @return void
*/
public function addMimeType(string $mimeType): void
{
$this->mimeTypes[$mimeType] = true;
}
/**
* Remove MIME type.
*
* @param string $mimeType
* @return void
*/
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
*
* @return int
*/
public function getCount(): int
{
return $this->count;
}
/**
* Load directory.
*
* @param string $directory
* @param string|null $root
* @return void
*
* @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));
while ($path = readdir($handle)) {
$extension = pathinfo($path, PATHINFO_EXTENSION);
if (in_array($path, ['.', '..'])) {
continue;
}
if (in_array($extension, ['php', 'phtml'])) {
continue;
}
if (substr($path, 0, 1) === '.') {
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.
*
* @param string $uri
* @return bool
*/
public function isFileLoaded(string $uri): bool
{
if (!array_key_exists($uri, $this->loaded)) {
return false;
}
return true;
}
/**
* Get file contents.
*
* @param string $uri
* @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.
*
* @param string $uri
* @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.
*
* @return void
*/
public function reset(): void
{
$this->count = 0;
$this->loaded = [];
$this->mimeTypes = [];
}
}