Skip to content

Commit

Permalink
add output option for shareslist command
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorentPoinsaut committed Sep 28, 2022
1 parent aea05b6 commit 082cf5e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 78 deletions.
68 changes: 0 additions & 68 deletions lib/BackgroundJob/EmailNotification.php

This file was deleted.

11 changes: 9 additions & 2 deletions lib/Command/ListShares.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,22 @@ public function configure() {
'f',
InputOption::VALUE_OPTIONAL,
'Filter shares, possible values: owner, initiator, recipient, token, has-expiration, no-expiration'
)
->addOption(
'output',
'o',
InputOption::VALUE_OPTIONAL,
'Output format (json or csv, default is json)',
'json'
);
parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$user = $input->getOption('user');
$path = $input->getOption('path');
$token = $input->getOption('token');
$filter = $this->sharesList->filterStringToInt($input->getOption('filter'));
$outputOpt = $input->getOption('output');

if ($user === null && $token === null) {
$shares = [];
Expand All @@ -111,7 +118,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$shares = iter\toArray($this->sharesList->getFormattedShares($user, $filter, $path, $token));
}

$output->writeln(json_encode($shares, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
$output->writeln($this->sharesList->getSerializedShares($shares, $outputOpt));
return 0;
}
}
1 change: 0 additions & 1 deletion lib/Command/SendShares.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public function configure() {
InputOption::VALUE_OPTIONAL,
'Filter shares, possible values: owner, initiator, recipient, token, has-expiration, no-expiration'
);
parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): int {
Expand Down
9 changes: 2 additions & 7 deletions lib/Service/ReportSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
use OCP\Mail\IMailer;
use OCP\Util;
use Psr\Log\LoggerInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\Serializer;

class ReportSender {
public const ACTIVITY_LIMIT = 20;
Expand Down Expand Up @@ -110,15 +107,13 @@ public function sendReport(
$shares = iter\toArray($this->sharesList->getFormattedShares($userId, $filter, $path, $token));
}

$encoders = [new CsvEncoder(), new JsonEncoder()];
$serializer = new Serializer([], $encoders);
$json_attachment = $this->mailer->createAttachment(
$serializer->serialize($shares, 'json', ['json_encode_options' => JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE]),
$this->sharesList->getSerializedShares($shares, 'json'),
'report.json',
'application/json; charset=utf-8'
);
$csv_attachment = $this->mailer->createAttachment(
$serializer->serialize($shares, 'csv', []),
$this->sharesList->getSerializedShares($shares, 'csv'),
'report.csv',
'text/csv; charset=utf-8'
);
Expand Down
21 changes: 21 additions & 0 deletions lib/Service/SharesList.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
use OCP\Share;
use OCP\Share\IManager as ShareManager;
use OCP\Share\IShare;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\Serializer;

class SharesList {

Expand Down Expand Up @@ -307,4 +310,22 @@ public function filterStringToInt(?string $filterString): int {

return $filter;
}

public function getSerializedShares(array $shares, ?string $format = 'json'): string
{
switch ($format) {
case 'csv':
$encoders = [new CsvEncoder()];
$context = [];
break;
default:
$encoders = [new JsonEncoder()];
$format = 'json';
$context = ['json_encode_options' => JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE];
break;
}

$serializer = new Serializer([], $encoders);
return $serializer->serialize($shares, $format, $context);
}
}

0 comments on commit 082cf5e

Please sign in to comment.