load->model('cronjob/Model_cron_file'); $this->load->database(); // Jalankan hanya dari CLI atau cron job (opsional) if (!$this->input->is_cli_request()) { // Boleh nonaktifkan kalau kamu mau bisa testing via browser // show_error('This script can only be run from CLI or cron.', 403); } } /** * Endpoint cron / worker untuk download file WhatsApp (gambar/audio/video) */ public function index() { // Ganti dengan access token WA Business API kamu $access_token = "EAAN0PjgeNRsBPqGqvuv48h90PQ6ZCo1StyQ5vSFBZB7UiyjFLxneRvz4xE0a66HbZBmY0rLo7cpsQV0Ecey3Ggy9UK36MJiSxQXebMexHLyKlFSTAbf6AUHO7EVQEaJvINxA51j4TMiq3InlIy7Yn9muxITYGvjB403uXcuD71MieL7Q1KEm8PXopiRqgZDZD"; // <--- PENTING: isi dengan token aktif if (empty($access_token)) { echo "❌ ACCESS TOKEN tidak ditemukan. Mohon isi di controller.\n"; return; } $files = $this->Model_cron_file->get_pending_files(10); if (empty($files)) { echo "ℹ️ Tidak ada file pending untuk diunduh.\n"; return; } foreach ($files as $file) { $file_id = $file['id']; $customer_number = $file['customer_number']; $meta_url = $file['meta_url']; $mime_type = $file['mime_type'] ?? 'application/octet-stream'; $ext = $this->get_extension_by_mime($mime_type); $filename = "file_{$file_id}." . $ext; echo "\n▶️ Proses file ID {$file_id} ({$mime_type})...\n"; // Update status awal $this->Model_cron_file->update_file_status($file_id, 'downloading'); // Ambil konten binary dari Meta Graph API $content = $this->Model_cron_file->download_file_from_meta($meta_url, $access_token); if ($content !== false && strlen($content) > 0) { // Simpan ke folder lokal $file_url = $this->Model_cron_file->save_file_to_local($customer_number, $filename, $content); // Cek ukuran file $file_path = FCPATH . ltrim($file_url, '/'); $file_size = file_exists($file_path) ? filesize($file_path) : 0; // Update status sukses $this->Model_cron_file->update_file_status($file_id, 'downloaded', $file_url, $filename, $file_size); echo "✅ Berhasil download {$filename} ({$file_size} bytes)\n"; } else { // Update status error $this->Model_cron_file->update_file_status($file_id, 'error'); echo "❌ Gagal download file {$filename}\n"; // Tambahkan log detail log_message('error', "Gagal ambil file ID {$file_id} dari Meta URL: {$meta_url}"); } } echo "\nSelesai memproses file pending.\n"; } /** * Tentukan ekstensi file berdasarkan MIME type */ private function get_extension_by_mime($mime) { $map = [ 'image/jpeg' => 'jpg', 'image/png' => 'png', 'image/gif' => 'gif', 'video/mp4' => 'mp4', 'audio/mpeg' => 'mp3', 'audio/ogg' => 'ogg', 'application/pdf' => 'pdf', 'application/msword' => 'doc', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx', 'text/plain' => 'txt', ]; return $map[$mime] ?? 'bin'; } }