From d976b8b42be405b7de296f8f51e106180743a9b4 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Mon, 17 Jun 2024 13:07:42 +0800 Subject: [PATCH] Export file directly from Laravel Vapor without going through S3 bucket --- .../ExportCustomersToExcelController.php | 27 +++++-------------- .../DownloadBillingWithDatesComponent.vue | 16 +++++++++-- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/app/Http/Controllers/Exports/ExportCustomersToExcelController.php b/app/Http/Controllers/Exports/ExportCustomersToExcelController.php index d5c45e1f..c55c989f 100644 --- a/app/Http/Controllers/Exports/ExportCustomersToExcelController.php +++ b/app/Http/Controllers/Exports/ExportCustomersToExcelController.php @@ -22,6 +22,7 @@ use App\Classes\Modules\Exports\Services\ExportsReceiptTransactions; use App\Classes\Modules\Exports\Services\ExportsImportedReceiptMappeds; use App\Classes\Modules\Exports\Services\ExportsWhiteFormTransactions; use Illuminate\Support\Facades\Storage; +use Carbon\Carbon; class ExportCustomersToExcelController { @@ -61,31 +62,17 @@ class ExportCustomersToExcelController $exportFileName = 'payment-transactions.xls'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - //Step 1: Upload to S3 $filePathForS3 = 'temp/' . $exportFileName; $exportsTransactions->store($filePathForS3, 's3'); - //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); + //Step 2: Return temporary URL from S3 + $temporaryUrl = Storage::disk('s3')->temporaryUrl( + $filePathForS3, + Carbon::now()->addMinutes(10) + ); - // //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); + return response([ 'src' => $temporaryUrl ]); } else{ $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); diff --git a/resources/assets/vue/components/bookings/elements/DownloadBillingWithDatesComponent.vue b/resources/assets/vue/components/bookings/elements/DownloadBillingWithDatesComponent.vue index 2d89690e..9757fde5 100644 --- a/resources/assets/vue/components/bookings/elements/DownloadBillingWithDatesComponent.vue +++ b/resources/assets/vue/components/bookings/elements/DownloadBillingWithDatesComponent.vue @@ -94,12 +94,24 @@ export default { apiRoute = route('whiteFormTransactions.export'); break; } - window.open(apiRoute + '?startDate=' + this.parameters.startDate + '&endDate=' + this.parameters.endDate, '_blank'); + if(window.LARAVEL_VAPOR_ENABLED){ + this.submit(apiRoute, 'get', this.section, false, false); + } + else{ + window.open(apiRoute + '?startDate=' + this.parameters.startDate + '&endDate=' + this.parameters.endDate, '_blank'); + } }, updateDocumentType(documentType) { this.parameters.type = documentType; this.selectedDocumentStatus = !this.selectedDocumentStatus - } + }, + successHandler(response) { + if(window.LARAVEL_VAPOR_ENABLED){ + if (response.src) { + window.location.href = response.src; + } + } + }, }, mixins: [componentHandler] };