Laravel Vapor - sync code for download files in bulk from S3 to more files

This commit is contained in:
Dillon Ngo
2024-04-27 10:08:55 +08:00
parent 38ad3d315d
commit 3f88c365e8
2 changed files with 52 additions and 43 deletions
@@ -53,20 +53,18 @@ 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'){
$zipDirectory = storage_path('app/bulk_invoice'); // Update this with the actual directory path
if (!file_exists($zipDirectory)) {
mkdir($zipDirectory, 0755, true);
}
$startDate = $startDate->format('d-m-Y');
$endDate = $endDate->format('d-m-Y');
$zipFileName = "invoices_{$startDate}_to_{$endDate}_{$company->reference}.zip";
$zip_file = "{$zipDirectory}/{$zipFileName}";
// $zipFileName2 = "invoices_{$startDate}_to_{$endDate}_{$company->reference}_dup.zip";
// $zip_file2 = "{$zipDirectory}/{$zipFileName2}";
$zip = new ZipArchive();
if ($zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
@@ -77,12 +75,6 @@ class BulkDownloadCustomerInvoicesLogic
}
$zip->close();
// $filePathForS3 = 'bulk_invoice/'.$zipFileName;
// Storage::put($filePathForS3, file_get_contents($zip_file), 's3');
// $responseContent = Storage::disk('s3')->get($filePathForS3);
// file_put_contents($zip_file2, $responseContent);
// return response()->download($zip_file2);
return response()->download($zip_file);
} else {
return response()->json([
@@ -92,13 +84,6 @@ class BulkDownloadCustomerInvoicesLogic
}
}
else{
$zipDirectory = storage_path('app/bulk_invoice'); // Update this with the actual directory path
if (!file_exists($zipDirectory)) {
mkdir($zipDirectory, 0755, true);
}
$zip_file = "{$zipDirectory}/invoices_{$request->input('startDate')}_to_{$request->input('endDate')}_{$company->reference}.zip";
$zip = new ZipArchive();
@@ -22,17 +22,17 @@ class BulkDownloadSupplierWhiteFormsLogic
public function execute(Request $request)
{
try {
$startDate = Carbon::createFromFormat('d-m-Y', $request->input('startDate'))->startOfDay();
$endDate = Carbon::createFromFormat('d-m-Y', $request->input('endDate'))->endOfDay();
$companyReference = Company::find($request->input('supplier'))->reference;
$groups = Group::where('issuer', $request->input('supplier'))
->whereDate('created_at', '>=', $startDate)
->whereDate('created_at', '<=', $endDate)
->orderBy('created_at', 'DESC')
->get();
if (count($groups) > 0) {
$zipDirectory = storage_path('app/bulk_whiteform'); // Update this with the actual directory path
@@ -40,25 +40,49 @@ class BulkDownloadSupplierWhiteFormsLogic
if (!file_exists($zipDirectory)) {
mkdir($zipDirectory, 0755, true);
}
$zip_file = "{$zipDirectory}/currency_vendor_orders_{$request->input('startDate')}_to_{$request->input('endDate')}_{$companyReference}.zip";
$zip = new ZipArchive();
if ($zip->open($zip_file, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) {
foreach($groups as $group) {
$document = $group->documents()->where('document_type', DocumentType::CURRENCY_VENDOR_ORDER)->whereNull('deleted_at')->first()->files()->first();
$created_at = $group->created_at->format('Y-m-d');
$zip->addFile(Storage::disk('documents')->path($document->file->file_info->original->file), $created_at . "_" . $group->amount . "_" . $group->reference . '.pdf');
}
$zip->close();
while (ob_get_level()) {
ob_end_clean();
}
return response()->download($zip_file);
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver == 's3'){
$startDate = $startDate->format('d-m-Y');
$endDate = $endDate->format('d-m-Y');
$zipFileName = "currency_vendor_orders_{$startDate}_to_{$endDate}_{$companyReference}.zip";
$zip_file = "{$zipDirectory}/{$zipFileName}";
$zip = new ZipArchive();
if ($zip->open($zip_file, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) {
foreach($groups as $group) {
$document = $group->documents()->where('document_type', DocumentType::CURRENCY_VENDOR_ORDER)->whereNull('deleted_at')->first()->files()->first();
$created_at = $group->created_at->format('Y-m-d');
$fileContent = Storage::disk('s3')->get('documents/'.$document->file->file_info->original->file);
$zip->addFromString('invoice-' . $created_at . "_" . $group->amount . "_" . $group->reference . '.pdf', $fileContent);
}
$zip->close();
return response()->download($zip_file);
}
}
else{
$zip_file = "{$zipDirectory}/currency_vendor_orders_{$request->input('startDate')}_to_{$request->input('endDate')}_{$companyReference}.zip";
$zip = new ZipArchive();
if ($zip->open($zip_file, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) {
foreach($groups as $group) {
$document = $group->documents()->where('document_type', DocumentType::CURRENCY_VENDOR_ORDER)->whereNull('deleted_at')->first()->files()->first();
$created_at = $group->created_at->format('Y-m-d');
$zip->addFile(Storage::disk('documents')->path($document->file->file_info->original->file), $created_at . "_" . $group->amount . "_" . $group->reference . '.pdf');
}
$zip->close();
while (ob_get_level()) {
ob_end_clean();
}
return response()->download($zip_file);
}
}
}
return response()->json([
'status' => 'Failed',
@@ -75,4 +99,4 @@ class BulkDownloadSupplierWhiteFormsLogic
]);
}
}
}
}