Perbarui metode bulkDownload untuk penanganan file lebih baik

Tambahkan pengelompokan dokumen berdasarkan jenis jaminan dalam folder terstruktur, log peringatan untuk file yang hilang, dan bersihkan nama folder untuk kompatibilitas.
This commit is contained in:
Daeng Deni Mardaeni
2024-11-14 03:14:07 +07:00
parent 0601936ca5
commit 187282fe9f

View File

@@ -383,7 +383,8 @@
public function bulkDownload()
{
$dokumenIds = request()->get('jaminan'); // Expecting an array of dokumen_jaminan_id
$documents = DetailDokumenJaminan::where('dokumen_jaminan_id', $dokumenIds)->get();
$documents = DokumenJaminan::where('id', $dokumenIds)->with(['jenisJaminan', 'detail','debiture'])->get();
if ($documents->isEmpty()) {
return redirect()->back()->with('error', 'No documents found for the provided IDs.');
}
@@ -394,18 +395,24 @@
if ($zip->open($zipFilePath, ZipArchive::CREATE) === true) {
foreach ($documents as $document) {
if($document->dokumen_jaminan) {
$files = is_array(json_decode($document->dokumen_jaminan)) ? json_decode(
$document->dokumen_jaminan,
) : [$document->dokumen_jaminan];
$jenisJaminan = $document->debiture->permohonan->nomor_registrasi ?? 'Uncategorized';
$folderName = $this->sanitizeFolderName($jenisJaminan);
foreach ($files as $file) {
$filePath = storage_path('app/public/' . $file);
if (file_exists($filePath)) {
$zip->addFile($filePath, basename($filePath));
} else {
// Log or display an error message for missing files
return redirect()->back()->with('error', 'File not found: ' . $filePath);
foreach ($document->detail as $detail) {
if($detail->dokumen_jaminan) {
$folderJaminanName = $this->sanitizeFolderName($detail->jenisLegalitasJaminan->name?? 'Uncategorized');
$files = is_array(json_decode($detail->dokumen_jaminan))
? json_decode($detail->dokumen_jaminan)
: [$detail->dokumen_jaminan];
foreach ($files as $file) {
$filePath = storage_path('app/public/' . $file);
if (file_exists($filePath)) {
$zip->addFile($filePath, $folderName . '/' .$folderJaminanName.'/'.basename($filePath));
} else {
// Log or display an error message for missing files
\Log::warning('File not found: ' . $filePath);
}
}
}
}
@@ -423,7 +430,13 @@
'Content-Type' => 'application/zip',
'Content-Disposition' => 'attachment; filename="' . $zipFileName . '"',
'Content-Length' => filesize($zipFilePath),
])->deleteFileAfterSend(false);
])->deleteFileAfterSend(true);
}
private function sanitizeFolderName($name)
{
// Remove any characters that are not allowed in folder names
return preg_replace('/[^a-zA-Z0-9_\-]/', '_', $name);
}