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
|
|
|
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
|
|
|
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
|
|
|
function bytesToHuman($bytes) |
42
|
|
|
{ |
43
|
3 |
|
$units = ['B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb']; |
44
|
|
|
|
45
|
3 |
|
for ($i = 0; $bytes > 1024; $i++) { |
46
|
|
|
$bytes /= 1024; |
47
|
|
|
} |
48
|
|
|
|
49
|
3 |
|
return round($bytes, 2) . ' ' . $units[$i]; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
if (!function_exists('replaceAccents')) { |
54
|
|
|
/** |
55
|
|
|
* @param string $str |
56
|
|
|
* @return bool|string |
57
|
|
|
*/ |
58
|
|
|
function replaceAccents($str) |
59
|
|
|
{ |
60
|
3 |
|
return iconv('UTF-8', 'ASCII//TRANSLIT', $str); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
if (!function_exists('sanitizeFilename')) { |
65
|
|
|
/** |
66
|
|
|
* @param string $filename |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
function sanitizeFilename($filename) |
70
|
|
|
{ |
71
|
3 |
|
$sanitizedFilename = replaceAccents($filename); |
72
|
|
|
|
73
|
|
|
$invalid = array( |
74
|
3 |
|
' ' => '-', |
75
|
|
|
'%20' => '-', |
76
|
|
|
'_' => '-', |
77
|
|
|
); |
78
|
|
|
|
79
|
3 |
|
$sanitizedFilename = str_replace(array_keys($invalid), array_values($invalid), $sanitizedFilename); |
80
|
|
|
|
81
|
3 |
|
$sanitizedFilename = preg_replace('/[^A-Za-z0-9-\. ]/', '', $sanitizedFilename); // Remove all non-alphanumeric except . |
82
|
3 |
|
$sanitizedFilename = preg_replace('/\.(?=.*\.)/', '', $sanitizedFilename); // Remove all but last . |
83
|
3 |
|
$sanitizedFilename = preg_replace('/-+/', '-', $sanitizedFilename); // Replace any more than one - in a row |
84
|
3 |
|
$sanitizedFilename = str_replace('-.', '.', $sanitizedFilename); // Remove last - if at the end |
85
|
3 |
|
$sanitizedFilename = strtolower($sanitizedFilename); // Lowercase |
86
|
|
|
|
87
|
3 |
|
return $sanitizedFilename; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|