Customizing Filament Export Action

Hello everyone, I would need some help with an issue I'm facing regarding the export action in Filament. Currently, I have a resource where I allow exporting using Filament's standard export action. After several attempts, I managed to get database notifications working as well, all very nice. Unfortunately, I can't figure out how to override/modify the link for downloading the file. I have a customized setup for the application's storage (it's not located internally but at a specific path on the Linux machine hosting my app). Using route:list, I noticed that there's a Filament route 'filament/exports/{id}/download' that is definitely used for returning the file to download, but I can't seem to access it or override it. Could you help me in some way? I could solve it in different ways; I just need to override the link with my custom one pointing to a route I created (already existing and working for other downloads). Thank you very much!
Solution:
actually linux permissions on the folder...
Jump to solution
34 Replies
SirAlyon
SirAlyonOP9mo ago
As you can see the notification is working perfectly fine!
No description
toeknee
toeknee9mo ago
You should use the storage disk: https://filamentphp.com/docs/3.x/actions/prebuilt-actions/export#customizing-the-storage-disk Once the disk is set, then you should be able to set the url method on the disk too.
SirAlyon
SirAlyonOP9mo ago
Thanks for the fast reply. This is my code (i added now the disk method, but actually it is the default one).
ExportAction::make()
->label('Export Report Scarico')
->exporter(KiidMailReportExporter::class)
->fileName(fn (): string => "kiid-report-" . Carbon::now()->toDateString())
->maxRows(50000)
->fileDisk('download')
->formats([
ExportFormat::Xlsx,
]),
ExportAction::make()
->label('Export Report Scarico')
->exporter(KiidMailReportExporter::class)
->fileName(fn (): string => "kiid-report-" . Carbon::now()->toDateString())
->maxRows(50000)
->fileDisk('download')
->formats([
ExportFormat::Xlsx,
]),
I can't understand how and where should i override the url u mentioned? ty
toeknee
toeknee9mo ago
you set fileDisk = download. So download is the disk defined in filesystem.php
SirAlyon
SirAlyonOP9mo ago
Yes of course 🙂
'download' => [
'driver' => 'local',
'root' => '/dati',
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
'throw' => false,
'permissions' => [
'file' => 0775,
'dir' => 0775,
]
'download' => [
'driver' => 'local',
'root' => '/dati',
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
'throw' => false,
'permissions' => [
'file' => 0775,
'dir' => 0775,
]
is this the url method i have to modify?
toeknee
toeknee9mo ago
I believe so
SirAlyon
SirAlyonOP9mo ago
mmm it doesn't change the notification link..
SirAlyon
SirAlyonOP9mo ago
No description
toeknee
toeknee9mo ago
are you getting a 419 by any chance?
SirAlyon
SirAlyonOP9mo ago
'download' => [
'driver' => 'local',
'root' => '/dati',
'url' => '/dati',
'visibility' => 'public',
'throw' => false,
'permissions' => [
'file' => 0775,
'dir' => 0775,
]
],
'download' => [
'driver' => 'local',
'root' => '/dati',
'url' => '/dati',
'visibility' => 'public',
'throw' => false,
'permissions' => [
'file' => 0775,
'dir' => 0775,
]
],
If you mean an error code 419, nope.
toeknee
toeknee9mo ago
So what are you trying to change?
SirAlyon
SirAlyonOP9mo ago
This download link:
No description
toeknee
toeknee9mo ago
But why? The download link is a route for filament to fetch the file and download it. It's not a direct link.
SirAlyon
SirAlyonOP9mo ago
I just want it working fine... and it isn't
toeknee
toeknee9mo ago
So the file isn't downloading?
SirAlyon
SirAlyonOP9mo ago
nop!
toeknee
toeknee9mo ago
What is the error?
SirAlyon
SirAlyonOP9mo ago
No description
toeknee
toeknee9mo ago
inspect the network what's the error. I have a feeling it is 419
SirAlyon
SirAlyonOP9mo ago
No description
toeknee
toeknee9mo ago
500, so inspect your logs to what is causing a 500
SirAlyon
SirAlyonOP9mo ago
Also, under my storage folder filament is saving the file under /filament_exports/{DINAMIC ID}/file.xlsx
toeknee
toeknee9mo ago
That's correct
SirAlyon
SirAlyonOP9mo ago
This route is trying to download from, filament/exports, no way its gona work(?)
toeknee
toeknee9mo ago
Read the route. It's a download function of a file. not a direct link.
Route::get('/filament/exports/{export}/download', DownloadExport::class)
->name('filament.exports.download')
->middleware(['web', 'auth']);
Route::get('/filament/exports/{export}/download', DownloadExport::class)
->name('filament.exports.download')
->middleware(['web', 'auth']);
which runs
public function __invoke(Request $request, Export $export): StreamedResponse
{
if (filled(Gate::getPolicyFor($export::class))) {
authorize('view', $export);
} else {
abort_unless($export->user()->is(auth()->user()), 403);
}

$format = ExportFormat::tryFrom($request->query('format'));

abort_unless($format !== null, 404);

return $format->getDownloader()($export);
}
public function __invoke(Request $request, Export $export): StreamedResponse
{
if (filled(Gate::getPolicyFor($export::class))) {
authorize('view', $export);
} else {
abort_unless($export->user()->is(auth()->user()), 403);
}

$format = ExportFormat::tryFrom($request->query('format'));

abort_unless($format !== null, 404);

return $format->getDownloader()($export);
}
SirAlyon
SirAlyonOP9mo ago
mmmm thanks due 😄 I fixed it!
toeknee
toeknee9mo ago
What was it..
SirAlyon
SirAlyonOP9mo ago
[2024-02-29 15:01:13] local.ERROR: fpassthru(): Argument #1 ($stream) must be of type resource, null given {"userId":1,"exception":"[object] (TypeError(code: 0): fpassthru(): Argument #1 ($stream) must be of type resource, null given at>
[stacktrace]
#0 /home/alby/pautoimporta/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php(309): fpassthru()
#1 /home/alby/pautoimporta/vendor/symfony/http-foundation/StreamedResponse.php(106): Illuminate\\Filesystem\\FilesystemAdapter->Illuminate\\Filesystem\\{closure}()
#2 /home/alby/pautoimporta/vendor/symfony/http-foundation/Response.php(423): Symfony\\Component\\HttpFoundation\\StreamedResponse->sendContent()
#3 /home/alby/pautoimporta/public/index.php(53): Symfony\\Component\\HttpFoundation\\Response->send()
#4 {main}
"}
[2024-02-29 15:01:13] local.ERROR: fpassthru(): Argument #1 ($stream) must be of type resource, null given {"userId":1,"exception":"[object] (TypeError(code: 0): fpassthru(): Argument #1 ($stream) must be of type resource, null given at>
[stacktrace]
#0 /home/alby/pautoimporta/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php(309): fpassthru()
#1 /home/alby/pautoimporta/vendor/symfony/http-foundation/StreamedResponse.php(106): Illuminate\\Filesystem\\FilesystemAdapter->Illuminate\\Filesystem\\{closure}()
#2 /home/alby/pautoimporta/vendor/symfony/http-foundation/Response.php(423): Symfony\\Component\\HttpFoundation\\StreamedResponse->sendContent()
#3 /home/alby/pautoimporta/public/index.php(53): Symfony\\Component\\HttpFoundation\\Response->send()
#4 {main}
"}
Solution
SirAlyon
SirAlyon9mo ago
actually linux permissions on the folder...
toeknee
toeknee9mo ago
Well done
SirAlyon
SirAlyonOP9mo ago
to you! Thank you very much
khairulazmi_
khairulazmi_7mo ago
how you change the permission ? I want to have when new folder created it should be use www-data
toeknee
toeknee7mo ago
You don't you should setup your server correctly since that's handled by php-fpm.
khairulazmi_
khairulazmi_7mo ago
Yeah . Apparently I set my supervisor wrong .
Want results from more Discord servers?
Add your server