127 lines
3.4 KiB
PHP
127 lines
3.4 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
class Model_cron_file extends CI_Model
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Ambil file yang statusnya 'pending'
|
|
* @param int $limit
|
|
* @return array
|
|
*/
|
|
public function get_pending_files($limit = 10)
|
|
{
|
|
return $this->db
|
|
->where('status', 'pending')
|
|
->order_by('created_at', 'ASC')
|
|
->limit($limit)
|
|
->get('message_files')
|
|
->result_array();
|
|
}
|
|
|
|
/**
|
|
* Update status file
|
|
*/
|
|
public function update_file_status($id, $status, $file_url = null, $file_name = null, $file_size = null)
|
|
{
|
|
$data = [
|
|
'status' => $status,
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
];
|
|
|
|
if ($file_url !== null) {
|
|
$data['file_url'] = $file_url;
|
|
}
|
|
|
|
if ($file_name !== null) {
|
|
$data['file_name'] = $file_name;
|
|
}
|
|
|
|
if ($file_size !== null) {
|
|
$data['file_size'] = $file_size;
|
|
}
|
|
|
|
$this->db->where('id', $id)->update('message_files', $data);
|
|
}
|
|
|
|
|
|
/**
|
|
* Simpan file ke folder uploads/{customer_number}/
|
|
*/
|
|
public function save_file_to_local($customer_number, $filename, $content)
|
|
{
|
|
$folder = FCPATH . "uploads/{$customer_number}/";
|
|
if (!is_dir($folder)) mkdir($folder, 0777, true);
|
|
$path = $folder . $filename;
|
|
file_put_contents($path, $content);
|
|
return "uploads/{$customer_number}/{$filename}";
|
|
}
|
|
|
|
/**
|
|
* Ambil file binary dari Meta Graph API
|
|
*/
|
|
public function download_file_from_meta($meta_url, $access_token)
|
|
{
|
|
// --- Jika URL dari DB tidak punya 'fields=url', tambahkan otomatis
|
|
if (preg_match('/^https:\/\/graph\.facebook\.com\/v\d+\.\d+\/\d+$/', $meta_url)) {
|
|
$meta_url .= '?fields=url';
|
|
}
|
|
|
|
// --- Step 1: Ambil URL file asli dari Meta Graph API
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $meta_url,
|
|
CURLOPT_HTTPHEADER => ["Authorization: Bearer $access_token"],
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 15,
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
]);
|
|
$response = curl_exec($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if ($http_code !== 200 || !$response) {
|
|
log_message('error', "Failed to fetch meta URL (HTTP $http_code): $error | $response");
|
|
return false;
|
|
}
|
|
|
|
$result = json_decode($response, true);
|
|
if (!isset($result['url'])) {
|
|
log_message('error', 'Meta Graph response missing "url": ' . $response);
|
|
return false;
|
|
}
|
|
|
|
$download_url = $result['url'];
|
|
|
|
// --- Step 2: Download file binary dari URL file asli
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $download_url,
|
|
CURLOPT_HTTPHEADER => ["Authorization: Bearer $access_token"],
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_TIMEOUT => 30,
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
]);
|
|
$file_content = curl_exec($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if ($http_code !== 200 || !$file_content) {
|
|
log_message('error', "Failed to download file (HTTP $http_code): $error");
|
|
return false;
|
|
}
|
|
|
|
return $file_content;
|
|
}
|
|
|
|
|
|
|
|
}
|