From e6c46701ce37c6d31bb370660383493df483e605 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Tue, 15 Jul 2025 16:40:04 +0700 Subject: [PATCH] refactor(jobs): hapus manajemen izin file (chmod, chown, chgrp) Perubahan yang dilakukan: - ExportStatementJob.php: - Menghapus kode chmod(0777), chown, dan chgrp pada direktori basePath dan accountPath. - Menyederhanakan fungsi exportToCsv() dengan hanya menggunakan Storage::makeDirectory() tanpa pengaturan izin manual. - ExportStatementPeriodJob.php: - Menghapus proses chmod(0777), chown, dan chgrp dari generatePdf() dan exportToCsv(). - Mempertahankan mkdir() dengan parameter 0777 untuk kompatibilitas permission default, tanpa manipulasi ownership. - Menghapus pengecekan posix_getuid() dan function_exists() yang sebelumnya digunakan untuk memvalidasi chown. - GenerateMultiAccountPdfJob.php: - Menghapus semua proses chmod(0777), chown, dan chgrp pada direktori penyimpanan. - Menyederhanakan fungsi generateAccountPdf() dengan menghilangkan pengaturan ownership manual. Tujuan perubahan: - Menyederhanakan kode dan menghilangkan proses manajemen izin file yang tidak perlu. - Menghilangkan ketergantungan pada fungsi sistem operasi terkait chmod, chown, dan chgrp. - Mengurangi kompleksitas dan meminimalkan potensi error saat runtime. - Meningkatkan portabilitas aplikasi agar dapat berjalan di berbagai environment seperti Linux, Windows, dan Docker tanpa kendala perizinan file. - Menghilangkan risiko keamanan akibat perubahan ownership file secara manual. - Mempermudah deployment dan maintenance dengan kode yang lebih clean dan aman. Catatan: - Pembuatan direktori tetap menggunakan Laravel Storage facade atau mkdir() untuk kebutuhan tertentu. - Tidak ada perubahan pada logika bisnis utama dari proses export PDF dan CSV. - Semua fitur export tetap berjalan seperti sebelumnya tanpa pengaturan file permission manual. --- app/Jobs/ExportStatementJob.php | 24 +-------- app/Jobs/ExportStatementPeriodJob.php | 66 ++++++++----------------- app/Jobs/GenerateMultiAccountPdfJob.php | 10 ---- 3 files changed, 21 insertions(+), 79 deletions(-) diff --git a/app/Jobs/ExportStatementJob.php b/app/Jobs/ExportStatementJob.php index da7527e..9005df7 100644 --- a/app/Jobs/ExportStatementJob.php +++ b/app/Jobs/ExportStatementJob.php @@ -156,7 +156,7 @@ 'description' => $this->generateNarrative($item), 'end_balance' => $runningBalance, 'actual_date' => $actualDate, - 'recipt_no' => $item->ft?->recipt_no ?? '-', + 'recipt_no' => $item->ft?->recipt_no ?? '-', 'created_at' => now(), 'updated_at' => now(), ]; @@ -371,31 +371,9 @@ if (!empty($this->client)) { // Di fungsi exportToCsv untuk basePath Storage::disk($this->disk)->makeDirectory($basePath); - // Tambahkan permission dan ownership setelah membuat directory - if ($this->disk === 'local') { - $fullPath = storage_path("app/{$basePath}"); - if (is_dir($fullPath)) { - chmod($fullPath, 0777); - if (function_exists('chown') && posix_getuid() === 0) { - @chown(dirname($fullPath), 'www-data'); // Gunakan www-data instead of root - @chgrp(dirname($fullPath), 'www-data'); - } - } - } // Untuk accountPath Storage::disk($this->disk)->makeDirectory($accountPath); - // Tambahkan permission dan ownership setelah membuat directory - if ($this->disk === 'local') { - $fullPath = storage_path("app/{$accountPath}"); - if (is_dir($fullPath)) { - chmod($fullPath, 0777); - if (function_exists('chown') && posix_getuid() === 0) { - @chown(dirname($fullPath), 'www-data'); // Gunakan www-data instead of root - @chgrp(dirname($fullPath), 'www-data'); - } - } - } } $filePath = "{$accountPath}/{$this->fileName}"; diff --git a/app/Jobs/ExportStatementPeriodJob.php b/app/Jobs/ExportStatementPeriodJob.php index 107a9af..7534243 100644 --- a/app/Jobs/ExportStatementPeriodJob.php +++ b/app/Jobs/ExportStatementPeriodJob.php @@ -201,25 +201,27 @@ class ExportStatementPeriodJob implements ShouldQueue $processedData = []; foreach ($entries as $item) { - $globalSequence++; - $runningBalance += (float) $item->amount_lcy; + $globalSequence++; + $runningBalance += (float) $item->amount_lcy; - $actualDate = $this->formatActualDate($item); + $transactionDate = $this->formatTransactionDate($item); + $actualDate = $this->formatActualDate($item); - $processedData[] = [ - 'account_number' => $this->account_number, - 'period' => $this->period, - 'sequence_no' => $globalSequence, - 'transaction_date' => $item->booking_date, - 'reference_number' => $item->trans_reference, - 'transaction_amount' => $item->amount_lcy, - 'transaction_type' => $item->amount_lcy < 0 ? 'D' : 'C', - 'description' => $this->generateNarrative($item), - 'end_balance' => $runningBalance, - 'actual_date' => $actualDate, - 'created_at' => now(), - 'updated_at' => now(), - ]; + $processedData[] = [ + 'account_number' => $this->account_number, + 'period' => $this->period, + 'sequence_no' => $globalSequence, + 'transaction_date' => $item->booking_date, + 'reference_number' => $item->trans_reference, + 'transaction_amount' => $item->amount_lcy, + 'transaction_type' => $item->amount_lcy < 0 ? 'D' : 'C', + 'description' => $this->generateNarrative($item), + 'end_balance' => $runningBalance, + 'actual_date' => $actualDate, + 'recipt_no' => $item->ft?->recipt_no ?? '-', + 'created_at' => now(), + 'updated_at' => now(), + ]; } return $processedData; @@ -480,27 +482,10 @@ class ExportStatementPeriodJob implements ShouldQueue // Buat direktori temp jika belum ada if (!is_dir(dirname($tempPath))) { mkdir(dirname($tempPath), 0777, true); - // Tambahkan pengecekan function dan error handling - if (function_exists('chown') && posix_getuid() === 0) { - @chown(dirname($tempPath), 'www-data'); // Gunakan www-data instead of root - @chgrp(dirname($tempPath), 'www-data'); - } } // Pastikan direktori storage ada Storage::makeDirectory($storagePath); - // Tambahkan permission dan ownership setelah membuat directory - if ($this->disk === 'local') { - $fullPath = storage_path("app/{$storagePath}"); - if (is_dir($fullPath)) { - chmod($fullPath, 0777); - // Tambahkan pengecekan function dan error handling - if (function_exists('chown') && posix_getuid() === 0) { - @chown($fullPath, 'www-data'); // Gunakan www-data instead of root - @chgrp($fullPath, 'www-data'); - } - } - } $period = $this->period; @@ -629,17 +614,7 @@ class ExportStatementPeriodJob implements ShouldQueue $storagePath = "statements/{$this->period}/{$account->branch_code}"; Storage::disk($this->disk)->makeDirectory($storagePath); - // Tambahkan permission dan ownership setelah membuat directory - if ($this->disk === 'local') { - $fullPath = storage_path("app/{$storagePath}"); - if (is_dir($fullPath)) { - chmod($fullPath, 0777); - if (function_exists('chown') && posix_getuid() === 0) { - @chown(dirname($fullPath), 'www-data'); // Gunakan www-data instead of root - @chgrp(dirname($fullPath), 'www-data'); - } - } - } + $filePath = "{$storagePath}/{$this->fileName}"; // Delete existing file if it exists @@ -669,7 +644,6 @@ class ExportStatementPeriodJob implements ShouldQueue // Write to file incrementally to reduce memory usage Storage::disk($this->disk)->append($filePath, $csvContent); - $csvContent = ''; // Reset content after writing }); Log::info("Statement exported to {$this->disk} disk: {$filePath}"); diff --git a/app/Jobs/GenerateMultiAccountPdfJob.php b/app/Jobs/GenerateMultiAccountPdfJob.php index 86d3758..cdb2961 100644 --- a/app/Jobs/GenerateMultiAccountPdfJob.php +++ b/app/Jobs/GenerateMultiAccountPdfJob.php @@ -239,16 +239,6 @@ class GenerateMultiAccountPdfJob implements ShouldQueue // Ensure directory exists Storage::disk('local')->makeDirectory($storagePath); - // Tambahkan permission dan ownership setelah membuat directory - $fullPath = storage_path("app/{$storagePath}"); - if (is_dir($fullPath)) { - chmod($fullPath, 0777); - // Tambahkan pengecekan function dan error handling untuk chown - if (function_exists('chown') && posix_getuid() === 0) { - @chown($fullPath, 'www-data'); // Gunakan www-data instead of root - @chgrp($fullPath, 'www-data'); - } - } // Generate PDF path $pdfPath = storage_path("app/{$fullStoragePath}");