Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Path Prefixing #53

Open
iMusicJJ opened this issue Nov 24, 2022 · 8 comments
Open

[Question] Path Prefixing #53

iMusicJJ opened this issue Nov 24, 2022 · 8 comments

Comments

@iMusicJJ
Copy link

Hello!

How do you set-up the path prefixing?

I've attempted to set it up with Laravel, and it uploads the files as expected in the prefixed path, but upon URL retrieval it throws:
"This driver does not support retrieving URLs.",

Do you have an example with a prefixed path?

@sifex
Copy link
Collaborator

sifex commented Nov 24, 2022

Hey, sure thing,

Should be as simple as wrapping your BunnyCDNAdapter inside of a PathPrefixAdapter

Edit 2: Yea sorry you also have to composer require league/flysystem-path-prefixing:^3.3

So update

$adapter = new BunnyCDNAdapter(
    new BunnyCDNClient(
        $config['storage_zone'],
        $config['api_key'],
        $config['region']
    ),
    'http://testing.b-cdn.net' # Optional
);

to be

$adapter = new PathPrefixedAdapter(
    new BunnyCDNAdapter(
        new BunnyCDNClient(
            $config['storage_zone'],
            $config['api_key'],
            $config['region']
        ),
        'http://testing.b-cdn.net' # Optional
    ),
    'prefix'
);

(Disclaimer) I wrote it by hand so no idea if it works. It's got test coverage though

Source: https://flysystem.thephpleague.com/docs/adapter/path-prefixing/

@iMusicJJ
Copy link
Author

iMusicJJ commented Nov 24, 2022

Funky, that is what I have; I believe.

$adapter = new BunnyCDNAdapter(
    new BunnyCDNClient(
        $config['storage_zone'],
        $config['api_key'],
        $config['region']
    ),
    $config['pullzone_url'] ?? null
);

$adapter = new PathPrefixedAdapter($adapter, 'images/mails');

Which is what throws the exception when trying to generate the URL.

Full example:

Storage::extend('bunnycdn-mail', function (Application $app, array $config) {
            $adapter = new BunnyCDNAdapter(
                new BunnyCDNClient(
                    $config['storage_zone'],
                    $config['api_key'],
                    $config['region']
                ),
                $config['pullzone_url'] ?? null
            );

            $adapter = new PathPrefixedAdapter($adapter, 'images/mails');

            return new FilesystemAdapter(
                new Filesystem($adapter, $config),
                $adapter,
                $config);
        });

@sifex
Copy link
Collaborator

sifex commented Nov 24, 2022

Let me have a quick look now for ya

@sifex
Copy link
Collaborator

sifex commented Nov 24, 2022

So you're not going to be able to get nice IDE completion, but there is a fall-through for calling adapter methods on the Storage facade.

Screenshot 2022-11-24 at 5 46 39 pm

tldr; You can call Storage::publicUrl instead of Storage::url and that seems to work for the moment.

@iMusicJJ
Copy link
Author

iMusicJJ commented Nov 24, 2022

Oohhh, this is where it gets funky.
I don't have access to the Storage::url call, it's happening inside mailcoach

I am trying to store email images on Bunny, instead of locally.

@sifex
Copy link
Collaborator

sifex commented Nov 24, 2022

Huh.... Well.

Because PathPrefixedAdapter doesn't have a getUrl method (I think v3 changed the method name), it's gonna throw "This driver does not support retrieving URLs.".

Screenshot 2022-11-24 at 6 01 18 pm

The quick & dirty:

Best way I'm going to suggest is to extend the PathPrefixAdapter with a getUrl method.

# /app/Providers/Support/PathPrefixedAdapter.php

<?php

namespace App\Providers\Support;

use League\Flysystem\Config;

class PathPrefixedAdapter extends \League\Flysystem\PathPrefixing\PathPrefixedAdapter
{
    public function getUrl($path): string
    {
        return $this->publicUrl($path, new Config());
    }
}

Then use that PathPrefixedAdapter instead of the one provided by \League\Flysystem

The actual fix:

src/Illuminate/Filesystem/FilesystemAdapter.php needs a lot of attention given the advances in Flysystem v3

@sifex
Copy link
Collaborator

sifex commented Nov 24, 2022

I'll open a quick PR for Laravel now

@iMusicJJ
Copy link
Author

Ah great! Thanks for the amazingly quick response!

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

No branches or pull requests

2 participants