Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip external mounts with disabled previews #190

Merged
merged 3 commits into from Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 37 additions & 8 deletions lib/Command/Generate.php
Expand Up @@ -36,13 +36,17 @@
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;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Generate extends Command {

/** @var GlobalStoragesService */
protected $globalService;

/** @var IUserManager */
protected $userManager;
Expand All @@ -69,14 +73,16 @@ public function __construct(IRootFolder $rootFolder,
IUserManager $userManager,
IPreview $previewGenerator,
IConfig $config,
IManager $encryptionManager) {
IManager $encryptionManager,
GlobalStoragesService $globalService = null) {
parent::__construct();

$this->userManager = $userManager;
$this->rootFolder = $rootFolder;
$this->previewGenerator = $previewGenerator;
$this->config = $config;
$this->encryptionManager = $encryptionManager;
$this->globalService = $globalService;
}

protected function configure() {
Expand Down Expand Up @@ -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());
Expand All @@ -144,32 +168,37 @@ 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) {
\OC_Util::tearDownFS();
\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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/WatcherTest.php
Expand Up @@ -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();
Expand Down