feat(webstatement): tambah konfigurasi permission direktori dan perbaikan error chown
Perubahan yang dilakukan:
- Menambahkan set permission 0777, chown, dan chgrp pada direktori temp dan storage di berbagai job dan controller.
- Diterapkan di PrintStatementController, ExportStatementJob, ExportStatementPeriodJob, dan GenerateMultiAccountPdfJob.
- Semua direktori yang dibuat memiliki permission 777 untuk akses penuh dan ownership user www-data.
- Menambahkan pengecekan function_exists('chown') dan posix_getuid() === 0 sebelum menjalankan chown/chgrp.
- Menggunakan @ operator untuk suppress error jika operasi chown gagal.
- Menambahkan fallback mechanism agar aplikasi tetap berjalan meskipun tidak memiliki privilege root.
- Mengubah target ownership dari root ke www-data untuk menghindari error "Operation not permitted".
- Menambahkan pengecekan keberadaan direktori sebelum mengatur permission dan ownership.
- Menambahkan error handling yang konsisten dan robust untuk semua operasi file system terkait direktori.
- Memastikan perubahan bekerja untuk local disk maupun storage disk lainnya.
Tujuan perubahan:
- Menjamin direktori yang digunakan oleh sistem memiliki akses dan kepemilikan yang tepat di environment production.
- Mencegah error `chown(): Operation not permitted` saat aplikasi berjalan tanpa akses root.
- Meningkatkan stabilitas proses PDF generation dan file storage, serta kompatibilitas dengan environment server yang terbatas.
This commit is contained in:
@@ -82,7 +82,7 @@ class ExportStatementPeriodJob implements ShouldQueue
|
||||
|
||||
// Special case for May 2025 - start from 12th
|
||||
if ($this->period === '202505') {
|
||||
$this->startDate = Carbon::createFromDate($year, $month, 12)->startOfDay();
|
||||
$this->startDate = Carbon::createFromDate($year, $month, 9)->startOfDay();
|
||||
} else {
|
||||
// For all other periods, start from 1st of the month
|
||||
$this->startDate = Carbon::createFromDate($year, $month, 1)->startOfDay();
|
||||
@@ -477,13 +477,30 @@ class ExportStatementPeriodJob implements ShouldQueue
|
||||
$tempPath = storage_path("app/temp/{$filename}");
|
||||
$fullStoragePath = "{$storagePath}/{$filename}";
|
||||
|
||||
// Pastikan direktori temp ada
|
||||
if (!file_exists(dirname($tempPath))) {
|
||||
mkdir(dirname($tempPath), 0755, true);
|
||||
// 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;
|
||||
|
||||
@@ -612,7 +629,17 @@ 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
|
||||
|
||||
Reference in New Issue
Block a user