Add libresign cli command

This commit is contained in:
Vitor Mattos 2022-04-26 11:36:27 -03:00
commit 4f6d2bbb74
No known key found for this signature in database
GPG key ID: B7AB4B76A7CA7318
5 changed files with 122 additions and 9 deletions

View file

@ -13,6 +13,11 @@ Types of changes:
- *Security* in case of vulnerabilities.
<!-- changelog-linker -->
## 5.1.0 - 2022-04-26
# Added
- Command to install LibreSign cli
## 5.0.0 - 2022-04-25
# Added and updated

View file

@ -173,6 +173,49 @@ class Base extends CommandBase {
$this->config->deleteAppValue(Application::APP_ID, 'jsignpdf_jar_path');
}
protected function installCli(OutputInterface $output): void {
$folder = $this->getFolder();
if (PHP_OS_FAMILY === 'Windows') {
throw new \RuntimeException('LibreSign CLI do not work in Windows!');
} elseif (PHP_OS_FAMILY === 'Darwin') {
$url = 'https://github.com/LibreSign/libresign-cli/releases/download/v0.0.4/libresign_0.0.4_Linux_arm64';
} elseif (PHP_OS_FAMILY === 'Linux') {
if (PHP_INT_SIZE === 4) {
$url = 'https://github.com/LibreSign/libresign-cli/releases/download/v0.0.4/libresign_0.0.4_Linux_i386';
} else {
$url = 'https://github.com/LibreSign/libresign-cli/releases/download/v0.0.4/libresign_0.0.4_Linux_x86_64';
}
}
$file = $folder->newFile('libresign-cli');
$fullPath = $this->getDataDir() . DIRECTORY_SEPARATOR . $file->getInternalPath();
$this->download($output, $url, 'libresign-cli', $fullPath);
if (PHP_OS_FAMILY !== 'Windows') {
chmod($fullPath, 0700);
}
$this->config->setAppValue(Application::APP_ID, 'libresign_cli_path', $fullPath);
}
protected function uninstallCli(): void {
$libresignCliPath = $this->config->getAppValue(Application::APP_ID, 'libresign_cli_path');
if (!$libresignCliPath) {
return;
}
$appFolder = $this->getAppRootFolder();
$name = $appFolder->getName();
// Remove prefix
$path = explode($name, $libresignCliPath)[1];
try {
$folder = $appFolder->get($path);
$folder->delete();
} catch (NotFoundException $e) {
}
$this->config->deleteAppValue(Application::APP_ID, 'libresign_cli_path');
}
protected function installCfssl(OutputInterface $output): void {
$folder = $this->getFolder();

View file

@ -49,6 +49,11 @@ class Install extends Base {
InputOption::VALUE_NONE,
'CFSSL'
)
->addOption('cli',
null,
InputOption::VALUE_NONE,
'LibreSign CLI'
)
->addOption('java',
null,
InputOption::VALUE_NONE,
@ -71,6 +76,10 @@ class Install extends Base {
$this->installCfssl($output);
$ok = true;
}
if ($input->getOption('cli') || $all) {
$this->installCli($output);
$ok = true;
}
if (!$ok) {
$output->writeln('<error>Please inform what you want to install</error>');
$output->writeln('<error>--all to all</error>');

View file

@ -49,6 +49,11 @@ class Uninstall extends Base {
InputOption::VALUE_NONE,
'CFSSL'
)
->addOption('cli',
null,
InputOption::VALUE_NONE,
'LibreSign CLI'
)
->addOption('java',
null,
InputOption::VALUE_NONE,
@ -71,6 +76,10 @@ class Uninstall extends Base {
$this->uninstallCfssl();
$ok = true;
}
if ($input->getOption('cli') || $all) {
$this->uninstallCli($output);
$ok = true;
}
if (!$ok) {
$output->writeln('<error>Please inform what you want to install</error>');
$output->writeln('<error>--all to all</error>');

View file

@ -6,25 +6,43 @@ namespace OCA\Libresign\Migration;
use Closure;
use Doctrine\DBAL\Types\Types;
use OCA\Libresign\Handler\TCPDILibresign;
use OC\SystemConfig;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Command\Install;
use OCP\DB\ISchemaWrapper;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\NullOutput;
/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version2040Date20211027183759 extends SimpleMigrationStep {
/** @var IRootFolder*/
private $root;
/** @var IDBConnection */
private $connection;
/** @var Install */
private $install;
/** @var IConfig */
private $config;
/** @var SystemConfig */
private $systemConfig;
/** @var array */
private $rows;
public function __construct(IRootFolder $root, IDBConnection $connection) {
public function __construct(IRootFolder $root,
IDBConnection $connection,
IRootFolder $rootfolder,
Install $install,
IConfig $config,
SystemConfig $systemConfig) {
$this->connection = $connection;
$this->install = $install;
$this->config = $config;
$this->systemConfig = $systemConfig;
$this->rootFolder = $rootfolder;
$this->root = $root;
}
@ -57,14 +75,13 @@ class Version2040Date20211027183759 extends SimpleMigrationStep {
}
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void {
$cli = $this->getLibesignCli();
foreach ($this->rows as $row) {
$userFolder = $this->root->getUserFolder($row['user_id']);
/** @var File[] */
$file = $userFolder->getById($row['node_id']);
if (count($file) >= 1) {
$pdf = new TCPDILibresign();
$pdf->setSourceData($file[0]->getContent());
$data = $pdf->getPagesMetadata();
$data['extension'] = 'pdf';
$data = $this->getMetadataFromCli($cli, $file[0]->getPath());
$json = json_encode($data);
$query = $this->connection->getQueryBuilder();
$query
@ -76,4 +93,34 @@ class Version2040Date20211027183759 extends SimpleMigrationStep {
}
}
}
private function getMetadataFromCli(string $cli, string $filePath): array {
$fullPath = $this->getDataDir() . $filePath;
$json = shell_exec($cli . ' info ' . $fullPath);
$array = json_decode($json, true);
$output = [
'p' => count($array['pages']),
'extension' => 'pdf',
];
foreach ($array['pages'] as $page) {
$output['d'][] = [
'w' => $page['width'],
'h' => $page['height'],
];
}
return $output;
}
private function getDataDir(): string {
return $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT . '/data/');
}
private function getLibesignCli(): string {
$path = $this->config->getAppValue(Application::APP_ID, 'libresign_cli_path');
if (!file_exists($path)) {
$this->install->run(new StringInput('--cli'), new NullOutput());
$path = $this->config->getAppValue(Application::APP_ID, 'libresign_cli_path');
}
return $path;
}
}