thephpleague / flysystem

Abstraction for local and remote filesystems

Home Page:https://flysystem.thephpleague.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implement publicUrl on LocalFilesystemAdapter

florentdestremau opened this issue · comments

Feature Request

Q A
Flysystem Version 3.16.0
Adapter Name LocalFilesystemAdapter
Adapter version 3.16.0

Scenario / Use-case

I want to use the interface FilesystemOperator from a Symfony app using https://github.com/1up-lab/OneupFlysystemBundle where I want to use "publicUrl". The method is listed in the interface's annotations, which in an IDE is confusing because we don't notice that it's not generally available. At the moment, only the AwsS3V3 adapter has implemented the method, and I'm wondering if in 3.x version it's possible to implement it in the LocalFilesystemAdapter too.

My use case is for a twig extension where I want to display an image in a <img> tag, I need the public URL. I'm using VichUploader for entities but sometimes it's just a zip i want to transfer...

It seems to me that a simple method such as this would suffice, as it's used all over the class for reading or manipulating the file:

public function publicUrl(string $path): string {
    return $this->prefixer->prefixPath($path);
}

The way to have public URLs for adapters that don't support it is to pass a PublicUrlGenerator into the Filesystem constructor, or to set a public_url configuration option, also on the Filesystem object.

I don't think I can configure the PublicUrlGenerator through my configuration file so that it's only overriden in local env, unless in my code I test for $appEnv = "dev" and define it on the fly which is not very durable.

For now my solution is this:

        try {
            $zipName = $this->filesFilesystem->publicUrl($name);
        } catch (\Exception) {
            $zipName = '/uploads/files/' . $name;
        }

As I'm retrieving my filesFilesystem (from Oneupbundle) already set, I can't override it with a proper api

You should ask the OneupBundle folks (lovely people) to support it. Shouldn't be too difficult to make a PR for it even.