-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.php
150 lines (122 loc) · 3.77 KB
/
Helpers.php
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
<?php
use App\Facades\License;
use Illuminate\Support\Facades\File as FileFacade;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
/**
* Get a URL for static file requests.
* If this installation of Koel has a CDN_URL configured, use it as the base.
* Otherwise, just use a full URL to the asset.
*
* @param string|null $name The optional resource name/path
*/
function static_url(?string $name = null): string
{
$cdnUrl = trim(config('koel.cdn.url'), '/ ');
return $cdnUrl ? $cdnUrl . '/' . trim(ltrim($name, '/')) : trim(asset($name));
}
function album_cover_path(?string $fileName): ?string
{
return $fileName ? public_path(config('koel.album_cover_dir') . $fileName) : null;
}
function album_cover_url(?string $fileName): ?string
{
return $fileName ? static_url(config('koel.album_cover_dir') . $fileName) : null;
}
function artist_image_path(?string $fileName): ?string
{
return $fileName ? public_path(config('koel.artist_image_dir') . $fileName) : null;
}
function artist_image_url(?string $fileName): ?string
{
return $fileName ? static_url(config('koel.artist_image_dir') . $fileName) : null;
}
function playlist_cover_path(?string $fileName): ?string
{
return $fileName ? public_path(config('koel.playlist_cover_dir') . $fileName) : null;
}
function playlist_cover_url(?string $fileName): ?string
{
return $fileName ? static_url(config('koel.playlist_cover_dir') . $fileName) : null;
}
function user_avatar_path(?string $fileName): ?string
{
return $fileName ? public_path(config('koel.user_avatar_dir') . $fileName) : null;
}
function user_avatar_url(?string $fileName): ?string
{
return $fileName ? static_url(config('koel.user_avatar_dir') . $fileName) : null;
}
function koel_version(): string
{
return trim(FileFacade::get(base_path('.version')));
}
/**
* @throws Throwable
*/
function attempt(callable $callback, bool $log = true, bool $throw = false): mixed
{
try {
return $callback();
} catch (Throwable $e) {
if (app()->runningUnitTests() || $throw) {
throw $e;
}
if ($log) {
Log::error('Failed attempt', ['error' => $e]);
}
return null;
}
}
function attempt_if($condition, callable $callback, bool $log = true): mixed
{
return value($condition) ? attempt($callback, $log) : null;
}
function attempt_unless($condition, callable $callback, bool $log = true): mixed
{
return !value($condition) ? attempt($callback, $log) : null;
}
function gravatar(string $email, int $size = 192): string
{
return sprintf("https://www.gravatar.com/avatar/%s?s=$size&d=robohash", md5(Str::lower($email)));
}
function avatar_or_gravatar(?string $avatar, string $email): string
{
if (!$avatar) {
return gravatar($email);
}
if (Str::startsWith($avatar, ['http://', 'https://'])) {
return $avatar;
}
return user_avatar_url($avatar);
}
/**
* A quick check to determine if a mailer is configured.
* This is not bulletproof but should work in most cases.
*/
function mailer_configured(): bool
{
return config('mail.default') && !in_array(config('mail.default'), ['log', 'array'], true);
}
/** @return array<string> */
function collect_sso_providers(): array
{
if (License::isCommunity()) {
return [];
}
$providers = [];
if (
config('services.google.client_id')
&& config('services.google.client_secret')
&& config('services.google.hd')
) {
$providers[] = 'Google';
}
return $providers;
}
function get_mtime(string|SplFileInfo $file): int
{
$file = is_string($file) ? new SplFileInfo($file) : $file;
// Workaround for #344, where getMTime() fails for certain files with Unicode names on Windows.
return attempt(static fn () => $file->getMTime()) ?? time();
}