diff --git a/src/Api/Webdav/Client.php b/src/Api/Webdav/Client.php index ae3aaaa..66def0d 100644 --- a/src/Api/Webdav/Client.php +++ b/src/Api/Webdav/Client.php @@ -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); diff --git a/src/Command/WebdavBillSyncCommand.php b/src/Command/WebdavBillSyncCommand.php new file mode 100644 index 0000000..58cc9b2 --- /dev/null +++ b/src/Command/WebdavBillSyncCommand.php @@ -0,0 +1,68 @@ +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 %s 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 %s supprimé', + $name + )); + } + + $isFirst = false; + } + + return Command::SUCCESS; + } +}