2023-05-26 11:47:40 +00:00
|
|
|
<?php
|
|
|
|
namespace App\Rclone;
|
|
|
|
|
|
|
|
use Symfony\Component\Process\Process;
|
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException;
|
|
|
|
|
2023-05-26 12:14:21 +00:00
|
|
|
class Rclone
|
|
|
|
{
|
|
|
|
protected $rclone_path;
|
|
|
|
protected $global_options = [];
|
2023-05-26 11:47:40 +00:00
|
|
|
|
|
|
|
function __construct($rclone_path = "rclone")
|
|
|
|
{
|
|
|
|
$this->rclone_path = $rclone_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVersion()
|
|
|
|
{
|
|
|
|
return $this->exec('--version');
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSize($path)
|
|
|
|
{
|
|
|
|
$output = $this->exec('size', ['--json', $path]);
|
|
|
|
|
|
|
|
return (int)json_decode($output[0])->bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
function copy($src, $dest, $bandwidth = null)
|
|
|
|
{
|
|
|
|
$options = array();
|
|
|
|
|
|
|
|
$options[] = $src;
|
|
|
|
$options[] = $dest;
|
2023-05-26 12:14:21 +00:00
|
|
|
if ($bandwidth) {
|
2023-05-26 11:47:40 +00:00
|
|
|
$options[] = "--bwlimit";
|
|
|
|
$options[] = $bandwidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->exec('copy', $options);
|
|
|
|
}
|
|
|
|
|
2023-05-26 12:14:21 +00:00
|
|
|
protected function exec(string $command, array $options = array())
|
2023-05-26 11:47:40 +00:00
|
|
|
{
|
2023-05-26 12:14:21 +00:00
|
|
|
$process = new Process(
|
|
|
|
array_merge(
|
|
|
|
[$this->rclone_path],
|
|
|
|
$this->global_options,
|
|
|
|
[$command],
|
|
|
|
$options
|
|
|
|
)
|
|
|
|
);
|
2023-05-26 11:47:40 +00:00
|
|
|
$process->setTimeout(4*3600);
|
|
|
|
$process->run();
|
|
|
|
|
|
|
|
// executes after the command finishes
|
|
|
|
if (!$process->isSuccessful()) {
|
|
|
|
throw new ProcessFailedException($process);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $process->getOutput();
|
|
|
|
}
|
|
|
|
}
|