143 lines
3.8 KiB
PHP
143 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Rclone;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\Process\Process;
|
|
use Symfony\Component\Process\Exception\ProcessFailedException;
|
|
use Exception;
|
|
|
|
/**
|
|
* Wrapper for the rclone command line utility
|
|
*
|
|
* Installation of rclone is required.
|
|
* Configuration of the mounts must be done before use.
|
|
* Tested using rclone v1.64.0
|
|
*/
|
|
final class Rclone
|
|
{
|
|
private const int MAX_RUNTIME = 4 * 3600; //4 hours maximum
|
|
protected string $version = "";
|
|
|
|
/**
|
|
* Create a new instance.
|
|
*
|
|
* Default it looks for "rclone" on the path.
|
|
* But the path can be configured to be absolute.
|
|
*
|
|
* @param string $rclonePath Relative or absolute path
|
|
* @param string $rcloneConfig Relative or absolute path to the rclone config file
|
|
*/
|
|
public function __construct(protected LoggerInterface $logger, protected string $rclonePath = "rclone", protected string $rcloneConfig = '')
|
|
{
|
|
$this->rclonePath = $rclonePath;
|
|
|
|
$process = $this->exec('--version');
|
|
if (! $process->isSuccessful()) {
|
|
throw new Exception("Check installation of rclone");
|
|
}
|
|
|
|
$this->version = explode("\n", $process->getOutput())[0];
|
|
|
|
if (! \str_contains($this->version, 'rclone')) {
|
|
throw new Exception("rclone not recognized");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the rclone version.
|
|
*
|
|
* @return string Version string
|
|
*/
|
|
public function getVersion(): string
|
|
{
|
|
return $this->version;
|
|
}
|
|
|
|
/**
|
|
* Calculate the size of a mount/path.
|
|
*
|
|
* @param string $path mount/path.
|
|
*
|
|
* @return int Size in bytes
|
|
*/
|
|
public function getSize(string $path): int
|
|
{
|
|
$process = $this->exec('size', ['--json', $path]);
|
|
|
|
if (! $process->isSuccessful()) {
|
|
throw new Exception($process->getErrorOutput());
|
|
}
|
|
|
|
/**
|
|
* @var array{bytes: int}
|
|
*/
|
|
$output = json_decode($process->getOutput(), TRUE);
|
|
return $output['bytes'];
|
|
}
|
|
|
|
/**
|
|
* Copy from source to destination.
|
|
*
|
|
* @param $src Source mount and path
|
|
* @param $dest Destination mount and path
|
|
* @param string[] $additionalOptions Additional options
|
|
*/
|
|
public function copy(string $src, string $dest, array $additionalOptions = []): void
|
|
{
|
|
$options = [];
|
|
|
|
$options[] = $src;
|
|
$options[] = $dest;
|
|
|
|
foreach ($additionalOptions as $key => $value) {
|
|
$options[] = '--' . (string)$key;
|
|
$options[] = $value;
|
|
}
|
|
|
|
$process = $this->exec('copy', $options);
|
|
if (! $process->isSuccessful()) {
|
|
throw new Exception($process->getErrorOutput());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Execute a command on the rclone binary.
|
|
*
|
|
* @param string $command Top level Rclone command
|
|
* @param array<String> $options Array of additional options
|
|
*
|
|
* @return Process Instance.
|
|
*/
|
|
private function exec(string $command, array $options = []): Process
|
|
{
|
|
$rcloneconfig = [];
|
|
if ($this->rcloneConfig != '') {
|
|
$rcloneconfig = ['--config', $this->rcloneConfig];
|
|
}
|
|
|
|
$process = new Process(
|
|
array_merge(
|
|
[$this->rclonePath],
|
|
$rcloneconfig,
|
|
[$command],
|
|
$options
|
|
)
|
|
);
|
|
|
|
$this->logger->info("Execute command", [$process->getCommandLine()]);
|
|
|
|
$process->setTimeout(self::MAX_RUNTIME);
|
|
$process->run();
|
|
|
|
// executes after the command finishes
|
|
if (! $process->isSuccessful()) {
|
|
$this->logger->error("Failed execution");
|
|
}
|
|
$this->logger->info("Return code", [$process->getExitCode()]);
|
|
return $process;
|
|
}
|
|
}
|