Export excel file directly from Laravel Vapor without going through S3 bucket

This commit is contained in:
Dillon Ngo
2024-06-18 09:16:04 +08:00
parent 4bda041c08
commit 700b72900a
9 changed files with 179 additions and 27 deletions
@@ -9,6 +9,8 @@ use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Excel;
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;
class ExportAnalyticToExcelController
{
@@ -24,14 +26,42 @@ class ExportAnalyticToExcelController
}
public function bookingData(ExportsAnalyticBookingTransactions $exportsAnalyticBookingTransactions, Request $request){
$response = $exportsAnalyticBookingTransactions->download('bookingData.csv', Excel::CSV, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
ob_end_clean();
return $response;
$exportFileName = 'bookingData.csv';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => $this->S3($exportFileName, $exportsAnalyticBookingTransactions) ]);
}
else{
$response = $exportsAnalyticBookingTransactions->download($exportFileName, Excel::CSV, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
ob_end_clean();
return $response;
}
}
public function billingData(ExportsAnalyticBillingTransactions $exportsAnalyticBillingTransactions, Request $request){
$response = $exportsAnalyticBillingTransactions->download('billingData.csv', Excel::CSV, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
ob_end_clean();
return $response;
$exportFileName = 'billingData.csv';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => $this->S3($exportFileName, $exportsAnalyticBillingTransactions) ]);
}
else{
$response = $exportsAnalyticBillingTransactions->download($exportFileName, Excel::CSV, ['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;
}
}
@@ -7,6 +7,8 @@ use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Excel;
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;
class ExportCustomersWalletTransactionToExcelController
{
@@ -24,9 +26,29 @@ class ExportCustomersWalletTransactionToExcelController
public function export(Request $request)
{
$exportsTransactions = new ExportsCustomersWalletTransactionHistory($request);
$filename = $request->route('marking') . '-wallet-' . ($request->route('is_precise') == 'true' ? 'precise-' : '') . 'transaction-history.xls';
$response = $exportsTransactions->download($filename, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
ob_end_clean();
return $response;
$exportFileName = $request->route('marking') . '-wallet-' . ($request->route('is_precise') == 'true' ? 'precise-' : '') . 'transaction-history.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;
}
}
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;
}
}
@@ -3,7 +3,7 @@
<div class="col">
<div class="row p-b-10 b-b b-grey">
<div class="col-2">{{ item.imported_date }}</div>
<div class="col-2">
<div class="col-2">
<button class="btn btn-xs btn-outline-success b-rad-none m-r-5" @click="downloadInvoiceMapped(item.imported_date)">
Download Invoices Mapped
</button>
@@ -30,8 +30,20 @@
downloadInvoiceMapped(importedDate) {
var arrDateTime = importedDate.split(" ");
const fileName = 'InvoiceMapped';
window.open(this.route('importedInvoiceMapped.export')+'?date='+arrDateTime[0]+'&time='+arrDateTime[1]+'&fileName='+fileName, '_blank');
const url = this.route('importedInvoiceMapped.export')+'?date='+arrDateTime[0]+'&time='+arrDateTime[1]+'&fileName='+fileName;
if(window.LARAVEL_VAPOR_ENABLED){
this.submit(url, 'get', this.section, false, false);
}
else{
window.open(url, '_blank');
}
},
successHandler(response) {
if(window.LARAVEL_VAPOR_ENABLED){
if (response.src) {
window.location.href = response.src;
}
}
},
},
mixins: [componentHandler]
@@ -3,7 +3,7 @@
<div class="col">
<div class="row p-b-10 b-b b-grey">
<div class="col-2">{{ item.imported_date }}</div>
<div class="col-2">
<div class="col-2">
<button class="btn btn-xs btn-outline-success b-rad-none m-r-5" @click="downloadInvoiceMapped(item.imported_date)">
Download Receipts Mapped
</button>
@@ -30,8 +30,20 @@
downloadInvoiceMapped(importedDate) {
var arrDateTime = importedDate.split(" ");
const fileName = 'ReceiptMapped';
window.open(this.route('importedReceiptMapped.export')+'?date='+arrDateTime[0]+'&time='+arrDateTime[1]+'&fileName='+fileName, '_blank');
const url = this.route('importedReceiptMapped.export')+'?date='+arrDateTime[0]+'&time='+arrDateTime[1]+'&fileName='+fileName;
if(window.LARAVEL_VAPOR_ENABLED){
this.submit(url, 'get', this.section, false, false);
}
else{
window.open(url, '_blank');
}
},
successHandler(response) {
if(window.LARAVEL_VAPOR_ENABLED){
if (response.src) {
window.location.href = response.src;
}
}
},
},
mixins: [componentHandler]
@@ -125,10 +125,23 @@
downloadInvoiceMapped() {
var arrDateTime = this.importedDate.split(" ");
const fileName = 'importInvoiceMapping';
const url = this.route('importedInvoiceMapped.export')+'?date='+arrDateTime[0]+'&time='+arrDateTime[1]+'&fileName='+fileName;
window.open(this.route('importedInvoiceMapped.export')+'?date='+arrDateTime[0]+'&time='+arrDateTime[1]+'&fileName='+fileName, '_blank');
if(window.LARAVEL_VAPOR_ENABLED){
return fetch(url, {
method: 'GET',
responseType: 'json',
}).then(response => {
if (response.src) {
window.location.href = response.src;
}
});
}
else{
window.open(url, '_blank');
}
},
}
}
</script>
</script>
@@ -129,10 +129,23 @@
downloadInvoiceMapped() {
var arrDateTime = this.importedDate.split(" ");
const fileName = 'ReceiptMapped';
const url = this.route('importedReceiptMapped.export')+'?date='+arrDateTime[0]+'&time='+arrDateTime[1]+'&fileName='+fileName;
window.open(this.route('importedReceiptMapped.export')+'?date='+arrDateTime[0]+'&time='+arrDateTime[1]+'&fileName='+fileName, '_blank');
if(window.LARAVEL_VAPOR_ENABLED){
return fetch(url, {
method: 'GET',
responseType: 'json',
}).then(response => {
if (response.src) {
window.location.href = response.src;
}
});
}
else{
window.open(url, '_blank');
}
},
}
}
</script>
</script>
@@ -113,7 +113,7 @@
</div>
</div>
<div class="col-1">Amount</div>
<div class="col-1" v-if="stage === 4"></div>
<div class="col-1" v-if="stage === 4"></div>
<div class="col-1" v-if="stage === 4">Select <input type="checkbox" v-model="selectAll">
</div>
</div>
@@ -273,7 +273,19 @@ export default {
route += '&bankStatementTransactionId='+checkedStatementTransactions;
}
window.open(route, '_blank');
if(window.LARAVEL_VAPOR_ENABLED){
return fetch(route, {
method: 'GET',
responseType: 'json',
}).then(response => {
if (response.src) {
window.location.href = response.src;
}
});
}
else{
window.open(route, '_blank');
}
},
exportReceiptToAutoCount(){
const checkedStatementTransactions = this.getCheckedStatementOwners();
@@ -285,7 +297,19 @@ export default {
route += '&bankStatementTransactionId='+checkedStatementTransactions;
}
window.open(route, '_blank');
if(window.LARAVEL_VAPOR_ENABLED){
return fetch(route, {
method: 'GET',
responseType: 'json',
}).then(response => {
if (response.src) {
window.location.href = response.src;
}
});
}
else{
window.open(route, '_blank');
}
},
getCheckedStatementOwners() {
let bankStatementTransactionId = [];
@@ -72,10 +72,23 @@
this.submit(this.route('api.debtor.import'), 'post', this.section, true, false)
},
downloadReport(){
window.open(route('newDebtor.export'), '_blank');
let route = route('newDebtor.export');
if(window.LARAVEL_VAPOR_ENABLED){
return fetch(route, {
method: 'GET',
responseType: 'json',
}).then(response => {
if (response.src) {
window.location.href = response.src;
}
});
}
else{
window.open(route, '_blank');
}
},
},
mixins: [ModalFromHandler]
}
</script>
</script>
@@ -77,8 +77,14 @@ export default {
if(!this.validate()){ return; }
var supplierIds = JSON.stringify(this.parameters.supplierIds);
let url = route('export.transactions.booking')+'?startDate='+this.parameters.startDate+'&endDate='+this.parameters.endDate+'&supplierIds='+supplierIds;
window.open(route('export.transactions.booking')+'?startDate='+this.parameters.startDate+'&endDate='+this.parameters.endDate+'&supplierIds='+supplierIds, '_blank');
if(window.LARAVEL_VAPOR_ENABLED){
this.submit(url, 'get', this.section, false, false);
}
else{
window.open(url, '_blank');
}
},
updateList(supplierList){
let supplierIds = [];
@@ -90,6 +96,13 @@ export default {
this.parameters.supplierIds = supplierIds;
this.parameters.supplierNames = supplierNames;
},
successHandler(response) {
if(window.LARAVEL_VAPOR_ENABLED){
if (response.src) {
window.location.href = response.src;
}
}
},
},
mixins: [componentHandler]
};