mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-22 05:53:58 +00:00
Laravel Vapor - Rewrite code to use S3 bucket
This commit is contained in:
+63
-20
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user