koel/app/Helpers.php

151 lines
3.8 KiB
PHP
Raw Permalink Normal View History

2020-06-13 12:19:24 +00:00
<?php
2024-03-30 16:49:25 +00:00
use App\Facades\License;
use Illuminate\Support\Facades\File as FileFacade;
2022-08-08 16:00:59 +00:00
use Illuminate\Support\Facades\Log;
2024-03-19 22:48:12 +00:00
use Illuminate\Support\Str;
2022-08-08 16:00:59 +00:00
2020-12-22 23:01:49 +00:00
/**
* 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.
*
2024-02-24 15:37:01 +00:00
* @param string|null $name The optional resource name/path
2020-12-22 23:01:49 +00:00
*/
function static_url(?string $name = null): string
{
$cdnUrl = trim(config('koel.cdn.url'), '/ ');
return $cdnUrl ? $cdnUrl . '/' . trim(ltrim($name, '/')) : trim(asset($name));
}
2020-06-13 12:19:24 +00:00
2022-07-18 11:00:37 +00:00
function album_cover_path(?string $fileName): ?string
2020-09-06 21:20:42 +00:00
{
2022-07-18 11:00:37 +00:00
return $fileName ? public_path(config('koel.album_cover_dir') . $fileName) : null;
2020-06-13 12:19:24 +00:00
}
2022-07-18 11:00:37 +00:00
function album_cover_url(?string $fileName): ?string
2020-09-06 21:20:42 +00:00
{
2022-07-18 11:00:37 +00:00
return $fileName ? static_url(config('koel.album_cover_dir') . $fileName) : null;
2020-06-13 12:19:24 +00:00
}
2022-07-18 11:00:37 +00:00
function artist_image_path(?string $fileName): ?string
2020-09-06 21:20:42 +00:00
{
2022-07-18 11:00:37 +00:00
return $fileName ? public_path(config('koel.artist_image_dir') . $fileName) : null;
2020-06-13 12:19:24 +00:00
}
2022-07-18 11:00:37 +00:00
function artist_image_url(?string $fileName): ?string
2020-09-06 21:20:42 +00:00
{
2022-07-18 11:00:37 +00:00
return $fileName ? static_url(config('koel.artist_image_dir') . $fileName) : null;
2020-06-13 12:19:24 +00:00
}
2024-02-24 15:37:01 +00:00
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;
}
2024-03-19 22:48:12 +00:00
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')));
}
2022-08-08 16:00:59 +00:00
/**
* @throws Throwable
*/
2024-01-05 16:42:50 +00:00
function attempt(callable $callback, bool $log = true, bool $throw = false): mixed
2022-08-08 16:00:59 +00:00
{
try {
return $callback();
} catch (Throwable $e) {
2024-01-05 16:42:50 +00:00
if (app()->runningUnitTests() || $throw) {
throw $e;
}
2022-08-08 16:00:59 +00:00
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;
}
2024-01-18 11:13:05 +00:00
function gravatar(string $email, int $size = 192): string
{
2024-03-19 22:48:12 +00:00
return sprintf("https://www.gravatar.com/avatar/%s?s=$size&d=robohash", md5(Str::lower($email)));
2024-01-18 11:13:05 +00:00
}
2024-02-25 19:32:53 +00:00
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);
}
2024-02-25 19:32:53 +00:00
/**
* 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);
}
2024-03-30 16:49:25 +00:00
/** @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;
}
2024-06-04 13:35:00 +00:00
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();
}