Export file directly from Laravel Vapor without going through S3 bucket

This commit is contained in:
Dillon Ngo
2024-06-17 12:43:23 +08:00
parent 5c39d4555e
commit 9395c51d4c
@@ -58,7 +58,7 @@ class ExportCustomersToExcelController
public function paymentTransactions(Request $request){
$exportsTransactions = new ExportsPaymentTransactions($request);
$exportFileName = 'payment-transactions.xlsx';
$exportFileName = 'payment-transactions.xls';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
@@ -68,15 +68,24 @@ class ExportCustomersToExcelController
//Step 2: Read and Download from S3
$fileContent = Storage::disk('s3')->get($filePathForS3);
$exportDirectory = storage_path('app/temp'); // Update this with the actual directory path
if (!file_exists($exportDirectory)) {
mkdir($exportDirectory, 0755, true);
}
$localPath = $exportDirectory . '/' . $exportFileName;
file_put_contents($localPath, $fileContent);
// $exportDirectory = storage_path('app/temp'); // Update this with the actual directory path
// if (!file_exists($exportDirectory)) {
// mkdir($exportDirectory, 0755, true);
// }
// $localPath = $exportDirectory . '/' . $exportFileName;
// file_put_contents($localPath, $fileContent);
//Step 3: Return
return response()->file($localPath, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
// //Step 3: Return
// return response()->file($localPath, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
$headers = [
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'Content-Disposition' => 'attachment; filename="' . $exportFileName . '"',
];
// Return the file content as a response with the headers
return response($fileContent, 200)
->withHeaders($headers);
}
else{
$response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);