aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/lib/Command/Object
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files/lib/Command/Object')
-rw-r--r--apps/files/lib/Command/Object/Delete.php60
-rw-r--r--apps/files/lib/Command/Object/Get.php63
-rw-r--r--apps/files/lib/Command/Object/Info.php80
-rw-r--r--apps/files/lib/Command/Object/ListObject.php50
-rw-r--r--apps/files/lib/Command/Object/Multi/Rename.php108
-rw-r--r--apps/files/lib/Command/Object/Multi/Users.php98
-rw-r--r--apps/files/lib/Command/Object/ObjectUtil.php115
-rw-r--r--apps/files/lib/Command/Object/Orphans.php79
-rw-r--r--apps/files/lib/Command/Object/Put.php68
9 files changed, 721 insertions, 0 deletions
diff --git a/apps/files/lib/Command/Object/Delete.php b/apps/files/lib/Command/Object/Delete.php
new file mode 100644
index 00000000000..07613ecc616
--- /dev/null
+++ b/apps/files/lib/Command/Object/Delete.php
@@ -0,0 +1,60 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\Files\Command\Object;
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Helper\QuestionHelper;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Question\ConfirmationQuestion;
+
+class Delete extends Command {
+ public function __construct(
+ private ObjectUtil $objectUtils,
+ ) {
+ parent::__construct();
+ }
+
+ protected function configure(): void {
+ $this
+ ->setName('files:object:delete')
+ ->setDescription('Delete an object from the object store')
+ ->addArgument('object', InputArgument::REQUIRED, 'Object to delete')
+ ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket to delete the object from, only required in cases where it can't be determined from the config");
+ }
+
+ public function execute(InputInterface $input, OutputInterface $output): int {
+ $object = $input->getArgument('object');
+ $objectStore = $this->objectUtils->getObjectStore($input->getOption('bucket'), $output);
+ if (!$objectStore) {
+ return -1;
+ }
+
+ if ($fileId = $this->objectUtils->objectExistsInDb($object)) {
+ $output->writeln("<error>Warning, object $object belongs to an existing file, deleting the object will lead to unexpected behavior if not replaced</error>");
+ $output->writeln(" Note: use <info>occ files:delete $fileId</info> to delete the file cleanly or <info>occ info:file $fileId</info> for more information about the file");
+ $output->writeln('');
+ }
+
+ if (!$objectStore->objectExists($object)) {
+ $output->writeln("<error>Object $object does not exist</error>");
+ return -1;
+ }
+
+ /** @var QuestionHelper $helper */
+ $helper = $this->getHelper('question');
+ $question = new ConfirmationQuestion("Delete $object? [y/N] ", false);
+ if ($helper->ask($input, $output, $question)) {
+ $objectStore->deleteObject($object);
+ }
+ return self::SUCCESS;
+ }
+}
diff --git a/apps/files/lib/Command/Object/Get.php b/apps/files/lib/Command/Object/Get.php
new file mode 100644
index 00000000000..c32de020c5a
--- /dev/null
+++ b/apps/files/lib/Command/Object/Get.php
@@ -0,0 +1,63 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\Files\Command\Object;
+
+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 Get extends Command {
+ public function __construct(
+ private ObjectUtil $objectUtils,
+ ) {
+ parent::__construct();
+ }
+
+ protected function configure(): void {
+ $this
+ ->setName('files:object:get')
+ ->setDescription('Get the contents of an object')
+ ->addArgument('object', InputArgument::REQUIRED, 'Object to get')
+ ->addArgument('output', InputArgument::REQUIRED, 'Target local file to output to, use - for STDOUT')
+ ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket to get the object from, only required in cases where it can't be determined from the config");
+ }
+
+ public function execute(InputInterface $input, OutputInterface $output): int {
+ $object = $input->getArgument('object');
+ $outputName = $input->getArgument('output');
+ $objectStore = $this->objectUtils->getObjectStore($input->getOption('bucket'), $output);
+ if (!$objectStore) {
+ return self::FAILURE;
+ }
+
+ if (!$objectStore->objectExists($object)) {
+ $output->writeln("<error>Object $object does not exist</error>");
+ return self::FAILURE;
+ }
+
+ try {
+ $source = $objectStore->readObject($object);
+ } catch (\Exception $e) {
+ $msg = $e->getMessage();
+ $output->writeln("<error>Failed to read $object from object store: $msg</error>");
+ return self::FAILURE;
+ }
+ $target = $outputName === '-' ? STDOUT : fopen($outputName, 'w');
+ if (!$target) {
+ $output->writeln("<error>Failed to open $outputName for writing</error>");
+ return self::FAILURE;
+ }
+
+ stream_copy_to_stream($source, $target);
+ return self::SUCCESS;
+ }
+
+}
diff --git a/apps/files/lib/Command/Object/Info.php b/apps/files/lib/Command/Object/Info.php
new file mode 100644
index 00000000000..6748de37cfe
--- /dev/null
+++ b/apps/files/lib/Command/Object/Info.php
@@ -0,0 +1,80 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\Files\Command\Object;
+
+use OC\Core\Command\Base;
+use OCP\Files\IMimeTypeDetector;
+use OCP\Files\ObjectStore\IObjectStoreMetaData;
+use OCP\Util;
+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 Info extends Base {
+ public function __construct(
+ private ObjectUtil $objectUtils,
+ private IMimeTypeDetector $mimeTypeDetector,
+ ) {
+ parent::__construct();
+ }
+
+ protected function configure(): void {
+ parent::configure();
+ $this
+ ->setName('files:object:info')
+ ->setDescription('Get the metadata of an object')
+ ->addArgument('object', InputArgument::REQUIRED, 'Object to get')
+ ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket to get the object from, only required in cases where it can't be determined from the config");
+ }
+
+ public function execute(InputInterface $input, OutputInterface $output): int {
+ $object = $input->getArgument('object');
+ $objectStore = $this->objectUtils->getObjectStore($input->getOption('bucket'), $output);
+ if (!$objectStore) {
+ return self::FAILURE;
+ }
+
+ if (!$objectStore instanceof IObjectStoreMetaData) {
+ $output->writeln('<error>Configured object store does currently not support retrieve metadata</error>');
+ return self::FAILURE;
+ }
+
+ if (!$objectStore->objectExists($object)) {
+ $output->writeln("<error>Object $object does not exist</error>");
+ return self::FAILURE;
+ }
+
+ try {
+ $meta = $objectStore->getObjectMetaData($object);
+ } catch (\Exception $e) {
+ $msg = $e->getMessage();
+ $output->writeln("<error>Failed to read $object from object store: $msg</error>");
+ return self::FAILURE;
+ }
+
+ if ($input->getOption('output') === 'plain' && isset($meta['size'])) {
+ $meta['size'] = Util::humanFileSize($meta['size']);
+ }
+ if (isset($meta['mtime'])) {
+ $meta['mtime'] = $meta['mtime']->format(\DateTimeImmutable::ATOM);
+ }
+ if (!isset($meta['mimetype'])) {
+ $handle = $objectStore->readObject($object);
+ $head = fread($handle, 8192);
+ fclose($handle);
+ $meta['mimetype'] = $this->mimeTypeDetector->detectString($head);
+ }
+
+ $this->writeArrayInOutputFormat($input, $output, $meta);
+
+ return self::SUCCESS;
+ }
+
+}
diff --git a/apps/files/lib/Command/Object/ListObject.php b/apps/files/lib/Command/Object/ListObject.php
new file mode 100644
index 00000000000..5d30232e09f
--- /dev/null
+++ b/apps/files/lib/Command/Object/ListObject.php
@@ -0,0 +1,50 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\Files\Command\Object;
+
+use OC\Core\Command\Base;
+use OCP\Files\ObjectStore\IObjectStoreMetaData;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class ListObject extends Base {
+ private const CHUNK_SIZE = 100;
+
+ public function __construct(
+ private readonly ObjectUtil $objectUtils,
+ ) {
+ parent::__construct();
+ }
+
+ protected function configure(): void {
+ parent::configure();
+ $this
+ ->setName('files:object:list')
+ ->setDescription('List all objects in the object store')
+ ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket to list the objects from, only required in cases where it can't be determined from the config");
+ }
+
+ public function execute(InputInterface $input, OutputInterface $output): int {
+ $objectStore = $this->objectUtils->getObjectStore($input->getOption('bucket'), $output);
+ if (!$objectStore) {
+ return self::FAILURE;
+ }
+
+ if (!$objectStore instanceof IObjectStoreMetaData) {
+ $output->writeln('<error>Configured object store does currently not support listing objects</error>');
+ return self::FAILURE;
+ }
+ $objects = $objectStore->listObjects();
+ $objects = $this->objectUtils->formatObjects($objects, $input->getOption('output') === self::OUTPUT_FORMAT_PLAIN);
+ $this->writeStreamingTableInOutputFormat($input, $output, $objects, self::CHUNK_SIZE);
+
+ return self::SUCCESS;
+ }
+}
diff --git a/apps/files/lib/Command/Object/Multi/Rename.php b/apps/files/lib/Command/Object/Multi/Rename.php
new file mode 100644
index 00000000000..562c68eb07f
--- /dev/null
+++ b/apps/files/lib/Command/Object/Multi/Rename.php
@@ -0,0 +1,108 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2025 Robin Appelman <robin@icewind.nl>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\Files\Command\Object\Multi;
+
+use OC\Core\Command\Base;
+use OC\Files\ObjectStore\PrimaryObjectStoreConfig;
+use OCP\IConfig;
+use OCP\IDBConnection;
+use Symfony\Component\Console\Helper\QuestionHelper;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Question\ConfirmationQuestion;
+
+class Rename extends Base {
+ public function __construct(
+ private readonly IDBConnection $connection,
+ private readonly PrimaryObjectStoreConfig $objectStoreConfig,
+ private readonly IConfig $config,
+ ) {
+ parent::__construct();
+ }
+
+ protected function configure(): void {
+ parent::configure();
+ $this
+ ->setName('files:object:multi:rename-config')
+ ->setDescription('Rename an object store configuration and move all users over to the new configuration,')
+ ->addArgument('source', InputArgument::REQUIRED, 'Object store configuration to rename')
+ ->addArgument('target', InputArgument::REQUIRED, 'New name for the object store configuration');
+ }
+
+ public function execute(InputInterface $input, OutputInterface $output): int {
+ $source = $input->getArgument('source');
+ $target = $input->getArgument('target');
+
+ $configs = $this->objectStoreConfig->getObjectStoreConfigs();
+ if (!isset($configs[$source])) {
+ $output->writeln('<error>Unknown object store configuration: ' . $source . '</error>');
+ return 1;
+ }
+
+ if ($source === 'root') {
+ $output->writeln('<error>Renaming the root configuration is not supported.</error>');
+ return 1;
+ }
+
+ if ($source === 'default') {
+ $output->writeln('<error>Renaming the default configuration is not supported.</error>');
+ return 1;
+ }
+
+ if (!isset($configs[$target])) {
+ $output->writeln('<comment>Target object store configuration ' . $target . ' doesn\'t exist yet.</comment>');
+ $output->writeln('The target configuration can be created automatically.');
+ $output->writeln('However, as this depends on modifying the config.php, this only works as long as the instance runs on a single node or all nodes in a clustered setup have a shared config file (such as from a shared network mount).');
+ $output->writeln('If the different nodes have a separate copy of the config.php file, the automatic object store configuration creation will lead to the configuration going out of sync.');
+ $output->writeln('If these requirements are not met, you can manually create the target object store configuration in each node\'s configuration before running the command.');
+ $output->writeln('');
+ $output->writeln('<error>Failure to check these requirements will lead to data loss for users.</error>');
+
+ /** @var QuestionHelper $helper */
+ $helper = $this->getHelper('question');
+ $question = new ConfirmationQuestion('Automatically create target object store configuration? [y/N] ', false);
+ if ($helper->ask($input, $output, $question)) {
+ $configs[$target] = $configs[$source];
+
+ // update all aliases
+ foreach ($configs as &$config) {
+ if ($config === $source) {
+ $config = $target;
+ }
+ }
+ $this->config->setSystemValue('objectstore', $configs);
+ } else {
+ return 0;
+ }
+ } elseif (($configs[$source] !== $configs[$target]) || $configs[$source] !== $target) {
+ $output->writeln('<error>Source and target configuration differ.</error>');
+ $output->writeln('');
+ $output->writeln('To ensure proper migration of users, the source and target configuration must be the same to ensure that the objects for the moved users exist on the target configuration.');
+ $output->writeln('The usual migration process consists of creating a clone of the old configuration, moving the users from the old configuration to the new one, and then adjust the old configuration that is longer used.');
+ return 1;
+ }
+
+ $query = $this->connection->getQueryBuilder();
+ $query->update('preferences')
+ ->set('configvalue', $query->createNamedParameter($target))
+ ->where($query->expr()->eq('appid', $query->createNamedParameter('homeobjectstore')))
+ ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('objectstore')))
+ ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter($source)));
+ $count = $query->executeStatement();
+
+ if ($count > 0) {
+ $output->writeln('Moved <info>' . $count . '</info> users');
+ } else {
+ $output->writeln('No users moved');
+ }
+
+ return 0;
+ }
+}
diff --git a/apps/files/lib/Command/Object/Multi/Users.php b/apps/files/lib/Command/Object/Multi/Users.php
new file mode 100644
index 00000000000..e8f7d012641
--- /dev/null
+++ b/apps/files/lib/Command/Object/Multi/Users.php
@@ -0,0 +1,98 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2025 Robin Appelman <robin@icewind.nl>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\Files\Command\Object\Multi;
+
+use OC\Core\Command\Base;
+use OC\Files\ObjectStore\PrimaryObjectStoreConfig;
+use OCP\IConfig;
+use OCP\IUser;
+use OCP\IUserManager;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class Users extends Base {
+ public function __construct(
+ private readonly IUserManager $userManager,
+ private readonly PrimaryObjectStoreConfig $objectStoreConfig,
+ private readonly IConfig $config,
+ ) {
+ parent::__construct();
+ }
+
+ protected function configure(): void {
+ parent::configure();
+ $this
+ ->setName('files:object:multi:users')
+ ->setDescription('Get the mapping between users and object store buckets')
+ ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, 'Only list users using the specified bucket')
+ ->addOption('object-store', 'o', InputOption::VALUE_REQUIRED, 'Only list users using the specified object store configuration')
+ ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'Only show the mapping for the specified user, ignores all other options');
+ }
+
+ public function execute(InputInterface $input, OutputInterface $output): int {
+ if ($userId = $input->getOption('user')) {
+ $user = $this->userManager->get($userId);
+ if (!$user) {
+ $output->writeln("<error>User $userId not found</error>");
+ return 1;
+ }
+ $users = new \ArrayIterator([$user]);
+ } else {
+ $bucket = (string)$input->getOption('bucket');
+ $objectStore = (string)$input->getOption('object-store');
+ if ($bucket !== '' && $objectStore === '') {
+ $users = $this->getUsers($this->config->getUsersForUserValue('homeobjectstore', 'bucket', $bucket));
+ } elseif ($bucket === '' && $objectStore !== '') {
+ $users = $this->getUsers($this->config->getUsersForUserValue('homeobjectstore', 'objectstore', $objectStore));
+ } elseif ($bucket) {
+ $users = $this->getUsers(array_intersect(
+ $this->config->getUsersForUserValue('homeobjectstore', 'bucket', $bucket),
+ $this->config->getUsersForUserValue('homeobjectstore', 'objectstore', $objectStore)
+ ));
+ } else {
+ $users = $this->userManager->getSeenUsers();
+ }
+ }
+
+ $this->writeStreamingTableInOutputFormat($input, $output, $this->infoForUsers($users), 100);
+ return 0;
+ }
+
+ /**
+ * @param string[] $userIds
+ * @return \Iterator<IUser>
+ */
+ private function getUsers(array $userIds): \Iterator {
+ foreach ($userIds as $userId) {
+ $user = $this->userManager->get($userId);
+ if ($user) {
+ yield $user;
+ }
+ }
+ }
+
+ /**
+ * @param \Iterator<IUser> $users
+ * @return \Iterator<array>
+ */
+ private function infoForUsers(\Iterator $users): \Iterator {
+ foreach ($users as $user) {
+ yield $this->infoForUser($user);
+ }
+ }
+
+ private function infoForUser(IUser $user): array {
+ return [
+ 'user' => $user->getUID(),
+ 'object-store' => $this->objectStoreConfig->getObjectStoreForUser($user),
+ 'bucket' => $this->objectStoreConfig->getSetBucketForUser($user) ?? 'unset',
+ ];
+ }
+}
diff --git a/apps/files/lib/Command/Object/ObjectUtil.php b/apps/files/lib/Command/Object/ObjectUtil.php
new file mode 100644
index 00000000000..5f053c2c42f
--- /dev/null
+++ b/apps/files/lib/Command/Object/ObjectUtil.php
@@ -0,0 +1,115 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\Files\Command\Object;
+
+use OCP\DB\QueryBuilder\IQueryBuilder;
+use OCP\Files\ObjectStore\IObjectStore;
+use OCP\IConfig;
+use OCP\IDBConnection;
+use OCP\Util;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class ObjectUtil {
+ public function __construct(
+ private IConfig $config,
+ private IDBConnection $connection,
+ ) {
+ }
+
+ private function getObjectStoreConfig(): ?array {
+ $config = $this->config->getSystemValue('objectstore_multibucket');
+ if (is_array($config)) {
+ $config['multibucket'] = true;
+ return $config;
+ }
+ $config = $this->config->getSystemValue('objectstore');
+ if (is_array($config)) {
+ if (!isset($config['multibucket'])) {
+ $config['multibucket'] = false;
+ }
+ return $config;
+ }
+
+ return null;
+ }
+
+ public function getObjectStore(?string $bucket, OutputInterface $output): ?IObjectStore {
+ $config = $this->getObjectStoreConfig();
+ if (!$config) {
+ $output->writeln('<error>Instance is not using primary object store</error>');
+ return null;
+ }
+ if ($config['multibucket'] && !$bucket) {
+ $output->writeln('<error>--bucket option required</error> because <info>multi bucket</info> is enabled.');
+ return null;
+ }
+
+ if (!isset($config['arguments'])) {
+ throw new \Exception('no arguments configured for object store configuration');
+ }
+ if (!isset($config['class'])) {
+ throw new \Exception('no class configured for object store configuration');
+ }
+
+ if ($bucket) {
+ // s3, swift
+ $config['arguments']['bucket'] = $bucket;
+ // azure
+ $config['arguments']['container'] = $bucket;
+ }
+
+ $store = new $config['class']($config['arguments']);
+ if (!$store instanceof IObjectStore) {
+ throw new \Exception('configured object store class is not an object store implementation');
+ }
+ return $store;
+ }
+
+ /**
+ * Check if an object is referenced in the database
+ */
+ public function objectExistsInDb(string $object): int|false {
+ if (!str_starts_with($object, 'urn:oid:')) {
+ return false;
+ }
+
+ $fileId = (int)substr($object, strlen('urn:oid:'));
+ $query = $this->connection->getQueryBuilder();
+ $query->select('fileid')
+ ->from('filecache')
+ ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
+ $result = $query->executeQuery();
+
+ if ($result->fetchOne() === false) {
+ return false;
+ }
+
+ return $fileId;
+ }
+
+ public function formatObjects(\Iterator $objects, bool $humanOutput): \Iterator {
+ foreach ($objects as $object) {
+ yield $this->formatObject($object, $humanOutput);
+ }
+ }
+
+ public function formatObject(array $object, bool $humanOutput): array {
+ $row = array_merge([
+ 'urn' => $object['urn'],
+ ], ($object['metadata'] ?? []));
+
+ if ($humanOutput && isset($row['size'])) {
+ $row['size'] = Util::humanFileSize($row['size']);
+ }
+ if (isset($row['mtime'])) {
+ $row['mtime'] = $row['mtime']->format(\DateTimeImmutable::ATOM);
+ }
+ return $row;
+ }
+}
diff --git a/apps/files/lib/Command/Object/Orphans.php b/apps/files/lib/Command/Object/Orphans.php
new file mode 100644
index 00000000000..f7132540fc8
--- /dev/null
+++ b/apps/files/lib/Command/Object/Orphans.php
@@ -0,0 +1,79 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\Files\Command\Object;
+
+use OC\Core\Command\Base;
+use OCP\DB\QueryBuilder\IQueryBuilder;
+use OCP\Files\ObjectStore\IObjectStoreMetaData;
+use OCP\IDBConnection;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class Orphans extends Base {
+ private const CHUNK_SIZE = 100;
+
+ private ?IQueryBuilder $query = null;
+
+ public function __construct(
+ private readonly ObjectUtil $objectUtils,
+ private readonly IDBConnection $connection,
+ ) {
+ parent::__construct();
+ }
+
+ private function getQuery(): IQueryBuilder {
+ if (!$this->query) {
+ $this->query = $this->connection->getQueryBuilder();
+ $this->query->select('fileid')
+ ->from('filecache')
+ ->where($this->query->expr()->eq('fileid', $this->query->createParameter('file_id')));
+ }
+ return $this->query;
+ }
+
+ protected function configure(): void {
+ parent::configure();
+ $this
+ ->setName('files:object:orphans')
+ ->setDescription('List all objects in the object store that don\'t have a matching entry in the database')
+ ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket to list the objects from, only required in cases where it can't be determined from the config");
+ }
+
+ public function execute(InputInterface $input, OutputInterface $output): int {
+ $objectStore = $this->objectUtils->getObjectStore($input->getOption('bucket'), $output);
+ if (!$objectStore) {
+ return self::FAILURE;
+ }
+
+ if (!$objectStore instanceof IObjectStoreMetaData) {
+ $output->writeln('<error>Configured object store does currently not support listing objects</error>');
+ return self::FAILURE;
+ }
+ $prefixLength = strlen('urn:oid:');
+
+ $objects = $objectStore->listObjects('urn:oid:');
+ $orphans = new \CallbackFilterIterator($objects, function (array $object) use ($prefixLength) {
+ $fileId = (int)substr($object['urn'], $prefixLength);
+ return !$this->fileIdInDb($fileId);
+ });
+
+ $orphans = $this->objectUtils->formatObjects($orphans, $input->getOption('output') === self::OUTPUT_FORMAT_PLAIN);
+ $this->writeStreamingTableInOutputFormat($input, $output, $orphans, self::CHUNK_SIZE);
+
+ return self::SUCCESS;
+ }
+
+ private function fileIdInDb(int $fileId): bool {
+ $query = $this->getQuery();
+ $query->setParameter('file_id', $fileId, IQueryBuilder::PARAM_INT);
+ $result = $query->executeQuery();
+ return $result->fetchOne() !== false;
+ }
+}
diff --git a/apps/files/lib/Command/Object/Put.php b/apps/files/lib/Command/Object/Put.php
new file mode 100644
index 00000000000..8516eb51183
--- /dev/null
+++ b/apps/files/lib/Command/Object/Put.php
@@ -0,0 +1,68 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\Files\Command\Object;
+
+use OCP\Files\IMimeTypeDetector;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Helper\QuestionHelper;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Question\ConfirmationQuestion;
+
+class Put extends Command {
+ public function __construct(
+ private ObjectUtil $objectUtils,
+ private IMimeTypeDetector $mimeTypeDetector,
+ ) {
+ parent::__construct();
+ }
+
+ protected function configure(): void {
+ $this
+ ->setName('files:object:put')
+ ->setDescription('Write a file to the object store')
+ ->addArgument('input', InputArgument::REQUIRED, 'Source local path, use - to read from STDIN')
+ ->addArgument('object', InputArgument::REQUIRED, 'Object to write')
+ ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket where to store the object, only required in cases where it can't be determined from the config");
+ ;
+ }
+
+ public function execute(InputInterface $input, OutputInterface $output): int {
+ $object = $input->getArgument('object');
+ $inputName = (string)$input->getArgument('input');
+ $objectStore = $this->objectUtils->getObjectStore($input->getOption('bucket'), $output);
+ if (!$objectStore) {
+ return -1;
+ }
+
+ if ($fileId = $this->objectUtils->objectExistsInDb($object)) {
+ $output->writeln("<error>Warning, object $object belongs to an existing file, overwriting the object contents can lead to unexpected behavior.</error>");
+ $output->writeln("You can use <info>occ files:put $inputName $fileId</info> to write to the file safely.");
+ $output->writeln('');
+
+ /** @var QuestionHelper $helper */
+ $helper = $this->getHelper('question');
+ $question = new ConfirmationQuestion('Write to the object anyway? [y/N] ', false);
+ if (!$helper->ask($input, $output, $question)) {
+ return -1;
+ }
+ }
+
+ $source = $inputName === '-' ? STDIN : fopen($inputName, 'r');
+ if (!$source) {
+ $output->writeln("<error>Failed to open $inputName</error>");
+ return self::FAILURE;
+ }
+ $objectStore->writeObject($object, $source, $this->mimeTypeDetector->detectPath($inputName));
+ return self::SUCCESS;
+ }
+
+}