Initial import
This commit is contained in:
132
application/controllers/tools/Downloadbackup.php
Normal file
132
application/controllers/tools/Downloadbackup.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
class Downloadbackup extends MY_Controller
|
||||
{
|
||||
var $db_onedev;
|
||||
|
||||
var $backup_folder;
|
||||
var $load;
|
||||
var $output;
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->backup_folder = "/data-sda/backup-db/";
|
||||
$this->load->helper(array('url', 'file')); // Load helper untuk URL dan file
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
echo "CPONE DOWNLOAD BACKUP API";
|
||||
}
|
||||
public function list_backup()
|
||||
{
|
||||
if (!$this->isLogin) {
|
||||
$this->sys_error("Invalid Token");
|
||||
exit;
|
||||
}
|
||||
$cpones = glob($this->backup_folder . 'cpone/*');
|
||||
$cpone_logs = glob($this->backup_folder . 'cpone_log/*');
|
||||
// Recursive function to scan files
|
||||
$allFile = [];
|
||||
$files = [];
|
||||
$files["cpone"] = [];
|
||||
foreach ($cpones as $file) {
|
||||
if (is_file($file)) {
|
||||
$files["cpone"][] = [
|
||||
'name' => basename($file),
|
||||
'full_path' => $file,
|
||||
'last_modified' => date("Y-m-d H:i:s", filemtime($file)),
|
||||
'size' => $this->formatSize(filesize($file)),
|
||||
'type' => 'cpone'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$files["cpone_log"] = [];
|
||||
foreach ($cpone_logs as $file) {
|
||||
if (is_file($file)) {
|
||||
$files["cpone_log"][] = [
|
||||
'name' => basename($file),
|
||||
'full_path' => $file,
|
||||
'last_modified' => date("Y-m-d H:i:s", filemtime($file)),
|
||||
'size' => $this->formatSize(filesize($file)),
|
||||
'type' => 'log'
|
||||
];
|
||||
}
|
||||
}
|
||||
// Sort files by last modified date descending
|
||||
usort($files["cpone"], function ($a, $b) {
|
||||
return strtotime($b['last_modified']) - strtotime($a['last_modified']);
|
||||
});
|
||||
usort($files["cpone_log"], function ($a, $b) {
|
||||
return strtotime($b['last_modified']) - strtotime($a['last_modified']);
|
||||
});
|
||||
|
||||
|
||||
// Return JSON response
|
||||
// echo json_encode(["status" => "OK", "result" => $files]);
|
||||
$this->sys_ok($files);
|
||||
}
|
||||
|
||||
private function formatSize($size)
|
||||
{
|
||||
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
$i = 0;
|
||||
while ($size >= 1024 && $i < count($units) - 1) {
|
||||
$size /= 1024;
|
||||
$i++;
|
||||
}
|
||||
return round($size, 2) . ' ' . $units[$i];
|
||||
}
|
||||
public function file()
|
||||
{
|
||||
if (!$this->isLogin) {
|
||||
$this->sys_error("Invalid Token");
|
||||
exit;
|
||||
}
|
||||
|
||||
$prm = $this->sys_input;
|
||||
$path = $prm['path'];
|
||||
$userid = $this->sys_user["M_UserID"];
|
||||
|
||||
if (!file_exists($path)) {
|
||||
$this->sys_error("File not exist");
|
||||
exit;
|
||||
}
|
||||
|
||||
$filesize = filesize($path);
|
||||
$filename = basename($path);
|
||||
|
||||
// Set header yang benar untuk mendukung progress
|
||||
header('Content-Type: application/octet-stream');
|
||||
header('Content-Disposition: attachment; filename="' . $filename . '"');
|
||||
header('Content-Length: ' . $filesize);
|
||||
header('Accept-Ranges: bytes');
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
header('Access-Control-Expose-Headers: Content-Length, Content-Disposition, Accept-Ranges');
|
||||
|
||||
// Matikan output buffering
|
||||
if (ob_get_level()) {
|
||||
ob_end_clean();
|
||||
}
|
||||
|
||||
// Untuk file kecil (<10MB), gunakan readfile
|
||||
if ($filesize < 10 * 1024 * 1024) {
|
||||
readfile($path);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Untuk file besar, gunakan chunked transfer
|
||||
$handle = fopen($path, 'rb');
|
||||
$chunkSize = 8192; // Gunakan chunk yang lebih kecil (8KB)
|
||||
|
||||
while (!feof($handle)) {
|
||||
echo fread($handle, $chunkSize);
|
||||
flush();
|
||||
}
|
||||
|
||||
fclose($handle);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user