From 92c6b641aadc95565524c0b410c2f37f08ce3125 Mon Sep 17 00:00:00 2001 From: Justin Kromlinger Date: Mon, 4 May 2020 18:00:00 +0200 Subject: [PATCH 1/3] Skip external mounts with disabled previews Signed-off-by: hashworks --- lib/Command/Generate.php | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/Command/Generate.php b/lib/Command/Generate.php index 78ae879..bb54588 100644 --- a/lib/Command/Generate.php +++ b/lib/Command/Generate.php @@ -36,6 +36,7 @@ use OCP\IPreview; use OCP\IUser; use OCP\IUserManager; +use OCA\Files_External\Service\GlobalStoragesService; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -43,6 +44,9 @@ use Symfony\Component\Console\Output\OutputInterface; class Generate extends Command { + + /** @var GlobalStoragesService */ + protected $globalService; /** @var IUserManager */ protected $userManager; @@ -65,13 +69,15 @@ class Generate extends Command { /** @var IManager */ protected $encryptionManager; - public function __construct(IRootFolder $rootFolder, + public function __construct(GlobalStoragesService $globalService, + IRootFolder $rootFolder, IUserManager $userManager, IPreview $previewGenerator, IConfig $config, IManager $encryptionManager) { parent::__construct(); + $this->globalService = $globalService; $this->userManager = $userManager; $this->rootFolder = $rootFolder; $this->previewGenerator = $previewGenerator; @@ -133,6 +139,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } + private function getNoPreviewMountPaths(IUser $user) { + $mountPaths = []; + $userId = $user->getUID(); + $mounts = $this->globalService->getStorageForAllUsers(); + foreach ($mounts as $mount) { + if (in_array($userId, $mount->getApplicableUsers()) && + $mount->getMountOptions()['previews'] === false + ) { + $rootFolder = $this->rootFolder->getUserFolder($userId)->getPath(); + array_push($mountPaths, $rootFolder.$mount->getMountPoint()); + } + } + return $mountPaths; + } + private function generatePathPreviews(IUser $user, string $path) { \OC_Util::tearDownFS(); \OC_Util::setupFS($user->getUID()); @@ -144,7 +165,8 @@ private function generatePathPreviews(IUser $user, string $path) { return; } $pathFolder = $userFolder->get($relativePath); - $this->parseFolder($pathFolder); + $noPreviewMountPaths = $this->getNoPreviewMountPaths($user); + $this->parseFolder($pathFolder, $noPreviewMountPaths); } private function generateUserPreviews(IUser $user) { @@ -152,18 +174,22 @@ private function generateUserPreviews(IUser $user) { \OC_Util::setupFS($user->getUID()); $userFolder = $this->rootFolder->getUserFolder($user->getUID()); - $this->parseFolder($userFolder); + $noPreviewMountPaths = $this->getNoPreviewMountPaths($user); + $this->parseFolder($userFolder, $noPreviewMountPaths); } - private function parseFolder(Folder $folder) { + private function parseFolder(Folder $folder, $noPreviewMountPaths) { try { + $folderPath = $folder->getPath(); + // Respect the '.nomedia' file. If present don't traverse the folder - if ($folder->nodeExists('.nomedia')) { - $this->output->writeln('Skipping folder ' . $folder->getPath()); + // Same for external mounts with previews disabled + if ($folder->nodeExists('.nomedia') || in_array($folderPath, $noPreviewMountPaths)) { + $this->output->writeln('Skipping folder ' . $folderPath); return; } - $this->output->writeln('Scanning folder ' . $folder->getPath()); + $this->output->writeln('Scanning folder ' . $folderPath); $nodes = $folder->getDirectoryListing(); From 3cde4b8a5ad86c9a7cc524e6c4eed22f02bf8e5b Mon Sep 17 00:00:00 2001 From: Justin Kromlinger Date: Mon, 4 May 2020 18:17:11 +0200 Subject: [PATCH 2/3] Make WatcherTest::setUp() compatibly with TestCase Signed-off-by: hashworks --- tests/WatcherTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/WatcherTest.php b/tests/WatcherTest.php index a89bbc0..18c5867 100644 --- a/tests/WatcherTest.php +++ b/tests/WatcherTest.php @@ -45,7 +45,7 @@ class WatcherTest extends TestCase { /** @var Watcher */ private $watcher; - public function setUp() { + public function setUp(): void { parent::setUp(); $this->connection = \OC::$server->getDatabaseConnection(); From 8a357bed1c76bef41057bd5b8e98650184b8cd00 Mon Sep 17 00:00:00 2001 From: hashworks Date: Sun, 5 Dec 2021 17:27:13 +0100 Subject: [PATCH 3/3] Make GlobalStoragesService optional GlobalStoragesService is only available when the "External storage support" app is enabled. Signed-off-by: hashworks --- lib/Command/Generate.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/Command/Generate.php b/lib/Command/Generate.php index bb54588..7e7663c 100644 --- a/lib/Command/Generate.php +++ b/lib/Command/Generate.php @@ -69,20 +69,20 @@ class Generate extends Command { /** @var IManager */ protected $encryptionManager; - public function __construct(GlobalStoragesService $globalService, - IRootFolder $rootFolder, + public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IPreview $previewGenerator, IConfig $config, - IManager $encryptionManager) { + IManager $encryptionManager, + GlobalStoragesService $globalService = null) { parent::__construct(); - $this->globalService = $globalService; $this->userManager = $userManager; $this->rootFolder = $rootFolder; $this->previewGenerator = $previewGenerator; $this->config = $config; $this->encryptionManager = $encryptionManager; + $this->globalService = $globalService; } protected function configure() { @@ -140,6 +140,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int } private function getNoPreviewMountPaths(IUser $user) { + if ($this->globalService === null) { + return []; + } $mountPaths = []; $userId = $user->getUID(); $mounts = $this->globalService->getStorageForAllUsers(); @@ -147,8 +150,8 @@ private function getNoPreviewMountPaths(IUser $user) { if (in_array($userId, $mount->getApplicableUsers()) && $mount->getMountOptions()['previews'] === false ) { - $rootFolder = $this->rootFolder->getUserFolder($userId)->getPath(); - array_push($mountPaths, $rootFolder.$mount->getMountPoint()); + $userFolder = $this->rootFolder->getUserFolder($userId)->getPath(); + array_push($mountPaths, $userFolder.$mount->getMountPoint()); } } return $mountPaths; @@ -195,7 +198,7 @@ private function parseFolder(Folder $folder, $noPreviewMountPaths) { foreach ($nodes as $node) { if ($node instanceof Folder) { - $this->parseFolder($node); + $this->parseFolder($node, $noPreviewMountPaths); } elseif ($node instanceof File) { $this->parseFile($node); }