From 357a4fde66eb26486a021781fe88b82ea5c022ae Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Mon, 29 Apr 2024 14:57:15 +0800 Subject: [PATCH] Laravel Vapor - sync code for download files in bulk from S3 to more files --- app/Classes/General/ExcelHandel.php | 15 ++++++++++++--- .../BulkDownloadCustomerInvoicesLogic.php | 2 +- .../BulkDownloadSupplierWhiteFormsLogic.php | 2 +- .../ControllersLogic/RenderDocumentLogic.php | 2 +- .../Documents/Services/ConvertsBase64ToFile.php | 2 +- .../Imports/ImportUpdateDebtorController.php | 1 - .../DownloadSupplierWhiteFormComponent.vue | 2 -- .../elements/BulkDownloadInvoiceComponent.vue | 2 -- 8 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/Classes/General/ExcelHandel.php b/app/Classes/General/ExcelHandel.php index 3060ed8b..31034ff8 100644 --- a/app/Classes/General/ExcelHandel.php +++ b/app/Classes/General/ExcelHandel.php @@ -3,6 +3,7 @@ namespace App\Classes\General; use Illuminate\Support\Str; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Storage; class ExcelHandel { @@ -67,9 +68,17 @@ class ExcelHandel public static function generateExcel($path = '', $exceldata = '', $filename = '', $extension = '') { $file_info = []; - $file = \Storage::disk('public')->put('excels/' . $path . '/' . $filename . '.' . $extension, $exceldata); + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + $filePathForS3 = 'public/excels/' . $path . '/' . $filename . '.' . $extension; + Storage::disk('s3')->put($filePathForS3, $exceldata); + } + else{ + $file = Storage::disk('public')->put('excels/' . $path . '/' . $filename . '.' . $extension, $exceldata); + } + $file_info['original']['file'] = storage_path('app/public/excels/' . $path . '/' . $filename . '.' . $extension); - + return $file_info; } @@ -84,4 +93,4 @@ class ExcelHandel return true; } -} \ No newline at end of file +} diff --git a/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php b/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php index dd48425c..f4d875c0 100644 --- a/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php +++ b/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php @@ -60,7 +60,7 @@ class BulkDownloadCustomerInvoicesLogic } $filesystemDriver = Storage::getDefaultDriver(); - if($filesystemDriver == 's3'){ + if($filesystemDriver === 's3'){ $startDate = $startDate->format('d-m-Y'); $endDate = $endDate->format('d-m-Y'); $zipFileName = "invoices_{$startDate}_to_{$endDate}_{$company->reference}.zip"; diff --git a/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadSupplierWhiteFormsLogic.php b/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadSupplierWhiteFormsLogic.php index 61c91d9b..29d2720c 100644 --- a/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadSupplierWhiteFormsLogic.php +++ b/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadSupplierWhiteFormsLogic.php @@ -44,7 +44,7 @@ class BulkDownloadSupplierWhiteFormsLogic $filesystemDriver = Storage::getDefaultDriver(); - if($filesystemDriver == 's3'){ + if($filesystemDriver === 's3'){ $startDate = $startDate->format('d-m-Y'); $endDate = $endDate->format('d-m-Y'); diff --git a/app/Classes/Modules/Documents/ControllersLogic/RenderDocumentLogic.php b/app/Classes/Modules/Documents/ControllersLogic/RenderDocumentLogic.php index 0146a2f7..193a7bd2 100644 --- a/app/Classes/Modules/Documents/ControllersLogic/RenderDocumentLogic.php +++ b/app/Classes/Modules/Documents/ControllersLogic/RenderDocumentLogic.php @@ -55,7 +55,7 @@ class RenderDocumentLogic extends AbstractControllerLogic $this->canRenderDocument->passes(); $filesystemDriver = Storage::getDefaultDriver(); - if($filesystemDriver == 's3'){ + if($filesystemDriver === 's3'){ if(!Storage::disk('s3')->exists($file)) { throw new ResourceNotFoundException(); diff --git a/app/Classes/Modules/Documents/Services/ConvertsBase64ToFile.php b/app/Classes/Modules/Documents/Services/ConvertsBase64ToFile.php index dc9c2210..f246fcfa 100644 --- a/app/Classes/Modules/Documents/Services/ConvertsBase64ToFile.php +++ b/app/Classes/Modules/Documents/Services/ConvertsBase64ToFile.php @@ -100,7 +100,7 @@ class ConvertsBase64ToFile private function generateFile(FileObject $file, string $suffix = '') { $filesystemDriver = Storage::getDefaultDriver(); - if($filesystemDriver == 's3'){ + if($filesystemDriver === 's3'){ $filePath = 'documents/'.$this->path.'/'.$file->getFileName().$suffix.'.'.$file->getExtension(); Storage::put($filePath, $file->getDecodedData(), 's3'); return $filePath; diff --git a/app/Http/Controllers/Imports/ImportUpdateDebtorController.php b/app/Http/Controllers/Imports/ImportUpdateDebtorController.php index 1acc7b11..b3d5a534 100644 --- a/app/Http/Controllers/Imports/ImportUpdateDebtorController.php +++ b/app/Http/Controllers/Imports/ImportUpdateDebtorController.php @@ -4,7 +4,6 @@ namespace App\Http\Controllers\Imports; use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject; use App\Classes\Modules\Imports\Services\ImportsDebtor; -use App\Classes\General\ExcelHandel; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Models\User; diff --git a/resources/assets/vue/components/bookings/elements/DownloadSupplierWhiteFormComponent.vue b/resources/assets/vue/components/bookings/elements/DownloadSupplierWhiteFormComponent.vue index 54f1617e..83f6e3f2 100644 --- a/resources/assets/vue/components/bookings/elements/DownloadSupplierWhiteFormComponent.vue +++ b/resources/assets/vue/components/bookings/elements/DownloadSupplierWhiteFormComponent.vue @@ -86,14 +86,12 @@ export default { }, successHandler(response) { this.isLoading = false; - console.log('debug response: ' + JSON.stringify(response)); if (response.message) { this.returnData = response; } else { this.returnData = null; if(window.LARAVEL_VAPOR_ENABLED){ - console.log('window.LARAVEL_VAPOR_ENABLED'); if (response.src) { window.location.href = response.src; } diff --git a/resources/assets/vue/components/companies/elements/BulkDownloadInvoiceComponent.vue b/resources/assets/vue/components/companies/elements/BulkDownloadInvoiceComponent.vue index b8b50998..88cd0d9c 100644 --- a/resources/assets/vue/components/companies/elements/BulkDownloadInvoiceComponent.vue +++ b/resources/assets/vue/components/companies/elements/BulkDownloadInvoiceComponent.vue @@ -88,14 +88,12 @@ }, successHandler(response) { this.isLoading = false; - console.log('debug response: ' + JSON.stringify(response)); if (response.message) { this.returnData = response; } else { this.returnData = null; if(window.LARAVEL_VAPOR_ENABLED){ - console.log('window.LARAVEL_VAPOR_ENABLED'); if (response.src) { window.location.href = response.src; }