From 4bda041c08a0d3b1dc2c54904263cb01c4d5b634 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Mon, 17 Jun 2024 13:59:00 +0800 Subject: [PATCH] Export file directly from Laravel Vapor without going through S3 bucket --- .../ExportCustomersToExcelController.php | 174 +++++++++++++----- 1 file changed, 131 insertions(+), 43 deletions(-) diff --git a/app/Http/Controllers/Exports/ExportCustomersToExcelController.php b/app/Http/Controllers/Exports/ExportCustomersToExcelController.php index c55c989f..60470b2b 100644 --- a/app/Http/Controllers/Exports/ExportCustomersToExcelController.php +++ b/app/Http/Controllers/Exports/ExportCustomersToExcelController.php @@ -38,23 +38,51 @@ class ExportCustomersToExcelController } public function export(ExportsCustomers $exportsCustomers, Request $request){ - return $exportsCustomers->download('customers.csv', Excel::CSV, ['Content-Type' => 'text/csv']); + $exportFileName = 'customers.csv'; + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + return response([ 'src' => $this->S3($exportFileName, $exportsCustomers) ]); + } + else{ + return $exportsCustomers->download($exportFileName, Excel::CSV, ['Content-Type' => 'text/csv']); + } } public function exportCurrencyVendorOrder(ExportCurrencyVendorOrder $exportCurrencyVendorOrder, Request $request){ $exportCurrencyVendorOrder = new ExportCurrencyVendorOrder($request); - $response = $exportCurrencyVendorOrder->download('CurrencyVendorOrder.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); - ob_end_clean(); - return $response; + $exportFileName = 'CurrencyVendorOrder.xls'; + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + return response([ 'src' => $this->S3($exportFileName, $exportCurrencyVendorOrder) ]); + } + else{ + $response = $exportCurrencyVendorOrder->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); + ob_end_clean(); + return $response; + } } public function transactions(ExportsTransactions $exportsTransactions, Request $request){ - return $exportsTransactions->download('transactions.csv', Excel::CSV, ['Content-Type' => 'text/csv']); + $exportFileName = 'transactions.csv'; + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + return response([ 'src' => $this->S3($exportFileName, $exportsTransactions) ]); + } + else{ + return $exportsTransactions->download($exportFileName, Excel::CSV, ['Content-Type' => 'text/csv']); + } } public function nullDebtor(ExportsNullDebtors $exportsNullDebtors, Request $request){ - $response = $exportsNullDebtors->download('nullDebtor.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); - ob_end_clean(); - return $response; + $exportFileName = 'nullDebtor.xls'; + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + return response([ 'src' => $this->S3($exportFileName, $exportsNullDebtors) ]); + } + else{ + $response = $exportsNullDebtors->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); + ob_end_clean(); + return $response; + } } public function paymentTransactions(Request $request){ @@ -62,17 +90,7 @@ 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: Return temporary URL from S3 - $temporaryUrl = Storage::disk('s3')->temporaryUrl( - $filePathForS3, - Carbon::now()->addMinutes(10) - ); - - return response([ 'src' => $temporaryUrl ]); + return response([ 'src' => $this->S3($exportFileName, $exportsTransactions) ]); } else{ $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); @@ -83,54 +101,124 @@ class ExportCustomersToExcelController public function whiteFormTransactions(Request $request){ $exportsTransactions = new ExportsWhiteFormTransactions($request); - $response = $exportsTransactions->download('white-form-transactions.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); - ob_end_clean(); - return $response; + $exportFileName = 'white-form-transactions.xls'; + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + return response([ 'src' => $this->S3($exportFileName, $exportsTransactions) ]); + } + else{ + $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); + ob_end_clean(); + return $response; + } } public function walletTransactions(Request $request){ $exportsTransactions = new ExportsWalletTransactions($request); - $response = $exportsTransactions->download('wallet-transactions.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); - ob_end_clean(); - return $response; + $exportFileName = 'wallet-transactions.xls'; + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + return response([ 'src' => $this->S3($exportFileName, $exportsTransactions) ]); + } + else{ + $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); + ob_end_clean(); + return $response; + } } public function invoiceTransactions(Request $request){ $exportsTransactions = new ExportsInvoiceTransactions($request); - $response = $exportsTransactions->download('invoice-transactions.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); - ob_end_clean(); - return $response; + $exportFileName = 'invoice-transactions.xls'; + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + return response([ 'src' => $this->S3($exportFileName, $exportsTransactions) ]); + } + else{ + $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); + ob_end_clean(); + return $response; + } } public function receiptTransactions(Request $request){ $exportsTransactions = new ExportsReceiptTransactions($request); - $response = $exportsTransactions->download('receipt-transactions.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); - ob_end_clean(); - return $response; + $exportFileName = 'receipt-transactions.xls'; + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + return response([ 'src' => $this->S3($exportFileName, $exportsTransactions) ]); + } + else{ + $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); + ob_end_clean(); + return $response; + } } public function bookingTransactions(ExportsBookingTransactions $exportsBookingTransactions, Request $request){ - $response = $exportsBookingTransactions->download('bookingTransactions.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); - ob_end_clean(); - return $response; + $exportFileName = 'bookingTransactions.xls'; + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + return response([ 'src' => $this->S3($exportFileName, $exportsBookingTransactions) ]); + } + else{ + $response = $exportsBookingTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); + ob_end_clean(); + return $response; + } } public function leadsData(ExportsLeadsTransactions $exportsLeadsTransactions, Request $request){ - $response = $exportsLeadsTransactions->download('leadsData.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); - ob_end_clean(); - return $response; + $exportFileName = 'leadsData.xls'; + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + return response([ 'src' => $this->S3($exportFileName, $exportsLeadsTransactions) ]); + } + else{ + $response = $exportsLeadsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); + ob_end_clean(); + return $response; + } } public function importedInvoiceMapped(ExportsImportedInvoiceMappeds $exportsImportedInvoiceMappeds, Request $request) { - $response = $exportsImportedInvoiceMappeds->download($request->input('fileName').'.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); - ob_end_clean(); - return $response; + $exportFileName = $request->input('fileName').'.xls'; + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + return response([ 'src' => $this->S3($exportFileName, $exportsImportedInvoiceMappeds) ]); + } + else{ + $response = $exportsImportedInvoiceMappeds->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); + ob_end_clean(); + return $response; + } } public function importedReceiptMapped(ExportsImportedReceiptMappeds $exportsImportedReceiptMappeds, Request $request) { - $response = $exportsImportedReceiptMappeds->download($request->input('fileName').'.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); - ob_end_clean(); - return $response; + $exportFileName = $request->input('fileName').'.xls'; + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + return response([ 'src' => $this->S3($exportFileName, $exportsImportedReceiptMappeds) ]); + } + else{ + $response = $exportsImportedReceiptMappeds->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); + ob_end_clean(); + return $response; + } + } + + private function S3($exportFileName, $exportableObject){ + //Step 1: Upload to S3 + $filePathForS3 = 'temp/' . $exportFileName; + $exportableObject->store($filePathForS3, 's3'); + + //Step 2: Return temporary URL from S3 + $temporaryUrl = Storage::disk('s3')->temporaryUrl( + $filePathForS3, + Carbon::now()->addMinutes(10) + ); + + return $temporaryUrl; } }