2022-08-13 01:58:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2022, Varun Patil <radialapps@gmail.com>
|
|
|
|
* @author Varun Patil <radialapps@gmail.com>
|
|
|
|
* @license AGPL-3.0-or-later
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-08-18 18:27:25 +00:00
|
|
|
namespace OCA\Memories\Command;
|
2022-08-13 01:58:37 +00:00
|
|
|
|
2022-10-19 17:10:36 +00:00
|
|
|
use OCA\Memories\Db\TimelineWrite;
|
2023-04-14 02:43:13 +00:00
|
|
|
use OCA\Memories\Service;
|
2022-08-13 01:58:37 +00:00
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
use OCP\IConfig;
|
2023-09-01 11:33:50 +00:00
|
|
|
use OCP\IGroupManager;
|
2022-08-13 01:58:37 +00:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2022-09-09 15:07:05 +00:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2022-11-21 10:18:06 +00:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2022-08-13 01:58:37 +00:00
|
|
|
|
2023-01-18 04:52:26 +00:00
|
|
|
class IndexOpts
|
|
|
|
{
|
2023-04-14 02:43:13 +00:00
|
|
|
public bool $force = false;
|
2023-01-18 04:52:26 +00:00
|
|
|
public bool $clear = false;
|
2023-03-29 23:38:17 +00:00
|
|
|
public ?string $user = null;
|
|
|
|
public ?string $folder = null;
|
2023-09-01 11:33:50 +00:00
|
|
|
public ?string $group = null;
|
|
|
|
public bool $skipCleanup = false;
|
2023-01-18 04:52:26 +00:00
|
|
|
|
|
|
|
public function __construct(InputInterface $input)
|
|
|
|
{
|
2023-04-14 02:43:13 +00:00
|
|
|
$this->force = (bool) $input->getOption('force');
|
2023-01-18 04:52:26 +00:00
|
|
|
$this->clear = (bool) $input->getOption('clear');
|
2023-03-29 23:38:17 +00:00
|
|
|
$this->user = $input->getOption('user');
|
|
|
|
$this->folder = $input->getOption('folder');
|
2023-09-01 11:33:50 +00:00
|
|
|
$this->skipCleanup = $input->getOption('skip-cleanup');
|
|
|
|
$this->group = $input->getOption('group');
|
2023-01-18 04:52:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 17:10:36 +00:00
|
|
|
class Index extends Command
|
|
|
|
{
|
2022-09-09 07:31:42 +00:00
|
|
|
protected IUserManager $userManager;
|
2023-09-01 11:33:50 +00:00
|
|
|
protected IGroupManager $groupManager;
|
2022-09-09 07:31:42 +00:00
|
|
|
protected IRootFolder $rootFolder;
|
|
|
|
protected IConfig $config;
|
2023-04-14 02:43:13 +00:00
|
|
|
protected Service\Index $indexer;
|
2022-09-09 07:31:42 +00:00
|
|
|
protected TimelineWrite $timelineWrite;
|
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
// IO
|
|
|
|
private InputInterface $input;
|
|
|
|
private OutputInterface $output;
|
2022-10-25 17:49:42 +00:00
|
|
|
|
2023-03-29 23:38:17 +00:00
|
|
|
// Command options
|
|
|
|
private IndexOpts $opts;
|
|
|
|
|
2022-10-19 17:10:36 +00:00
|
|
|
public function __construct(
|
|
|
|
IRootFolder $rootFolder,
|
|
|
|
IUserManager $userManager,
|
2023-09-01 11:33:50 +00:00
|
|
|
IGroupManager $groupManager,
|
2022-10-19 17:10:36 +00:00
|
|
|
IConfig $config,
|
2023-04-14 02:43:13 +00:00
|
|
|
Service\Index $indexer,
|
|
|
|
TimelineWrite $timelineWrite
|
2022-10-19 17:15:14 +00:00
|
|
|
) {
|
2022-09-09 07:31:42 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->userManager = $userManager;
|
2023-09-01 11:33:50 +00:00
|
|
|
$this->groupManager = $groupManager;
|
2022-09-09 07:31:42 +00:00
|
|
|
$this->rootFolder = $rootFolder;
|
|
|
|
$this->config = $config;
|
2023-04-14 02:43:13 +00:00
|
|
|
$this->indexer = $indexer;
|
|
|
|
$this->timelineWrite = $timelineWrite;
|
2022-09-09 07:31:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 17:10:36 +00:00
|
|
|
protected function configure(): void
|
|
|
|
{
|
2022-09-09 07:31:42 +00:00
|
|
|
$this
|
|
|
|
->setName('memories:index')
|
2023-09-29 16:50:49 +00:00
|
|
|
->setDescription('Index the metadata in files')
|
2023-03-29 23:38:17 +00:00
|
|
|
->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'Index only the specified user')
|
2023-09-29 16:50:49 +00:00
|
|
|
->addOption('group', 'g', InputOption::VALUE_REQUIRED, 'Index only specified group')
|
2023-04-14 02:43:13 +00:00
|
|
|
->addOption('folder', null, InputOption::VALUE_REQUIRED, 'Index only the specified folder (relative to the user\'s root)')
|
|
|
|
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force refresh of existing index entries')
|
|
|
|
->addOption('clear', null, InputOption::VALUE_NONE, 'Clear all existing index entries')
|
2023-09-29 16:50:49 +00:00
|
|
|
->addOption('skip-cleanup', null, InputOption::VALUE_NONE, 'Skip cleanup step (removing index entries with missing files)')
|
2022-10-19 17:10:36 +00:00
|
|
|
;
|
2022-09-09 07:31:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 17:10:36 +00:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
|
{
|
2023-04-20 21:25:43 +00:00
|
|
|
/** @var \Symfony\Component\Console\Output\ConsoleOutputInterface $output */
|
|
|
|
$output = $output;
|
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
// Store input/output/opts for later use
|
|
|
|
$this->input = $input;
|
|
|
|
$this->output = $output;
|
2023-03-29 23:38:17 +00:00
|
|
|
$this->opts = new IndexOpts($input);
|
2023-04-14 05:24:01 +00:00
|
|
|
|
|
|
|
// Assign to indexer
|
2023-04-14 04:59:01 +00:00
|
|
|
$this->indexer->output = $output;
|
2023-04-14 05:24:01 +00:00
|
|
|
$this->indexer->section = $output->section();
|
2022-09-11 00:15:40 +00:00
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
try {
|
|
|
|
// Use static exiftool process
|
|
|
|
\OCA\Memories\Exif::ensureStaticExiftoolProc();
|
2023-09-29 18:25:49 +00:00
|
|
|
Service\BinExt::testExiftool(); // throws
|
2023-03-29 23:38:17 +00:00
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
// Perform steps based on opts
|
|
|
|
$this->checkClear();
|
|
|
|
$this->checkForce();
|
2023-01-18 04:52:26 +00:00
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
// Run the indexer
|
|
|
|
$this->runIndex();
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2023-04-25 00:59:26 +00:00
|
|
|
// Clean up the index
|
2023-09-01 11:33:50 +00:00
|
|
|
if (!$this->opts->skipCleanup) {
|
|
|
|
$this->indexer->cleanupStale();
|
|
|
|
}
|
2023-04-25 00:59:26 +00:00
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
return 0;
|
2022-09-09 15:18:55 +00:00
|
|
|
} catch (\Exception $e) {
|
2023-09-30 01:48:52 +00:00
|
|
|
$this->output->writeln("<error>{$e->getMessage()}</error>\n");
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2022-09-09 15:18:55 +00:00
|
|
|
return 1;
|
|
|
|
} finally {
|
|
|
|
\OCA\Memories\Exif::closeStaticExiftoolProc();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
/**
|
|
|
|
* Check and act on the clear option if set.
|
|
|
|
*/
|
|
|
|
protected function checkClear(): void
|
2022-10-19 17:10:36 +00:00
|
|
|
{
|
2023-04-14 04:59:01 +00:00
|
|
|
if (!$this->opts->clear) {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-09 07:31:42 +00:00
|
|
|
|
2023-04-14 04:59:01 +00:00
|
|
|
if ($this->input->isInteractive()) {
|
2023-10-07 15:56:50 +00:00
|
|
|
$this->output->writeln('<error>Clearing the index can have unintended consequences like');
|
|
|
|
$this->output->writeln('duplicates for some files appearing on the mobile app.');
|
|
|
|
$this->output->writeln('Using --force instead of --clear is recommended in most cases.</error>');
|
2023-04-14 04:59:01 +00:00
|
|
|
$this->output->write('Are you sure you want to clear the existing index? (y/N): ');
|
|
|
|
if ('y' !== trim(fgets(STDIN))) {
|
2023-04-20 21:11:39 +00:00
|
|
|
throw new \Exception('Aborting');
|
2023-04-14 04:59:01 +00:00
|
|
|
}
|
2023-04-14 02:43:13 +00:00
|
|
|
}
|
2023-04-14 04:59:01 +00:00
|
|
|
|
|
|
|
$this->timelineWrite->clear();
|
2023-10-14 02:03:13 +00:00
|
|
|
$this->output->writeln('<info>Cleared existing index</info>');
|
2023-04-14 02:43:13 +00:00
|
|
|
}
|
2022-09-09 07:31:42 +00:00
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
/**
|
|
|
|
* Check and act on the force option if set.
|
|
|
|
*/
|
|
|
|
protected function checkForce(): void
|
|
|
|
{
|
2023-04-14 04:59:01 +00:00
|
|
|
if (!$this->opts->force) {
|
|
|
|
return;
|
2022-09-09 07:31:42 +00:00
|
|
|
}
|
2023-04-14 04:59:01 +00:00
|
|
|
|
|
|
|
$this->output->writeln('Forcing refresh of existing index entries');
|
|
|
|
|
|
|
|
$this->timelineWrite->orphanAll();
|
2023-04-14 02:43:13 +00:00
|
|
|
}
|
2022-08-13 01:58:37 +00:00
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
/**
|
|
|
|
* Run the indexer.
|
|
|
|
*/
|
|
|
|
protected function runIndex(): void
|
|
|
|
{
|
2023-04-14 04:59:01 +00:00
|
|
|
$this->runForUsers(function (IUser $user) {
|
|
|
|
try {
|
2023-10-14 02:03:13 +00:00
|
|
|
$this->indexer->indexUser($user->getUID(), $this->opts->folder);
|
2023-04-14 04:59:01 +00:00
|
|
|
} catch (\Exception $e) {
|
2023-09-30 01:48:52 +00:00
|
|
|
$this->output->writeln("<error>{$e->getMessage()}</error>\n");
|
2023-04-14 04:59:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run function for all users (or selected user if set).
|
|
|
|
*
|
|
|
|
* @param mixed $closure
|
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
private function runForUsers($closure): void
|
2023-04-14 04:59:01 +00:00
|
|
|
{
|
2023-03-29 23:38:17 +00:00
|
|
|
if ($uid = $this->opts->user) {
|
|
|
|
if ($user = $this->userManager->get($uid)) {
|
2023-04-14 04:59:01 +00:00
|
|
|
$closure($user);
|
2023-03-29 23:38:17 +00:00
|
|
|
} else {
|
2023-09-30 01:48:52 +00:00
|
|
|
$this->output->writeln("<error>User {$uid} not found</error>\n");
|
2023-03-29 23:38:17 +00:00
|
|
|
}
|
2023-09-01 11:33:50 +00:00
|
|
|
} elseif ($gid = $this->opts->group) {
|
|
|
|
if ($group = $this->groupManager->get($gid)) {
|
|
|
|
foreach ($group->getUsers() as $user) {
|
|
|
|
$closure($user);
|
2023-09-08 23:10:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-09-30 01:48:52 +00:00
|
|
|
$this->output->writeln("<error>Group {$gid} not found</error>\n");
|
2023-09-08 23:10:29 +00:00
|
|
|
}
|
2023-03-29 23:38:17 +00:00
|
|
|
} else {
|
2023-08-30 18:10:08 +00:00
|
|
|
$this->userManager->callForSeenUsers(static fn (IUser $user) => $closure($user));
|
2022-09-09 07:31:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-19 17:10:36 +00:00
|
|
|
}
|