-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormat.php
More file actions
executable file
·109 lines (92 loc) · 3.27 KB
/
Format.php
File metadata and controls
executable file
·109 lines (92 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
<?php
require_once "Init.php";
require_once "Log.php";
/**
* Check and Safety the Path, Return the Safety Path.
*/
function SafePath(string $Path, array $Options = null): array {
$DftOpts = [
"MaxLength" => 50,
"ForceReplace" => []
];
$Options = MergeDictionaries($DftOpts, $Options);
$Response = [
"Ec" => 0,
"Em" => "",
"Path" => ""
];
try {
foreach ($Options["ForceReplace"] as $_Rule) {
if (is_array($_Rule) && count($_Rule) == 2) {
if (is_string($_Rule[0]) && is_string($_Rule[1])) {
$Path = str_replace($_Rule[0], $_Rule[1], $Path);
}
}
}
$Path = preg_replace("/\s+/", " ", preg_replace("/[\\/:*?\"<>|\n]/", " ", $Path));
$Response["Path"] = trim(substr(ltrim($Path), 0, $Options["MaxLength"]));
} catch (Exception $Error) {
$Response["Ec"] = 50000; $Response["Em"] = MakeErrorMessage($Error); return $Response;
}
return $Response;
}
/**
* Convert the Size to Human Readable Format, Support Magic Variables for Storage Unit.
*/
function ConvertSize(int $Size, string $Unit = "B", array $Options = null): array {
$DftOpts = [
"Format" => "{{Size}} {{Unit}}",
"Place_After_Decimal_Point" => 2
];
$Options = MergeDictionaries($DftOpts, $Options);
$Response = [
"Ec" => 0,
"Em" => "",
"Result" => ""
];
$Units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
$Weights = array_combine($Units, range(0, count($Units) - 1));
$Sizes = array_fill_keys($Units, -1);
try {
if (!is_int($Size) && !is_float($Size)) {
throw new TypeError("Size Must be Int or Float");
}
if ($Size < 0) {
throw new ValueError("Size Must be Positive");
}
if (!in_array($Unit, $Units)) {
throw new ValueError("Unit Must be in B, KB, MB, GB, TB or PB");
}
} catch (Exception $Error) {
$Response["Ec"] = 50001; $Response["Em"] = MakeErrorMessage($Error); return $Response;
}
try {
$SizeInBytes = $Size * (1024 ** $Weights[$Unit]);
foreach ($Units as $Unit) {
$Sizes[$Unit] = $SizeInBytes / (1024 ** $Weights[$Unit]);
}
AutoUnit = 'B';
for ($i = count($Units) - 1; $i >= 1; $i--) {
$Unit = $Units[$i];
if ($Sizes[$Unit] >= 1) {
AutoUnit = $Unit;
break;
}
}
} catch (Exception $Error) {
$Response["Ec"] = 50002; $Response["Em"] = MakeErrorMessage($Error); return $Response;
}
try {
$Result = str_replace("{{Size}}", number_format($Sizes[$AutoUnit], $Options["Place_After_Decimal_Point"]), $Options["Format"]);
$Result = str_replace("{{Unit}}", $AutoUnit, $Result);
foreach ($Units as $Unit) {
$Result = str_replace("{{Size:$Unit}}", number_format($Sizes[$Unit], $Options["Place_After_Decimal_Point"]), $Result);
$Result = str_replace("{{Unit:$Unit}}", $Unit, $Result);
}
$Response["Result"] = $Result;
} catch (Exception $Error) {
$Response["Ec"] = 50003; $Response["Em"] = MakeErrorMessage($Error); return $Response;
}
return $Response;
}
?>