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
SirAlyon5mo ago
As you can see the notification is working perfectly fine!
No description
toeknee
toeknee5mo 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
SirAlyon5mo 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
toeknee5mo ago
you set fileDisk = download. So download is the disk defined in filesystem.php
SirAlyon
SirAlyon5mo 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
toeknee5mo ago
I believe so
SirAlyon
SirAlyon5mo ago
mmm it doesn't change the notification link..
SirAlyon
SirAlyon5mo ago
No description
toeknee
toeknee5mo ago
are you getting a 419 by any chance?
SirAlyon
SirAlyon5mo 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
toeknee5mo ago
So what are you trying to change?
SirAlyon
SirAlyon5mo ago
This download link:
No description
toeknee
toeknee5mo 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
SirAlyon5mo ago
I just want it working fine... and it isn't
toeknee
toeknee5mo ago
So the file isn't downloading?
SirAlyon
SirAlyon5mo ago
nop!
toeknee
toeknee5mo ago
What is the error?
SirAlyon
SirAlyon5mo ago
No description
toeknee
toeknee5mo ago
inspect the network what's the error. I have a feeling it is 419
SirAlyon
SirAlyon5mo ago
No description
toeknee
toeknee5mo ago
500, so inspect your logs to what is causing a 500
SirAlyon
SirAlyon5mo ago
Also, under my storage folder filament is saving the file under /filament_exports/{DINAMIC ID}/file.xlsx
toeknee
toeknee5mo ago
That's correct
SirAlyon
SirAlyon5mo ago
This route is trying to download from, filament/exports, no way its gona work(?)
toeknee
toeknee5mo 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
SirAlyon5mo ago
mmmm thanks due 😄 I fixed it!
toeknee
toeknee5mo ago
What was it..
SirAlyon
SirAlyon5mo 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
SirAlyon5mo ago
actually linux permissions on the folder...
toeknee
toeknee5mo ago
Well done
SirAlyon
SirAlyon5mo ago
to you! Thank you very much
khairulazmi_
khairulazmi_3mo ago
how you change the permission ? I want to have when new folder created it should be use www-data
toeknee
toeknee2mo ago
You don't you should setup your server correctly since that's handled by php-fpm.
khairulazmi_
khairulazmi_2mo ago
Yeah . Apparently I set my supervisor wrong .