From e7357fd9da2d2391af196f69bf9c96720e5be56e Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Tue, 30 Apr 2024 11:25:07 +0800 Subject: [PATCH 1/2] Laravel Vapor - sync code for download files in bulk from S3 to more files --- .../DownloadBookingDocumentLogic.php | 160 ++++++++++++------ .../BulkDownloadCustomerInvoicesLogic.php | 4 +- .../BulkDownloadSupplierWhiteFormsLogic.php | 2 +- 3 files changed, 108 insertions(+), 58 deletions(-) diff --git a/app/Classes/Modules/Bookings/ControllersLogic/DownloadBookingDocumentLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/DownloadBookingDocumentLogic.php index ff9427fe..561357b6 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/DownloadBookingDocumentLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/DownloadBookingDocumentLogic.php @@ -15,6 +15,7 @@ use Illuminate\Support\Facades\Storage; use App\Classes\Exceptions\MalformedRequestException; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Classes\ValueObjects\Constants\DocumentType; +use Illuminate\Support\Facades\Log; class DownloadBookingDocumentLogic { @@ -32,71 +33,120 @@ class DownloadBookingDocumentLogic $zip_file = $document_type.'.zip'; $attachment = storage_path().'/app/documents/collections/' . $zip_file; - $zip = new ZipArchive(); - if ($zip->open($attachment, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) { + $bookings = Booking::where('status', ApprovalStatus::COMPLETED) + ->whereDate('created_at', '>=', Carbon::parse($request->input('startDate'))) + ->whereDate('created_at', '<=', Carbon::parse($request->input('endDate'))) + ->whereHas('transactions', function ($query) use ($request){ + return $query->where('type', TransactionType::PAYMENT)->whereHas('transactions', function ($query) use ($request){ + return $query->where('type', TransactionType::BILL)->where('issuer', $request->input('supplier')); + }); + })->get(); - $bookings = Booking::where('status', ApprovalStatus::COMPLETED) - ->whereDate('created_at', '>=', Carbon::parse($request->input('startDate'))) - ->whereDate('created_at', '<=', Carbon::parse($request->input('endDate'))) - ->whereHas('transactions', function ($query) use ($request){ - return $query->where('type', TransactionType::PAYMENT)->whereHas('transactions', function ($query) use ($request){ - return $query->where('type', TransactionType::BILL)->where('issuer', $request->input('supplier')); - }); - })->get(); + if (!count($bookings)) { + return response()->json(['no file to download']); + } + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + $zip = new ZipArchive(); + if ($zip->open($attachment, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) { + foreach ($bookings as $booking) { - if (!count($bookings)) { - return response()->json(['no file to download']); - } + if ($document_type == 'INVOICEPODO' || $document_type == 'INVOICEPODOSDO') { + $invoice_file = $booking->documents()->where('document_type', DocumentType::INVOICE)->first()->files()->first(); + $purchase_file = $booking->documents()->where('document_type', DocumentType::PURCHASE_ORDER)->first()->files()->first(); + $deliver_file = $booking->documents()->where('document_type', DocumentType::DELIVER_ORDER)->first()->files()->first(); + if ($document_type == 'INVOICEPODOSDO') { + $supplier_deliver_order_file = $booking->documents()->where('document_type', DocumentType::SUPPLIER_DELIVER_ORDER)->first()->files()->first(); + } + $fileContent = Storage::disk('s3')->get('documents/'.$invoice_file->file->file_info->original->file); + $zip->addFromString('invoice-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf', $fileContent); - foreach ($bookings as $booking) { + $fileContent = Storage::disk('s3')->get('documents/'.$purchase_file->file->file_info->original->file); + $zip->addFromString('purchase-order-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf', $fileContent); - if ($document_type == 'INVOICEPODO' || $document_type == 'INVOICEPODOSDO') { - $invoice_file = $booking->documents()->where('document_type', DocumentType::INVOICE)->first()->files()->first(); - $purchase_file = $booking->documents()->where('document_type', DocumentType::PURCHASE_ORDER)->first()->files()->first(); - $deliver_file = $booking->documents()->where('document_type', DocumentType::DELIVER_ORDER)->first()->files()->first(); - if ($document_type == 'INVOICEPODOSDO') { - $supplier_deliver_order_file = $booking->documents()->where('document_type', DocumentType::SUPPLIER_DELIVER_ORDER)->first()->files()->first(); + $fileContent = Storage::disk('s3')->get('documents/'.$deliver_file->file->file_info->original->file); + $zip->addFromString('deliver-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf', $fileContent); + + $fileContent = Storage::disk('s3')->get('documents/'.$supplier_deliver_order_file->file->file_info->original->file); + $zip->addFromString('supplier-deliver-order-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf', $fileContent); + } + else { + $file = $booking->documents()->where('document_type', $document_type)->first()->files()->first(); + $fileContent = Storage::disk('s3')->get('documents/'.$file->file->file_info->original->file); + $zip->addFromString($booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf', $fileContent); } - $zip->addFile(Storage::disk('documents')->path($invoice_file->file->file_info->original->file), 'invoice-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); - - $zip->addFile(Storage::disk('documents')->path($purchase_file->file->file_info->original->file), 'purchase-order-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); - - $zip->addFile(Storage::disk('documents')->path($deliver_file->file->file_info->original->file), 'deliver-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); - - $zip->addFile(Storage::disk('documents')->path($supplier_deliver_order_file->file->file_info->original->file), 'supplier-deliver-order-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); - } - else { - $file = $booking->documents()->where('document_type', $document_type)->first()->files()->first(); - $zip->addFile(Storage::disk('documents')->path($file->file->file_info->original->file), $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); } + $zip->close(); + $filePathForS3 = 'collections/'.$zip_file; + Storage::disk('s3')->put($filePathForS3, file_get_contents($attachment)); + + Log::info('DownloadBookingDocumentLogic finished processing files to zip count: '.count($bookings)); + + $temporaryUrl = Storage::disk('s3')->temporaryUrl( + $filePathForS3, + Carbon::now()->addMinutes(10) + ); + + return response(['src' => $temporaryUrl ]); } - $zip->close(); - while (ob_get_level()) { - ob_end_clean(); - } - ob_start(); - header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK'); - header("Content-Type: application/zip"); - header("Content-Transfer-Encoding: Binary"); - header("Content-Length: " . filesize($attachment)); - header('Pragma: no-cache'); - header("Content-Disposition: attachment; filename=\"" . basename($attachment) . "\""); - ob_flush(); - ob_clean(); - - readfile($attachment); - File::delete($attachment); - - exit; - -// header('Content-Type: application/zip'); -// header('Content-Length: ' . filesize($attachment)); -// readfile($attachment); -// unlink($attachment); - } + else{ + $zip = new ZipArchive(); + if ($zip->open($attachment, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) { + + foreach ($bookings as $booking) { + + if ($document_type == 'INVOICEPODO' || $document_type == 'INVOICEPODOSDO') { + $invoice_file = $booking->documents()->where('document_type', DocumentType::INVOICE)->first()->files()->first(); + $purchase_file = $booking->documents()->where('document_type', DocumentType::PURCHASE_ORDER)->first()->files()->first(); + $deliver_file = $booking->documents()->where('document_type', DocumentType::DELIVER_ORDER)->first()->files()->first(); + if ($document_type == 'INVOICEPODOSDO') { + $supplier_deliver_order_file = $booking->documents()->where('document_type', DocumentType::SUPPLIER_DELIVER_ORDER)->first()->files()->first(); + } + + $zip->addFile(Storage::disk('documents')->path($invoice_file->file->file_info->original->file), 'invoice-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); + + $zip->addFile(Storage::disk('documents')->path($purchase_file->file->file_info->original->file), 'purchase-order-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); + + $zip->addFile(Storage::disk('documents')->path($deliver_file->file->file_info->original->file), 'deliver-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); + + $zip->addFile(Storage::disk('documents')->path($supplier_deliver_order_file->file->file_info->original->file), 'supplier-deliver-order-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); + } + else { + $file = $booking->documents()->where('document_type', $document_type)->first()->files()->first(); + $zip->addFile(Storage::disk('documents')->path($file->file->file_info->original->file), $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); + } + + } + $zip->close(); + while (ob_get_level()) { + ob_end_clean(); + } + ob_start(); + header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK'); + header("Content-Type: application/zip"); + header("Content-Transfer-Encoding: Binary"); + header("Content-Length: " . filesize($attachment)); + header('Pragma: no-cache'); + header("Content-Disposition: attachment; filename=\"" . basename($attachment) . "\""); + ob_flush(); + ob_clean(); + + readfile($attachment); + File::delete($attachment); + + exit; + + //header('Content-Type: application/zip'); + //header('Content-Length: ' . filesize($attachment)); + //readfile($attachment); + //unlink($attachment); + + } + } + } } diff --git a/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php b/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php index f4d875c0..faca4dcd 100644 --- a/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php +++ b/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php @@ -75,10 +75,10 @@ class BulkDownloadCustomerInvoicesLogic } $zip->close(); - $filePathForS3 = 'bulk_whiteform/'.$zipFileName; + $filePathForS3 = 'bulk_invoice/'.$zipFileName; Storage::disk('s3')->put($filePathForS3, file_get_contents($zip_file)); - Log::info('BulkDownloadCustomerInvoicesLogic finished processing files count: '.count($invoicebookings)); + Log::info('BulkDownloadCustomerInvoicesLogic finished processing files to zip count: '.count($invoicebookings)); $temporaryUrl = Storage::disk('s3')->temporaryUrl( $filePathForS3, diff --git a/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadSupplierWhiteFormsLogic.php b/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadSupplierWhiteFormsLogic.php index 29d2720c..ae1c035e 100644 --- a/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadSupplierWhiteFormsLogic.php +++ b/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadSupplierWhiteFormsLogic.php @@ -71,7 +71,7 @@ class BulkDownloadSupplierWhiteFormsLogic $filePathForS3 = 'bulk_whiteform/'.$zipFileName; Storage::disk('s3')->put($filePathForS3, file_get_contents($zip_file)); - Log::info('BulkDownloadSupplierWhiteFormsLogic finished processing files count: '.count($groups)); + Log::info('BulkDownloadSupplierWhiteFormsLogic finished processing files to zip count: '.count($groups)); $temporaryUrl = Storage::disk('s3')->temporaryUrl( $filePathForS3, From 2c9c5d49ed94ba401df633b71c294d6debd257f8 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Tue, 30 Apr 2024 11:26:59 +0800 Subject: [PATCH 2/2] Laravel Vapor - sync code for download files in bulk from S3 to more files --- vapor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vapor.yml b/vapor.yml index a0d31602..4328e569 100644 --- a/vapor.yml +++ b/vapor.yml @@ -55,7 +55,7 @@ environments: - exchange-default-development - exchange-high_priority-development database: cief-rds-mysql - storage: exchange-2.0-development + storage: exchange-2.0-production runtime: 'docker' timeout: 180 build: