PSR12 code standard.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
@ -1,18 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rclone;
|
||||
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
use Psr\Log\NullLogger;
|
||||
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.53.3-DEV
|
||||
@ -24,76 +23,72 @@ class Rclone
|
||||
protected string $rclonePath;
|
||||
/**
|
||||
* Global options
|
||||
*
|
||||
* @var array<string>
|
||||
*
|
||||
* @var array<string>
|
||||
*/
|
||||
protected array $globalOptions = [];
|
||||
|
||||
protected string $version = "";
|
||||
|
||||
/**
|
||||
* Create a new instance
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
function __construct(string $rclonePath = "rclone")
|
||||
public function __construct(string $rclonePath = "rclone")
|
||||
{
|
||||
$this->rclonePath = $rclonePath;
|
||||
$this->setLogger(new NullLogger);
|
||||
try
|
||||
{
|
||||
$this->setLogger(new NullLogger());
|
||||
|
||||
try {
|
||||
$version = $this->exec('--version');
|
||||
$this->version = explode("\n", $version)[0];
|
||||
}
|
||||
catch(ProcessFailedException $e)
|
||||
{
|
||||
} catch (ProcessFailedException $e) {
|
||||
throw new Exception("Check installation of rclone");
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (!\str_contains($this->version, 'rclone')) {
|
||||
throw new Exception("Rclone not recognized");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the rclone version
|
||||
*
|
||||
* Get the rclone version.
|
||||
*
|
||||
* @return string Version string
|
||||
*/
|
||||
function getVersion(): string
|
||||
public function getVersion(): string
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the size of a mount/path
|
||||
*
|
||||
* Calculate the size of a mount/path.
|
||||
*
|
||||
* @param string $path mount/path.
|
||||
*
|
||||
*
|
||||
* @return int Size in bytes
|
||||
*/
|
||||
function getSize(string $path): int
|
||||
public function getSize(string $path): int
|
||||
{
|
||||
$output = $this->exec('size', ['--json', $path]);
|
||||
return (int)json_decode($output)->bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy from src to dest
|
||||
*
|
||||
* Copy from source to destination.
|
||||
*
|
||||
* @param $src Source mount and path
|
||||
* @param $dest Destination mount and path
|
||||
* @param $bandwidth Bandwidth limit provided as string
|
||||
*
|
||||
*
|
||||
* @return string Stdout from command
|
||||
*/
|
||||
function copy(string $src, string $dest, string $bandwidth = null): string
|
||||
{
|
||||
public function copy(string $src, string $dest, string $bandwidth = null): string
|
||||
{
|
||||
$options = array();
|
||||
|
||||
$options[] = $src;
|
||||
@ -107,14 +102,14 @@ class Rclone
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a command on the rclone binary
|
||||
*
|
||||
* Execute a command on the rclone binary.
|
||||
*
|
||||
* @param string $command Top level Rclone command
|
||||
* @param array<String> $options Array of additional options
|
||||
*
|
||||
*
|
||||
* @return string stdout data.
|
||||
*/
|
||||
protected function exec(string $command, array $options = array()) : string
|
||||
protected function exec(string $command, array $options = array()): string
|
||||
{
|
||||
$process = new Process(
|
||||
array_merge(
|
||||
@ -127,8 +122,8 @@ class Rclone
|
||||
if ($this->logger instanceof LoggerInterface) {
|
||||
$this->logger->info("Execute command", [$process->getCommandLine()]);
|
||||
}
|
||||
|
||||
$process->setTimeout(4*3600);
|
||||
|
||||
$process->setTimeout(4 * 3600);
|
||||
$process->run();
|
||||
|
||||
// executes after the command finishes
|
||||
@ -143,4 +138,4 @@ class Rclone
|
||||
}
|
||||
return $process->getOutput();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user