koel/app/Helpers.php

74 lines
1.8 KiB
PHP
Raw Normal View History

2020-06-13 14:19:24 +02:00
<?php
use Illuminate\Support\Facades\File as FileFacade;
2022-08-08 18:00:59 +02:00
use Illuminate\Support\Facades\Log;
2020-12-23 00:01:49 +01: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.
*
* @param string $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));
}
2020-06-13 14:19:24 +02:00
2022-07-18 13:00:37 +02:00
function album_cover_path(?string $fileName): ?string
2020-09-06 23:20:42 +02:00
{
2022-07-18 13:00:37 +02:00
return $fileName ? public_path(config('koel.album_cover_dir') . $fileName) : null;
2020-06-13 14:19:24 +02:00
}
2022-07-18 13:00:37 +02:00
function album_cover_url(?string $fileName): ?string
2020-09-06 23:20:42 +02:00
{
2022-07-18 13:00:37 +02:00
return $fileName ? static_url(config('koel.album_cover_dir') . $fileName) : null;
2020-06-13 14:19:24 +02:00
}
2022-07-18 13:00:37 +02:00
function artist_image_path(?string $fileName): ?string
2020-09-06 23:20:42 +02:00
{
2022-07-18 13:00:37 +02:00
return $fileName ? public_path(config('koel.artist_image_dir') . $fileName) : null;
2020-06-13 14:19:24 +02:00
}
2022-07-18 13:00:37 +02:00
function artist_image_url(?string $fileName): ?string
2020-09-06 23:20:42 +02:00
{
2022-07-18 13:00:37 +02:00
return $fileName ? static_url(config('koel.artist_image_dir') . $fileName) : null;
2020-06-13 14:19:24 +02:00
}
function koel_version(): string
{
return trim(FileFacade::get(base_path('.version')));
}
2022-08-08 18:00:59 +02:00
/**
* @throws Throwable
*/
2022-08-08 18:00:59 +02:00
function attempt(callable $callback, bool $log = true): mixed
{
try {
return $callback();
} catch (Throwable $e) {
if (app()->runningUnitTests()) {
throw $e;
}
2022-08-08 18:00:59 +02: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;
}