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

Refactors files app commands #39150

Merged
merged 4 commits into from Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 6 additions & 8 deletions apps/files/lib/Command/Delete.php
Expand Up @@ -35,10 +35,9 @@
use Symfony\Component\Console\Question\ConfirmationQuestion;

class Delete extends Command {
private FileUtils $fileUtils;

public function __construct(FileUtils $fileUtils) {
$this->fileUtils = $fileUtils;
public function __construct(
private FileUtils $fileUtils,
) {
parent::__construct();
}

Expand All @@ -58,7 +57,7 @@ public function execute(InputInterface $input, OutputInterface $output): int {

if (!$node) {
$output->writeln("<error>file $fileInput not found</error>");
return 1;
return self::FAILURE;
}

$deleteConfirmed = $force;
Expand All @@ -72,7 +71,7 @@ public function execute(InputInterface $input, OutputInterface $output): int {
$question = new ConfirmationQuestion("<info>$fileInput</info> in a shared file, do you want to unshare the file from <info>$user</info> instead of deleting the source file? [Y/n] ", true);
if ($helper->ask($input, $output, $question)) {
$storage->unshareStorage();
return 0;
return self::SUCCESS;
} else {
$node = $storage->getShare()->getNode();
$output->writeln("");
Expand Down Expand Up @@ -110,7 +109,6 @@ public function execute(InputInterface $input, OutputInterface $output): int {
}
}

return 0;
return self::SUCCESS;
}

}
16 changes: 6 additions & 10 deletions apps/files/lib/Command/DeleteOrphanedFiles.php
Expand Up @@ -35,17 +35,13 @@
class DeleteOrphanedFiles extends Command {
public const CHUNK_SIZE = 200;

/**
* @var IDBConnection
*/
protected $connection;

public function __construct(IDBConnection $connection) {
$this->connection = $connection;
public function __construct(
protected IDBConnection $connection,
) {
parent::__construct();
}

protected function configure() {
protected function configure(): void {
$this
->setName('files:cleanup')
->setDescription('cleanup filecache');
Expand Down Expand Up @@ -81,10 +77,10 @@ public function execute(InputInterface $input, OutputInterface $output): int {

$deletedMounts = $this->cleanupOrphanedMounts();
$output->writeln("$deletedMounts orphaned mount entries deleted");
return 0;
return self::SUCCESS;
}

private function cleanupOrphanedMounts() {
private function cleanupOrphanedMounts(): int {
$deletedEntries = 0;

$query = $this->connection->getQueryBuilder();
Expand Down
60 changes: 29 additions & 31 deletions apps/files/lib/Command/Get.php
Expand Up @@ -32,10 +32,9 @@
use Symfony\Component\Console\Output\OutputInterface;

class Get extends Command {
private FileUtils $fileUtils;

public function __construct(FileUtils $fileUtils) {
$this->fileUtils = $fileUtils;
public function __construct(
private FileUtils $fileUtils,
) {
parent::__construct();
}

Expand All @@ -54,36 +53,35 @@ public function execute(InputInterface $input, OutputInterface $output): int {

if (!$node) {
$output->writeln("<error>file $fileInput not found</error>");
return 1;
return self::FAILURE;
}

if ($node instanceof File) {
$isTTY = stream_isatty(STDOUT);
if ($outputName === null && $isTTY && $node->getMimePart() !== 'text') {
$output->writeln([
"<error>Warning: Binary output can mess up your terminal</error>",
" Use <info>occ files:get $fileInput -</info> to output it to the terminal anyway",
" Or <info>occ files:get $fileInput <FILE></info> to save to a file instead"
]);
return 1;
}
$source = $node->fopen('r');
if (!$source) {
$output->writeln("<error>Failed to open $fileInput for reading</error>");
return 1;
}
$target = ($outputName === null || $outputName === '-') ? STDOUT : fopen($outputName, 'w');
if (!$target) {
$output->writeln("<error>Failed to open $outputName for reading</error>");
return 1;
}

stream_copy_to_stream($source, $target);
return 0;
} else {
if (!($node instanceof File)) {
$output->writeln("<error>$fileInput is a directory</error>");
return 1;
return self::FAILURE;
}

$isTTY = stream_isatty(STDOUT);
if ($outputName === null && $isTTY && $node->getMimePart() !== 'text') {
$output->writeln([
"<error>Warning: Binary output can mess up your terminal</error>",
" Use <info>occ files:get $fileInput -</info> to output it to the terminal anyway",
" Or <info>occ files:get $fileInput <FILE></info> to save to a file instead"
]);
return self::FAILURE;
}
$source = $node->fopen('r');
if (!$source) {
$output->writeln("<error>Failed to open $fileInput for reading</error>");
return self::FAILURE;
}
$target = ($outputName === null || $outputName === '-') ? STDOUT : fopen($outputName, 'w');
if (!$target) {
$output->writeln("<error>Failed to open $outputName for reading</error>");
return self::FAILURE;
}
}

stream_copy_to_stream($source, $target);
return self::SUCCESS;
}
}
9 changes: 4 additions & 5 deletions apps/files/lib/Command/Object/Delete.php
Expand Up @@ -34,10 +34,9 @@
use Symfony\Component\Console\Question\ConfirmationQuestion;

class Delete extends Command {
private ObjectUtil $objectUtils;

public function __construct(ObjectUtil $objectUtils) {
$this->objectUtils = $objectUtils;
public function __construct(
private ObjectUtil $objectUtils,
) {
parent::__construct();
}

Expand Down Expand Up @@ -73,6 +72,6 @@ public function execute(InputInterface $input, OutputInterface $output): int {
if ($helper->ask($input, $output, $question)) {
$objectStore->deleteObject($object);
}
return 0;
return self::SUCCESS;
}
}
41 changes: 20 additions & 21 deletions apps/files/lib/Command/Object/Get.php
Expand Up @@ -31,10 +31,9 @@
use Symfony\Component\Console\Output\OutputInterface;

class Get extends Command {
private ObjectUtil $objectUtils;

public function __construct(ObjectUtil $objectUtils) {
$this->objectUtils = $objectUtils;
public function __construct(
private ObjectUtil $objectUtils,
) {
parent::__construct();
}

Expand All @@ -52,29 +51,29 @@ public function execute(InputInterface $input, OutputInterface $output): int {
$outputName = $input->getArgument('output');
$objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output);
if (!$objectStore) {
return 1;
return self::FAILURE;
}

if (!$objectStore->objectExists($object)) {
$output->writeln("<error>Object $object does not exist</error>");
return 1;
} else {
try {
$source = $objectStore->readObject($object);
} catch (\Exception $e) {
$msg = $e->getMessage();
$output->writeln("<error>Failed to read $object from object store: $msg</error>");
return 1;
}
$target = $outputName === '-' ? STDOUT : fopen($outputName, 'w');
if (!$target) {
$output->writeln("<error>Failed to open $outputName for writing</error>");
return 1;
}
return self::FAILURE;
}

stream_copy_to_stream($source, $target);
return 0;
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;
}

}
41 changes: 20 additions & 21 deletions apps/files/lib/Command/Object/ObjectUtil.php
Expand Up @@ -30,12 +30,10 @@
use Symfony\Component\Console\Output\OutputInterface;

class ObjectUtil {
private IConfig $config;
private IDBConnection $connection;

public function __construct(IConfig $config, IDBConnection $connection) {
$this->config = $config;
$this->connection = $connection;
public function __construct(
private IConfig $config,
private IDBConnection $connection,
) {
}

private function getObjectStoreConfig(): ?array {
Expand All @@ -50,9 +48,9 @@ private function getObjectStoreConfig(): ?array {
$config['multibucket'] = false;
}
return $config;
} else {
return null;
}

return null;
}

public function getObjectStore(?string $bucket, OutputInterface $output): ?IObjectStore {
Expand Down Expand Up @@ -91,20 +89,21 @@ public function getObjectStore(?string $bucket, OutputInterface $output): ?IObje
* Check if an object is referenced in the database
*/
public function objectExistsInDb(string $object): int|false {
if (str_starts_with($object, 'urn:oid:')) {
$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 $fileId;
} else {
return false;
}
} else {
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;
}
}
14 changes: 6 additions & 8 deletions apps/files/lib/Command/Object/Put.php
Expand Up @@ -33,12 +33,10 @@
use Symfony\Component\Console\Question\ConfirmationQuestion;

class Put extends Command {
private ObjectUtil $objectUtils;
private IMimeTypeDetector $mimeTypeDetector;

public function __construct(ObjectUtil $objectUtils, IMimeTypeDetector $mimeTypeDetector) {
$this->objectUtils = $objectUtils;
$this->mimeTypeDetector = $mimeTypeDetector;
public function __construct(
private ObjectUtil $objectUtils,
private IMimeTypeDetector $mimeTypeDetector,
) {
parent::__construct();
}

Expand Down Expand Up @@ -75,10 +73,10 @@ public function execute(InputInterface $input, OutputInterface $output): int {
$source = $inputName === '-' ? STDIN : fopen($inputName, 'r');
if (!$source) {
$output->writeln("<error>Failed to open $inputName</error>");
return 1;
return self::FAILURE;
}
$objectStore->writeObject($object, $source, $this->mimeTypeDetector->detectPath($inputName));
return 0;
return self::SUCCESS;
}

}
21 changes: 9 additions & 12 deletions apps/files/lib/Command/Put.php
Expand Up @@ -34,12 +34,10 @@
use Symfony\Component\Console\Output\OutputInterface;

class Put extends Command {
private FileUtils $fileUtils;
private IRootFolder $rootFolder;

public function __construct(FileUtils $fileUtils, IRootFolder $rootFolder) {
$this->fileUtils = $fileUtils;
$this->rootFolder = $rootFolder;
public function __construct(
private FileUtils $fileUtils,
private IRootFolder $rootFolder,
) {
parent::__construct();
}

Expand All @@ -58,29 +56,28 @@ public function execute(InputInterface $input, OutputInterface $output): int {

if ($node instanceof Folder) {
$output->writeln("<error>$fileOutput is a folder</error>");
return 1;
return self::FAILURE;
}
if (!$node and is_numeric($fileOutput)) {
$output->writeln("<error>$fileOutput not found</error>");
return 1;
return self::FAILURE;
}

$source = ($inputName === null || $inputName === '-') ? STDIN : fopen($inputName, 'r');
if (!$source) {
$output->writeln("<error>Failed to open $inputName</error>");
return 1;
return self::FAILURE;
}
if ($node instanceof File) {
$target = $node->fopen('w');
if (!$target) {
$output->writeln("<error>Failed to open $fileOutput</error>");
return 1;
return self::FAILURE;
}
stream_copy_to_stream($source, $target);
} else {
$this->rootFolder->newFile($fileOutput, $source);
}
return 0;
return self::SUCCESS;
}

}