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
Loading
Loading
Loading
Loading
Loading
+1 −8
Original line number Diff line number Diff line
@@ -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
+1 −1
Original line number Diff line number Diff line
@@ -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
+31 −0
Original line number Diff line number Diff line
<?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()
        );
    }
}