122 lines
4.4 KiB
PHP
122 lines
4.4 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
class Get_file extends CI_Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->load->model('cronjob/Model_cron_file');
|
|
$this->load->database();
|
|
}
|
|
|
|
public function download($id = null)
|
|
{
|
|
$access_token = "EAAN0PjgeNRsBPqGqvuv48h90PQ6ZCo1StyQ5vSFBZB7UiyjFLxneRvz4xE0a66HbZBmY0rLo7cpsQV0Ecey3Ggy9UK36MJiSxQXebMexHLyKlFSTAbf6AUHO7EVQEaJvINxA51j4TMiq3InlIy7Yn9muxITYGvjB403uXcuD71MieL7Q1KEm8PXopiRqgZDZD"; // 🔑 WA Business API Access Token
|
|
|
|
if (empty($access_token)) {
|
|
$this->response_json(false, "ACCESS TOKEN tidak ditemukan");
|
|
return;
|
|
}
|
|
|
|
if (empty($id)) {
|
|
$this->response_json(false, "Parameter ID wajib diisi");
|
|
return;
|
|
}
|
|
|
|
// Ambil file yang masih pending
|
|
$file = $this->db->get_where('message_files', [
|
|
'id' => $id
|
|
])->row_array();
|
|
|
|
if (!$file) {
|
|
$this->response_json(false, "Tidak ada file pending dengan ID {$id}");
|
|
return;
|
|
}
|
|
|
|
$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;
|
|
|
|
$max_attempts = 3;
|
|
$success = false;
|
|
|
|
for ($attempt = 1; $attempt <= $max_attempts; $attempt++) {
|
|
echo "▶️ Percobaan {$attempt}/{$max_attempts} untuk file ID {$file_id}\n";
|
|
|
|
$content = $this->Model_cron_file->download_file_from_meta($meta_url, $access_token);
|
|
|
|
if ($content !== false && strlen($content) > 0) {
|
|
// ✅ Update status menjadi downloading
|
|
$this->Model_cron_file->update_file_status($file_id, 'downloading');
|
|
|
|
// Simpan file ke folder lokal
|
|
$file_url = $this->Model_cron_file->save_file_to_local($customer_number, $filename, $content);
|
|
$file_path = FCPATH . ltrim($file_url, '/');
|
|
$file_size = file_exists($file_path) ? filesize($file_path) : 0;
|
|
|
|
if ($file_size > 100) {
|
|
// ✅ Update status sukses
|
|
$this->Model_cron_file->update_file_status($file_id, 'downloaded', $file_url, $filename, $file_size);
|
|
|
|
$this->response_json(true, "Berhasil download {$filename} ({$file_size} bytes)", [
|
|
'file_id' => $file_id,
|
|
'url' => $file_url,
|
|
'size' => $file_size
|
|
]);
|
|
|
|
$success = true;
|
|
break; // selesai, keluar dari loop
|
|
}
|
|
}
|
|
|
|
// ⚠️ Jika belum berhasil, beri info dan tunggu sebelum retry
|
|
if (!$success && $attempt < $max_attempts) {
|
|
echo "⚠️ File belum siap diunduh (percobaan {$attempt}). Menunggu 2 detik sebelum retry...\n";
|
|
sleep(2);
|
|
}
|
|
}
|
|
|
|
if (!$success) {
|
|
// ❌ Setelah 3x gagal, biarkan status tetap pending
|
|
log_message('error', "File ID {$file_id} gagal diunduh setelah {$max_attempts} percobaan. Tetap pending.");
|
|
$this->response_json(false, "File belum siap diunduh, masih pending setelah {$max_attempts} percobaan");
|
|
}
|
|
}
|
|
|
|
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';
|
|
}
|
|
|
|
/**
|
|
* Fungsi respon JSON standar agar bisa dibaca dari Node.js
|
|
*/
|
|
private function response_json($success, $message, $data = [])
|
|
{
|
|
$response = [
|
|
'success' => $success,
|
|
'message' => $message,
|
|
'data' => $data
|
|
];
|
|
header('Content-Type: application/json');
|
|
echo json_encode($response);
|
|
}
|
|
}
|