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

Forms: FileUpload - Image preview on subdomain #12648

Closed
wants to merge 3 commits into from

Conversation

camya
Copy link
Contributor

@camya camya commented May 6, 2024

Description

This PR for the FileUpload field adds support for image previews on subdomains.

In a multi-tenant application, tenants have their subdomains, e.g. "https://tenent1.site.test".
If a user on this subdomain uploads an image in a filament-form, the preview will not work because the URL from APP_URL is used, e.g. "https://www.site.test".

The method ->getUploadedFileUrlUsing(fn (string $file): string => url("/{$file}")) allows to set the current domain, instead of the one stored in APP_URL.

From the updated documentation: "Set a different base URL"

By default, uploaded files are served from the domain specified in the APP_URL set in your .env file. If your multitenant application uses subdomains, you can set the current subdomain URL using the `getUploadedFileUrlUsing' callback.

Filament\Forms\Components\FileUpload;

FileUpload::make('image')
    ->getUploadedFileUrlUsing(fn (string $file): string => url("/{$file}"))

To correctly preview images and other files, FilePond requires files to be served from the same domain as the app, or the appropriate CORS headers need to be present. If you're hosting files on a separate CDN domain like S3, ensure that CORS headers are set up.

Functional changes

  • Code style has been fixed by running the composer cs command.
  • Changes have been tested to not break existing functionality.
  • Documentation is up-to-date.

@camya camya changed the title Forms: Option to set a different base URL for subdomains Forms: FileUpload - Image preview on subdomain May 6, 2024
@camya
Copy link
Contributor Author

camya commented May 6, 2024

Maybe it's better to simplify it more to ->getUploadedFileDomainUsing(fn (): string => url('/'))

The filename is not needed here. What do you think?

@danharrin
Copy link
Member

I think the current solution is good and changes are not necessary, thanks

@danharrin
Copy link
Member

Before I merge this, I just wanted to check to see if there is an easier solution. You're using the public disk, right? Isn't it just easier to config()->set('filesystems.public.url', $tenantUrl), or set the entire app.url in middleware? Then we don't need to implement this in Filament @camya

@danharrin danharrin added the enhancement New feature or request label May 6, 2024
@danharrin danharrin added this to the v3 milestone May 6, 2024
@danharrin danharrin self-assigned this May 6, 2024
@camya
Copy link
Contributor Author

camya commented May 6, 2024

You're right. Your middleware idea works fine. You just missed the disc segment in the dot path. :)

Guess, this config()->set('filesystems.disks.public.url', url("/storage")); info should be added to the documentation too.

class SubdomainMiddleware
{
    public function handle(Request $request, Closure $next): Response
    {
        // Handle subdomain ...

        config()->set('filesystems.disks.public.url', url("/storage"));

        return $next($request);
    }
}

@danharrin
Copy link
Member

To be honest, I disagree there, it's not really a concern of Filament how you have your filesystems set up, so I don't think it's our responsibility to document

@danharrin danharrin closed this May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants