Skip to content

Commit 1836ada

Browse files
Use Laravel Filesystem URL Generator for Images (#621)
1 parent 8bd44d2 commit 1836ada

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

backend/app/Helper/Url.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,26 @@ public static function getApiUrl(string $path, array $queryParams = []): string
2929
return self::addQueryParamsToUrl($queryParams, $url);
3030
}
3131

32+
/**
33+
* Generates a CDN URL for the given path if a CDN URL is configured.
34+
* Falls back to generating a URL using the specified or default filesystem disk.
35+
*
36+
* @param string $path The relative path to the asset.
37+
* @return string The fully qualified URL to the asset, either via CDN or storage disk.
38+
*/
3239
public static function getCdnUrl(string $path): string
3340
{
34-
return config('app.cnd_url') . '/' . $path;
41+
// Fetch the CDN URL from environment variables
42+
// Checking against the env variable instead of config() as config falls back to the default value
43+
// and we want to ensure that if the env variable is not set, we do not use a default value.
44+
$envCDNUrl = env('APP_CDN_URL');
45+
46+
if ($envCDNUrl) {
47+
return $envCDNUrl . '/' . $path;
48+
}
49+
50+
$disk = config('filesystems.public', 'public');
51+
return app('filesystem')->disk($disk)->url($path);
3552
}
3653

3754
private static function addQueryParamsToUrl(array $queryParams, mixed $url): mixed

0 commit comments

Comments
 (0)