|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Support\Facades\Storage; |
|
4
|
|
|
|
|
5
|
1 |
|
if (!function_exists('s3Endpoint')) { |
|
6
|
|
|
/** |
|
7
|
|
|
* @param string $disk |
|
8
|
|
|
* @return string |
|
9
|
|
|
*/ |
|
10
|
1 |
|
function s3Endpoint($disk = 'libraries') |
|
11
|
|
|
{ |
|
12
|
|
|
// if a custom s3 endpoint is configured explicitly, return it |
|
13
|
|
|
$customEndpoint = config("filesystems.disks.{$disk}.endpoint"); |
|
14
|
|
|
|
|
15
|
|
|
if ($customEndpoint) { |
|
16
|
|
|
return $customEndpoint; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
$scheme = config("filesystems.disks.{$disk}.use_https") ? 'https://' : ''; |
|
20
|
|
|
return $scheme . config("filesystems.disks.{$disk}.bucket") . '.' . Storage::disk($disk)->getAdapter()->getClient()->getEndpoint()->getHost(); |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
1 |
|
if (!function_exists('azureEndpoint')) { |
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $disk |
|
27
|
|
|
* @return string |
|
28
|
|
|
*/ |
|
29
|
1 |
|
function azureEndpoint($disk = 'libraries') |
|
30
|
|
|
{ |
|
31
|
|
|
$scheme = config("filesystems.disks.{$disk}.use_https") ? 'https://' : ''; |
|
32
|
|
|
return $scheme . config("filesystems.disks.{$disk}.name") . '.blob.' . config("filesystems.disks.{$disk}.endpoint-suffix") . '/' . config("filesystems.disks.{$disk}.container"); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
if (!function_exists('bytesToHuman')) { |
|
37
|
|
|
/** |
|
38
|
|
|
* @param float $bytes |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
1 |
|
function bytesToHuman($bytes) |
|
42
|
|
|
{ |
|
43
|
3 |
|
$units = ['B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb']; |
|
44
|
|
|
|
|
45
|
3 |
|
for ($i = 0; $bytes > 1024; $i++) { |
|
46
|
3 |
|
$bytes /= 1024; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
3 |
|
return round($bytes, 2) . ' ' . $units[$i]; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
if (!function_exists('cyrillicToLatin')) { |
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $str |
|
56
|
|
|
* @return bool|string |
|
57
|
|
|
*/ |
|
58
|
1 |
|
function cyrillicToLatin($str) |
|
59
|
|
|
{ |
|
60
|
|
|
$tr = [ |
|
61
|
|
|
// Russian |
|
62
|
3 |
|
"А" => "a", "Б" => "b", "В" => "v", "Г" => "g", "Д" => "d", |
|
63
|
|
|
"Е" => "e", "Ё" => "yo", "Ж" => "zh", "З" => "z", "И" => "i", |
|
64
|
|
|
"Й" => "j", "К" => "k", "Л" => "l", "М" => "m", "Н" => "n", |
|
65
|
|
|
"О" => "o", "П" => "p", "Р" => "r", "С" => "s", "Т" => "t", |
|
66
|
|
|
"У" => "u", "Ф" => "f", "Х" => "kh", "Ц" => "ts", "Ч" => "ch", |
|
67
|
|
|
"Ш" => "sh", "Щ" => "sch", "Ъ" => "", "Ы" => "y", "Ь" => "", |
|
68
|
|
|
"Э" => "e", "Ю" => "yu", "Я" => "ya", "а" => "a", "б" => "b", |
|
69
|
|
|
"в" => "v", "г" => "g", "д" => "d", "е" => "e", "ё" => "yo", |
|
70
|
|
|
"ж" => "zh", "з" => "z", "и" => "i", "й" => "j", "к" => "k", |
|
71
|
|
|
"л" => "l", "м" => "m", "н" => "n", "о" => "o", "п" => "p", |
|
72
|
|
|
"р" => "r", "с" => "s", "т" => "t", "у" => "u", "ф" => "f", |
|
73
|
|
|
"х" => "kh", "ц" => "ts", "ч" => "ch", "ш" => "sh", "щ" => "sch", |
|
74
|
|
|
"ъ" => "", "ы" => "y", "ь" => "", "э" => "e", "ю" => "yu", |
|
75
|
|
|
"я" => "ya", |
|
76
|
|
|
// Ukrainian |
|
77
|
|
|
'Ґ' => 'G', 'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'ґ' => 'g', |
|
78
|
|
|
'є' => 'ie', 'і' => 'i', 'ї' => 'i', 'і́' => 'i', |
|
79
|
|
|
// Kazakh |
|
80
|
|
|
'Ә' => 'A', 'Ғ' => 'G', 'Қ' => 'Q', 'Ң' => 'N', 'Ө' => 'O', |
|
81
|
|
|
'Ұ' => 'U', 'Ү' => 'U', 'Һ' => 'H', 'І' => 'I', 'ә' => 'a', |
|
82
|
|
|
'ғ' => 'g', 'қ' => 'q', 'ң' => 'n', 'ө' => 'o', 'ұ' => 'u', |
|
83
|
|
|
'ү' => 'u', 'һ' => 'h', 'і' => 'i', |
|
84
|
|
|
]; |
|
85
|
|
|
|
|
86
|
3 |
|
return strtr($str, $tr); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
if (!function_exists('replaceAccents')) { |
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $str |
|
93
|
|
|
* @return bool|string |
|
94
|
|
|
*/ |
|
95
|
1 |
|
function replaceAccents($str) |
|
96
|
|
|
{ |
|
97
|
3 |
|
return iconv('UTF-8', 'ASCII//TRANSLIT', cyrillicToLatin($str)); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
if (!function_exists('sanitizeFilename')) { |
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $filename |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
1 |
|
function sanitizeFilename($filename) |
|
107
|
|
|
{ |
|
108
|
3 |
|
$sanitizedFilename = replaceAccents($filename); |
|
109
|
|
|
|
|
110
|
|
|
$invalid = array( |
|
111
|
3 |
|
' ' => '-', |
|
112
|
|
|
'%20' => '-', |
|
113
|
|
|
'_' => '-', |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
3 |
|
$sanitizedFilename = str_replace(array_keys($invalid), array_values($invalid), $sanitizedFilename); |
|
117
|
|
|
|
|
118
|
3 |
|
$sanitizedFilename = preg_replace('/[^A-Za-z0-9-\. ]/', '', $sanitizedFilename); // Remove all non-alphanumeric except . |
|
119
|
3 |
|
$sanitizedFilename = preg_replace('/\.(?=.*\.)/', '', $sanitizedFilename); // Remove all but last . |
|
120
|
3 |
|
$sanitizedFilename = preg_replace('/-+/', '-', $sanitizedFilename); // Replace any more than one - in a row |
|
121
|
3 |
|
$sanitizedFilename = str_replace('-.', '.', $sanitizedFilename); // Remove last - if at the end |
|
122
|
3 |
|
$sanitizedFilename = strtolower($sanitizedFilename); // Lowercase |
|
123
|
|
|
|
|
124
|
3 |
|
return $sanitizedFilename; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|