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;
|
|
|
|
|
|
2025-09-11 08:52:34 -04:00
|
|
|
use OC\Preview\Db\PreviewMapper;
|
|
|
|
|
use OC\Preview\Storage\StorageFactory;
|
2025-03-09 04:34:11 -04:00
|
|
|
use OCP\Files\FileInfo;
|
2016-10-14 09:09:51 -04:00
|
|
|
use OCP\Files\Folder;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\Files\Node;
|
2025-09-30 09:56:31 -04:00
|
|
|
use OCP\IDBConnection;
|
2016-10-14 09:09:51 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Watcher
|
|
|
|
|
*
|
|
|
|
|
* @package OC\Preview
|
|
|
|
|
*
|
|
|
|
|
* Class that will watch filesystem activity and remove previews as needed.
|
|
|
|
|
*/
|
|
|
|
|
class Watcher {
|
|
|
|
|
/**
|
|
|
|
|
* Watcher constructor.
|
|
|
|
|
*/
|
2025-09-11 08:52:34 -04:00
|
|
|
public function __construct(
|
2025-09-30 09:56:31 -04:00
|
|
|
private readonly StorageFactory $storageFactory,
|
|
|
|
|
private readonly PreviewMapper $previewMapper,
|
|
|
|
|
private readonly IDBConnection $connection,
|
2025-09-11 08:52:34 -04:00
|
|
|
) {
|
2016-10-14 09:09:51 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-11 08:52:34 -04:00
|
|
|
public function postWrite(Node $node): void {
|
2018-05-14 13:58:19 -04:00
|
|
|
$this->deleteNode($node);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-11 08:52:34 -04:00
|
|
|
protected function deleteNode(FileInfo $node): void {
|
2016-10-14 09:09:51 -04:00
|
|
|
// We only handle files
|
|
|
|
|
if ($node instanceof Folder) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 05:34:41 -04:00
|
|
|
$nodeId = $node->getId();
|
|
|
|
|
if (is_null($nodeId)) {
|
2025-09-11 08:52:34 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 05:34:41 -04:00
|
|
|
[$node->getId() => $previews] = $this->previewMapper->getAvailablePreviews([$nodeId]);
|
2025-09-30 09:56:31 -04:00
|
|
|
$this->connection->beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
foreach ($previews as $preview) {
|
|
|
|
|
$this->storageFactory->deletePreview($preview);
|
|
|
|
|
$this->previewMapper->delete($preview);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
$this->connection->commit();
|
2016-10-14 09:09:51 -04:00
|
|
|
}
|
|
|
|
|
}
|
2018-05-14 13:58:19 -04:00
|
|
|
|
2025-09-11 08:52:34 -04:00
|
|
|
public function versionRollback(array $data): void {
|
2018-05-14 13:58:19 -04:00
|
|
|
if (isset($data['node'])) {
|
|
|
|
|
$this->deleteNode($data['node']);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-14 09:09:51 -04:00
|
|
|
}
|