2016-10-14 09:09:51 -04:00
|
|
|
<?php
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-05-08 08:52:36 -04:00
|
|
|
declare(strict_types=1);
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2016-10-14 09:09:51 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-10-14 09:09:51 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Preview;
|
|
|
|
|
|
|
|
|
|
use OC\SystemConfig;
|
2025-03-09 04:34:11 -04:00
|
|
|
use OCA\Files_Versions\Events\VersionRestoredEvent;
|
|
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2016-10-14 09:09:51 -04:00
|
|
|
use OCP\Files\IRootFolder;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\Files\Node;
|
2025-11-17 09:32:54 -05:00
|
|
|
use OCP\Server;
|
2016-10-14 09:09:51 -04:00
|
|
|
|
|
|
|
|
class WatcherConnector {
|
2025-12-02 05:39:50 -05:00
|
|
|
private ?Watcher $watcher = null;
|
|
|
|
|
|
2025-03-09 04:34:11 -04:00
|
|
|
public function __construct(
|
|
|
|
|
private IRootFolder $root,
|
|
|
|
|
private SystemConfig $config,
|
|
|
|
|
private IEventDispatcher $dispatcher,
|
|
|
|
|
) {
|
2016-10-14 09:09:51 -04:00
|
|
|
}
|
|
|
|
|
|
2018-05-08 08:52:36 -04:00
|
|
|
private function getWatcher(): Watcher {
|
2025-12-02 05:39:50 -05:00
|
|
|
if ($this->watcher !== null) {
|
|
|
|
|
return $this->watcher;
|
|
|
|
|
}
|
|
|
|
|
$this->watcher = Server::get(Watcher::class);
|
|
|
|
|
return $this->watcher;
|
2016-10-14 09:09:51 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-09 04:34:11 -04:00
|
|
|
public function connectWatcher(): void {
|
2016-10-14 09:09:51 -04:00
|
|
|
// Do not connect if we are not setup yet!
|
|
|
|
|
if ($this->config->getValue('instanceid', null) !== null) {
|
2025-11-17 09:32:54 -05:00
|
|
|
$this->root->listen('\OC\Files', 'postWrite', function (Node $node): void {
|
2016-10-14 09:09:51 -04:00
|
|
|
$this->getWatcher()->postWrite($node);
|
|
|
|
|
});
|
2018-05-14 13:58:19 -04:00
|
|
|
|
2025-11-17 09:32:54 -05:00
|
|
|
$this->dispatcher->addListener(VersionRestoredEvent::class, function (VersionRestoredEvent $event): void {
|
2025-03-09 04:34:11 -04:00
|
|
|
$this->getWatcher()->versionRollback(['node' => $event->getVersion()->getSourceFile()]);
|
|
|
|
|
});
|
2016-10-14 09:09:51 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|