add webdav sync command
This commit is contained in:
parent
732f2dab51
commit
1a670442bf
|
@ -64,6 +64,14 @@ class Client
|
|||
);
|
||||
}
|
||||
|
||||
public function ls(string $directory): array
|
||||
{
|
||||
return $this->client->propfind($this->baseUrl.'/'.$directory, [
|
||||
'{DAV:}displayname',
|
||||
'{DAV:}getcontentlength',
|
||||
], 1);
|
||||
}
|
||||
|
||||
public function exists(string $file): bool
|
||||
{
|
||||
$response = $this->client->request('GET', $this->baseUrl.'/'.$file);
|
||||
|
|
68
src/Command/WebdavBillSyncCommand.php
Normal file
68
src/Command/WebdavBillSyncCommand.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Api\Webdav\Client;
|
||||
use App\Repository\BillRepositoryQuery;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'webdav:bill:sync',
|
||||
description: 'Sync bills',
|
||||
)]
|
||||
class WebdavBillSyncCommand extends Command
|
||||
{
|
||||
protected Client $client;
|
||||
protected BillRepositoryQuery $query;
|
||||
|
||||
public function __construct(Client $client, BillRepositoryQuery $query)
|
||||
{
|
||||
$this->client = $client;
|
||||
$this->query = $query;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
chdir(__DIR__.'/../../public');
|
||||
|
||||
$remoteFiles = $this->client->ls('/');
|
||||
$localFiles = [];
|
||||
|
||||
foreach ($this->query->find() as $entity) {
|
||||
$localFiles[basename($entity->getFile())] = $entity->getFile();
|
||||
}
|
||||
|
||||
foreach ($localFiles as $basename => $file) {
|
||||
if (!$this->client->exists($basename)) {
|
||||
$this->client->sendFile($file, $basename);
|
||||
$output->writeln(sprintf(
|
||||
'Fichier <comment>%s</comment> envoyé',
|
||||
$file
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
$isFirst = true;
|
||||
|
||||
foreach ($remoteFiles as $remoteFile) {
|
||||
$name = $remoteFile['{DAV:}displayname'];
|
||||
|
||||
if (!isset($localFiles[$name]) && !$isFirst) {
|
||||
$this->client->rm($name);
|
||||
$output->writeln(sprintf(
|
||||
'Fichier distant <comment>%s</comment> supprimé',
|
||||
$name
|
||||
));
|
||||
}
|
||||
|
||||
$isFirst = false;
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue