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

This commit is contained in:
Yassine Doghri 2023-04-14 09:33:52 +00:00
parent c5a1359218
commit 502f53c970
3 changed files with 33 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,31 @@
<?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()
);
}
}