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:
Daeng Deni Mardaeni
2025-07-12 13:41:46 +07:00
parent c264d63fa6
commit 92afe58e66
4 changed files with 98 additions and 14 deletions

View File

@@ -367,7 +367,12 @@ ini_set('max_execution_time', 300000);
// Ensure the temp directory exists
if (!file_exists(storage_path('app/temp'))) {
mkdir(storage_path('app/temp'), 0755, true);
mkdir(storage_path('app/temp'), 0777, true);
if (function_exists('chown') && posix_getuid() === 0) {
@chown(dirname('app/temp'), 'www-data'); // Gunakan www-data instead of root
@chgrp(dirname('app/temp'), 'www-data');
}
Log::info('Created temp directory for zip files');
}
@@ -733,7 +738,11 @@ ini_set('max_execution_time', 300000);
// Ensure the temp directory exists
if (!file_exists(storage_path('app/temp'))) {
mkdir(storage_path('app/temp'), 0755, true);
mkdir(storage_path('app/temp'), 0777, true);
if (function_exists('chown') && posix_getuid() === 0) {
@chown(dirname('app/temp'), 'www-data'); // Gunakan www-data instead of root
@chgrp(dirname('app/temp'), 'www-data');
}
}
// Create a new zip archive
@@ -796,7 +805,11 @@ ini_set('max_execution_time', 300000);
// Ensure the temp directory exists
if (!file_exists(storage_path('app/temp'))) {
mkdir(storage_path('app/temp'), 0755, true);
mkdir(storage_path('app/temp'), 0777, true);
if (function_exists('chown') && posix_getuid() === 0) {
@chown(dirname('app/temp'), 'www-data'); // Gunakan www-data instead of root
@chgrp(dirname('app/temp'), 'www-data');
}
}
// Download/copy the file to local temp storage
@@ -905,6 +918,16 @@ ini_set('max_execution_time', 300000);
// Pastikan direktori storage ada
Storage::disk('local')->makeDirectory($storagePath);
// Tambahkan permission dan ownership setelah membuat directory
$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');
}
}
// Path temporary untuk Browsershot
$tempPath = storage_path("app/{$fullStoragePath}");