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
|
|
|
|
|
|
|
use OCP\Encryption\IManager;
|
|
|
|
use OCP\Files\File;
|
|
|
|
use OCP\Files\Folder;
|
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
use OCP\Files\StorageNotAvailableException;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\IPreview;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCA\Files_External\Service\GlobalStoragesService;
|
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
class Index extends Command {
|
|
|
|
|
|
|
|
/** @var ?GlobalStoragesService */
|
|
|
|
protected $globalService;
|
|
|
|
|
|
|
|
/** @var int[][] */
|
|
|
|
protected array $sizes;
|
|
|
|
|
|
|
|
protected IUserManager $userManager;
|
|
|
|
protected IRootFolder $rootFolder;
|
|
|
|
protected IPreview $previewGenerator;
|
|
|
|
protected IConfig $config;
|
|
|
|
protected OutputInterface $output;
|
|
|
|
protected IManager $encryptionManager;
|
|
|
|
protected IDBConnection $connection;
|
2022-08-18 18:27:25 +00:00
|
|
|
protected \OCA\Memories\Db\Util $util;
|
2022-08-13 01:58:37 +00:00
|
|
|
|
|
|
|
public function __construct(IRootFolder $rootFolder,
|
|
|
|
IUserManager $userManager,
|
|
|
|
IPreview $previewGenerator,
|
|
|
|
IConfig $config,
|
|
|
|
IManager $encryptionManager,
|
|
|
|
IDBConnection $connection,
|
|
|
|
ContainerInterface $container) {
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->rootFolder = $rootFolder;
|
|
|
|
$this->previewGenerator = $previewGenerator;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->encryptionManager = $encryptionManager;
|
|
|
|
$this->connection = $connection;
|
2022-08-18 18:27:25 +00:00
|
|
|
$this->util = new \OCA\Memories\Db\Util($connection);
|
2022-08-13 01:58:37 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->globalService = $container->get(GlobalStoragesService::class);
|
|
|
|
} catch (ContainerExceptionInterface $e) {
|
|
|
|
$this->globalService = null;
|
|
|
|
}
|
2022-08-20 02:25:39 +00:00
|
|
|
|
|
|
|
// Refuse to run without exiftool
|
|
|
|
if (!$this->testExif()) {
|
|
|
|
error_log('FATAL: exiftool could not be found or test failed');
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Make sure exiftool is available */
|
|
|
|
private function testExif() {
|
|
|
|
$testfile = dirname(__FILE__). '/../../exiftest.jpg';
|
|
|
|
$stream = fopen($testfile, 'rb');
|
|
|
|
if (!$stream) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$exif = \OCA\Memories\Db\Util::getExifFromStream($stream);
|
|
|
|
fclose($stream);
|
|
|
|
|
|
|
|
if (!$exif || $exif["DateTimeOriginal"] !== "2004:08:31 19:52:58") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2022-08-13 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function configure(): void {
|
|
|
|
$this
|
2022-08-18 18:27:25 +00:00
|
|
|
->setName('memories:index')
|
2022-08-13 01:58:37 +00:00
|
|
|
->setDescription('Generate entries');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int {
|
|
|
|
if ($this->encryptionManager->isEnabled()) {
|
|
|
|
$output->writeln('Encryption is enabled. Aborted.');
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
$this->output = $output;
|
|
|
|
|
|
|
|
$this->userManager->callForSeenUsers(function (IUser $user) {
|
|
|
|
$this->generateUserEntries($user);
|
|
|
|
});
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-20 02:14:03 +00:00
|
|
|
private function generateUserEntries(IUser &$user): void {
|
2022-08-13 01:58:37 +00:00
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_Util::setupFS($user->getUID());
|
|
|
|
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
|
2022-08-16 02:33:15 +00:00
|
|
|
$this->parseFolder($userFolder);
|
2022-08-13 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 02:14:03 +00:00
|
|
|
private function parseFolder(Folder &$folder): void {
|
2022-08-13 01:58:37 +00:00
|
|
|
try {
|
|
|
|
$folderPath = $folder->getPath();
|
|
|
|
$this->output->writeln('Scanning folder ' . $folderPath);
|
|
|
|
|
|
|
|
$nodes = $folder->getDirectoryListing();
|
|
|
|
|
2022-08-20 02:14:03 +00:00
|
|
|
foreach ($nodes as &$node) {
|
2022-08-13 01:58:37 +00:00
|
|
|
if ($node instanceof Folder) {
|
2022-08-16 02:33:15 +00:00
|
|
|
$this->parseFolder($node);
|
2022-08-13 01:58:37 +00:00
|
|
|
} elseif ($node instanceof File) {
|
2022-08-16 02:33:15 +00:00
|
|
|
$this->parseFile($node);
|
2022-08-13 01:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->output->writeln(sprintf('<error>Storage for folder folder %s is not available: %s</error>',
|
|
|
|
$folder->getPath(),
|
|
|
|
$e->getHint()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-20 02:14:03 +00:00
|
|
|
private function parseFile(File &$file): void {
|
2022-08-16 00:20:32 +00:00
|
|
|
// $this->output->writeln('Generating entry for ' . $file->getPath() . ' ' . $file->getId());
|
2022-08-16 02:33:15 +00:00
|
|
|
$this->util->processFile($file);
|
2022-08-13 01:58:37 +00:00
|
|
|
}
|
|
|
|
}
|