Skip to content
Snippets Groups Projects
Commit 502f53c9 authored by Yassine Doghri's avatar Yassine Doghri
Browse files

fix(s3): serve files using media base url to allow for CDN setup

parent c5a13592
No related branches found
No related tags found
No related merge requests found
Pipeline #12462 failed
......@@ -57,14 +57,7 @@ class FS implements FileManagerInterface
public function getUrl(string $key): string
{
$appConfig = config('App');
$mediaBaseUrl = $this->config->baseURL === '' ? $appConfig->baseURL : $this->config->baseURL;
return rtrim((string) $mediaBaseUrl, '/') .
'/' .
$this->config->root .
'/' .
$key;
return media_url($this->config->root . '/' . $key);
}
public function rename(string $oldKey, string $newKey): bool
......
......@@ -64,7 +64,7 @@ class S3 implements FileManagerInterface
public function getUrl(string $key): string
{
return url_to('media-serve', $key);
return media_url((string) route_to('media-serve', $key));
}
public function rename(string $oldKey, string $newKey): bool
......
<?php
declare(strict_types=1);
use CodeIgniter\HTTP\URI;
use Modules\Media\Config\Media;
if (! function_exists('media_url')) {
/**
* Returns a media URL as defined by the Media config.
*
* @param array|string $relativePath URI string or array of URI segments
*/
function media_url($relativePath = '', ?string $scheme = null): string
{
// Convert array of segments to a string
if (is_array($relativePath)) {
$relativePath = implode('/', $relativePath);
}
$uri = new URI(rtrim((string) config(Media::class)->baseURL, '/') . '/' . ltrim($relativePath));
return URI::createURIString(
$scheme ?? $uri->getScheme(),
$uri->getAuthority(),
$uri->getPath(),
$uri->getQuery(),
$uri->getFragment()
);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment