Skip to content

[9.x] Added scoped filesystem driver - #44105

Merged
taylorotwell merged 5 commits into
laravel:9.xfrom
frankdejonge:feature/prefixed-filesystem-adapter
Sep 13, 2022
Merged

[9.x] Added scoped filesystem driver#44105
taylorotwell merged 5 commits into
laravel:9.xfrom
frankdejonge:feature/prefixed-filesystem-adapter

Conversation

@frankdejonge

@frankdejonge frankdejonge commented Sep 12, 2022

Copy link
Copy Markdown
Contributor

Often, files are placed on a single filesystem (like S3) but at different paths. This base path needs to be kept in sync all across the codebase. The scoped driver makes it easy to re-use disk configurations, and decorated the internal adapter.

Consider the following situation, when I have an existing s3 disk:

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
            'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
            'throw' => false,
        ],

Using the new scoped driver, I can create a version of the existing s3 driver with a deeper base path:

        's3_videos' => [
            'driver' => 'scoped',
            'prefix' => 'path/for/videos',
            'disk' => 's3',
        ],

Possible optimisation, since this is a decorated adapter, we could re-use the inner adapter. I didn't find an elegant way to do so, but I'm open to suggestions.

@taylorotwell
taylorotwell merged commit de7c408 into laravel:9.x Sep 13, 2022
@frankdejonge
frankdejonge deleted the feature/prefixed-filesystem-adapter branch September 13, 2022 15:22
@iotron

iotron commented Sep 27, 2022

Copy link
Copy Markdown

@frankdejonge Hi, I cannot get the correct URL for the scoped drivers with $storage->url(). The prefix is missing as it's not implemented in the url function in class FilesystemAdapter.

    /**
     * Get the URL for the file at the given path.
     *
     * @param  string  $path
     * @return string
     *
     * @throws \RuntimeException
     */
    public function url($path)
    {
        $adapter = $this->adapter;

        if (method_exists($adapter, 'getUrl')) {
            return $adapter->getUrl($path);
        } elseif (method_exists($this->driver, 'getUrl')) {
            return $this->driver->getUrl($path);
        } elseif ($adapter instanceof FtpAdapter || $adapter instanceof SftpAdapter) {
            return $this->getFtpUrl($path);
        } elseif ($adapter instanceof LocalAdapter) {
            return $this->getLocalUrl($path);
        } else {
            throw new RuntimeException('This driver does not support retrieving URLs.');
        }
    }

@iotron

iotron commented Sep 27, 2022

Copy link
Copy Markdown

I have created an issue with details.
#44327

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants