feat(webstatement): tambahkan fitur unlock PDF file dan jadwal otomatisasi harian

- Menambahkan command baru `webstatement:unlock-pdf` untuk membuka file PDF yang dilindungi password:
  - Dapat menerima parameter `directory` untuk menetapkan direktori file sumber.
  - Opsi `--password` untuk menentukan password yang digunakan dalam proses unlock, dengan default `123456`.
  - Menampilkan log proses unlock PDF dengan pesan sukses atau error.
- Membuat job baru `UnlockPdfJob` untuk menangani proses unlock PDF secara asinkron:
  - Memindai direktori utama berdasarkan struktur folder (tahun dan ID).
  - Membuka proteksi file PDF dengan menggunakan library `qpdf`.
  - Menghasilkan file PDF yang telah didekripsi di direktori yang sama dengan nama file ekstensi `.dec.pdf`.
  - Melakukan logging untuk setiap file yang berhasil diproses atau ketika terjadi error.
  - Menghindari duplikasi dengan memeriksa keberadaan file decrypted sebelumnya.
- Memperbarui `WebstatementServiceProvider`:
  - Mendaftarkan command `UnlockPdf` untuk digunakan dalam aplikasi.
  - Menambah jadwal otomatisasi harian pada pukul 09:30 untuk menjalankan command `webstatement:unlock-pdf`.
  - Logging hasil proses executables ke file `logs/unlock-pdf.log`.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
Daeng Deni Mardaeni
2025-06-02 19:59:33 +07:00
parent 173b229f07
commit d326cce6e0
3 changed files with 193 additions and 1 deletions

135
app/Jobs/UnlockPdfJob.php Normal file
View File

@@ -0,0 +1,135 @@
<?php
namespace Modules\Webstatement\Jobs;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\File;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class UnlockPdfJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $baseDirectory;
protected $password;
/**
* Create a new job instance.
*
* @param string $baseDirectory Base directory path to scan
* @param string $password Password to unlock PDF files
*/
public function __construct(string $baseDirectory, string $password = '123456')
{
$this->baseDirectory = $baseDirectory;
$this->password = $password;
}
/**
* Execute the job.
*/
public function handle(): void
{
try {
Log::info("Starting PDF unlock process in directory: {$this->baseDirectory}");
// Check if directory exists
if (!File::isDirectory($this->baseDirectory)) {
Log::error("Directory not found: {$this->baseDirectory}");
return;
}
// Get all subdirectories (year folders like 202505)
$yearDirectories = File::directories($this->baseDirectory);
foreach ($yearDirectories as $yearDirectory) {
$this->processYearDirectory($yearDirectory);
}
Log::info("PDF unlock process completed successfully.");
} catch (Exception $e) {
Log::error("Error unlocking PDF files: " . $e->getMessage());
}
}
/**
* Process a year directory (e.g., 202505)
*
* @param string $yearDirectory Directory path to process
*/
protected function processYearDirectory(string $yearDirectory): void
{
try {
// Get all ID directories (e.g., ID0010001)
$idDirectories = File::directories($yearDirectory);
foreach ($idDirectories as $idDirectory) {
$this->processIdDirectory($idDirectory);
}
} catch (Exception $e) {
Log::error("Error processing year directory {$yearDirectory}: " . $e->getMessage());
}
}
/**
* Process an ID directory (e.g., ID0010001)
*
* @param string $idDirectory Directory path to process
*/
protected function processIdDirectory(string $idDirectory): void
{
try {
// Get all PDF files in the directory
$pdfFiles = File::glob($idDirectory . '/*.pdf');
foreach ($pdfFiles as $pdfFile) {
$this->unlockPdf($pdfFile);
}
} catch (Exception $e) {
Log::error("Error processing ID directory {$idDirectory}: " . $e->getMessage());
}
}
/**
* Unlock a password-protected PDF file
*
* @param string $pdfFilePath Path to PDF file
*/
protected function unlockPdf(string $pdfFilePath): void
{
try {
$filename = pathinfo($pdfFilePath, PATHINFO_FILENAME);
$directory = pathinfo($pdfFilePath, PATHINFO_DIRNAME);
$decryptedPdfPath = $directory . '/' . $filename . '.dec.pdf';
// Skip if the decrypted file already exists
if (File::exists($decryptedPdfPath)) {
Log::info("Decrypted file already exists: {$decryptedPdfPath}");
return;
}
// Create qpdf command
$command = ['qpdf', '--password=' . $this->password, '--decrypt', $pdfFilePath, $decryptedPdfPath];
// Execute the command
$process = new Process($command);
$process->run();
// Check if the command was successful
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
Log::info("Unlocked PDF: {$pdfFilePath} to {$decryptedPdfPath}");
} catch (Exception $e) {
Log::error("Error unlocking PDF {$pdfFilePath}: " . $e->getMessage());
}
}
}