From 9c4ed9aba294b3ee66352a57e659742e8f93b4c8 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Wed, 24 Apr 2024 17:26:50 +0800 Subject: [PATCH] Laravel Vapor - Rewrite code to use S3 bucket --- .../BulkDownloadCustomerInvoicesLogic.php | 83 ++++++++++++++----- 1 file changed, 63 insertions(+), 20 deletions(-) diff --git a/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php b/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php index 41a64ecf..89c8c1c4 100644 --- a/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php +++ b/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php @@ -8,7 +8,7 @@ use ZipArchive; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; use App\Classes\Exceptions\MalformedRequestException; -use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\File; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Classes\ValueObjects\Constants\DocumentType; use App\Models\Company; @@ -53,33 +53,76 @@ class BulkDownloadCustomerInvoicesLogic ]); } - $zipDirectory = storage_path('app/bulk_invoice'); // Update this with the actual directory path - if (!file_exists($zipDirectory)) { - mkdir($zipDirectory, 0755, true); - } + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver == 's3'){ + $zip = new ZipArchive(); + $zipDataStr = ''; - $zip_file = "{$zipDirectory}/invoices_{$request->input('startDate')}_to_{$request->input('endDate')}_{$company->reference}.zip"; - - $zip = new ZipArchive(); - if ($zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE)) { foreach ($invoicebookings as $booking) { - $invoice_file = $booking->documents()->where('document_type', DocumentType::INVOICE)->whereNull('deleted_at')->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'); + $invoiceFile = $booking->documents() + ->where('document_type', DocumentType::INVOICE) + ->whereNull('deleted_at') + ->first() + ->files() + ->first(); + + $fileContent = Storage::disk('documents') + ->get($invoiceFile->file->file_info->original->file); + + $zipDataStr .= 'invoice-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'; + $zipDataStr .= "\n"; // Add a separator + $zipDataStr .= $fileContent; } - $zip->close(); + $zipFileName = "invoices_{$startDate}_to_{$endDate}_{$company->reference}.zip"; - while (ob_get_level()) { - ob_end_clean(); + if ($zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE)) { + $zip->addFromString($zipFileName, $zipDataStr); + $zip->close(); + + $filePath = 'bulk_invoice/'.$zipFileName; + Storage::put($filePath, file_get_contents($zipFileName), 's3'); + + return response(['src' => Storage::disk('s3')->get($$filePath) ]); + + } else { + return response()->json([ + 'status' => 'Error', + 'message' => 'Failed to create the Zip archive.', + ]); + } + } + else{ + + $zipDirectory = storage_path('app/bulk_invoice'); // Update this with the actual directory path + + if (!file_exists($zipDirectory)) { + mkdir($zipDirectory, 0755, true); } - return response()->download($zip_file); - } else { - return response()->json([ - 'status' => 'Error', - 'message' => 'Failed to create the Zip archive.', - ]); + $zip_file = "{$zipDirectory}/invoices_{$request->input('startDate')}_to_{$request->input('endDate')}_{$company->reference}.zip"; + + $zip = new ZipArchive(); + if ($zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE)) { + foreach ($invoicebookings as $booking) { + $invoice_file = $booking->documents()->where('document_type', DocumentType::INVOICE)->whereNull('deleted_at')->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->close(); + + while (ob_get_level()) { + ob_end_clean(); + } + + return response()->download($zip_file); + } else { + return response()->json([ + 'status' => 'Error', + 'message' => 'Failed to create the Zip archive.', + ]); + } } } catch (\Exception $e) { // Log the exception for debugging