Skip to content

Commit

Permalink
Merge pull request #190 from hashworks/filterExternalStorage
Browse files Browse the repository at this point in the history
Skip external mounts with disabled previews
  • Loading branch information
st3iny committed Dec 6, 2021
2 parents 0800318 + 8a357be commit 476462d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
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

0 comments on commit 476462d

Please sign in to comment.