From 3e07c4f73a80bfa7e5de5a9e0074d2c7d35f18cc Mon Sep 17 00:00:00 2001
From: Roeland Jago Douma <roeland@famdouma.nl>
Date: Tue, 8 May 2018 14:52:36 +0200
Subject: [PATCH] Kill the dete preview watcher

This code had the potential to time out. If a huge folder with pictures
for example was deleted then this could easily grow the number of files
to clean with a factor 5 (or more).

Now the previews just get cleaned up in the background. Which is good
enough for the 99% case

As a bonus this now also keeps the previews when in the trashbin so you
don't have a spiking server load when a user opens the trashbin view.

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
---
 lib/private/Preview/Watcher.php          | 44 ++----------------------
 lib/private/Preview/WatcherConnector.php | 11 ++----
 2 files changed, 4 insertions(+), 51 deletions(-)

diff --git a/lib/private/Preview/Watcher.php b/lib/private/Preview/Watcher.php
index 8d091b84b0e..be462d9c935 100644
--- a/lib/private/Preview/Watcher.php
+++ b/lib/private/Preview/Watcher.php
@@ -1,4 +1,5 @@
 <?php
+declare(strict_types=1);
 /**
  * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  *
@@ -22,7 +23,6 @@
  */
 namespace OC\Preview;
 
-use OCP\Files\File;
 use OCP\Files\Node;
 use OCP\Files\Folder;
 use OCP\Files\IAppData;
@@ -39,9 +39,6 @@ class Watcher {
 	/** @var IAppData */
 	private $appData;
 
-	/** @var int[] */
-	private $toDelete = [];
-
 	/**
 	 * Watcher constructor.
 	 *
@@ -58,47 +55,10 @@ class Watcher {
 		}
 
 		try {
-			$folder = $this->appData->getFolder($node->getId());
+			$folder = $this->appData->getFolder((string)$node->getId());
 			$folder->delete();
 		} catch (NotFoundException $e) {
 			//Nothing to do
 		}
 	}
-
-	public function preDelete(Node $node) {
-		// To avoid cycles
-		if ($this->toDelete !== []) {
-			return;
-		}
-
-		if ($node instanceof File) {
-			$this->toDelete[] = $node->getId();
-			return;
-		}
-
-		/** @var Folder $node */
-		$this->deleteFolder($node);
-	}
-
-	private function deleteFolder(Folder $folder) {
-		$nodes = $folder->getDirectoryListing();
-		foreach ($nodes as $node) {
-			if ($node instanceof File) {
-				$this->toDelete[] = $node->getId();
-			} else if ($node instanceof Folder) {
-				$this->deleteFolder($node);
-			}
-		}
-	}
-
-	public function postDelete(Node $node) {
-		foreach ($this->toDelete as $fid) {
-			try {
-				$folder = $this->appData->getFolder($fid);
-				$folder->delete();
-			} catch (NotFoundException $e) {
-				// continue
-			}
-		}
-	}
 }
diff --git a/lib/private/Preview/WatcherConnector.php b/lib/private/Preview/WatcherConnector.php
index 4e6e786cec7..bf9e6c29e4f 100644
--- a/lib/private/Preview/WatcherConnector.php
+++ b/lib/private/Preview/WatcherConnector.php
@@ -1,4 +1,5 @@
 <?php
+declare(strict_types=1);
 /**
  * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  *
@@ -49,7 +50,7 @@ class WatcherConnector {
 	/**
 	 * @return Watcher
 	 */
-	private function getWatcher() {
+	private function getWatcher(): Watcher {
 		return \OC::$server->query(Watcher::class);
 	}
 
@@ -59,14 +60,6 @@ class WatcherConnector {
 			$this->root->listen('\OC\Files', 'postWrite', function (Node $node) {
 				$this->getWatcher()->postWrite($node);
 			});
-
-			$this->root->listen('\OC\Files', 'preDelete', function (Node $node) {
-				$this->getWatcher()->preDelete($node);
-			});
-
-			$this->root->listen('\OC\Files', 'postDelete', function (Node $node) {
-				$this->getWatcher()->postDelete($node);
-			});
 		}
 	}
 }
-- 
GitLab