diff --git a/lib/Command/Generate.php b/lib/Command/Generate.php index 78ae879..7e7663c 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; @@ -69,7 +73,8 @@ public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IPreview $previewGenerator, IConfig $config, - IManager $encryptionManager) { + IManager $encryptionManager, + GlobalStoragesService $globalService = null) { parent::__construct(); $this->userManager = $userManager; @@ -77,6 +82,7 @@ public function __construct(IRootFolder $rootFolder, $this->previewGenerator = $previewGenerator; $this->config = $config; $this->encryptionManager = $encryptionManager; + $this->globalService = $globalService; } protected function configure() { @@ -133,6 +139,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } + private function getNoPreviewMountPaths(IUser $user) { + if ($this->globalService === null) { + return []; + } + $mountPaths = []; + $userId = $user->getUID(); + $mounts = $this->globalService->getStorageForAllUsers(); + foreach ($mounts as $mount) { + if (in_array($userId, $mount->getApplicableUsers()) && + $mount->getMountOptions()['previews'] === false + ) { + $userFolder = $this->rootFolder->getUserFolder($userId)->getPath(); + array_push($mountPaths, $userFolder.$mount->getMountPoint()); + } + } + return $mountPaths; + } + private function generatePathPreviews(IUser $user, string $path) { \OC_Util::tearDownFS(); \OC_Util::setupFS($user->getUID()); @@ -144,7 +168,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,24 +177,28 @@ 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(); foreach ($nodes as $node) { if ($node instanceof Folder) { - $this->parseFolder($node); + $this->parseFolder($node, $noPreviewMountPaths); } elseif ($node instanceof File) { $this->parseFile($node); } 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();