Skip to content

Commit

Permalink
feat: add repair step to install default fonts on installation
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Trovic <[email protected]>
  • Loading branch information
luka-nextcloud committed Nov 29, 2023
1 parent 9eea957 commit 5de1f0b
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 0 deletions.
6 changes: 6 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ You can also edit your documents off-line with the Collabora Office app from the
<command>OCA\Richdocuments\Command\ActivateConfig</command>
<command>OCA\Richdocuments\Command\ConvertToBigInt</command>
<command>OCA\Richdocuments\Command\UpdateEmptyTemplates</command>
<command>OCA\Richdocuments\Command\InstallDefaultFonts</command>
</commands>
<settings>
<admin>OCA\Richdocuments\Settings\Admin</admin>
<admin-section>OCA\Richdocuments\Settings\Section</admin-section>
<personal>OCA\Richdocuments\Settings\Personal</personal>
<personal-section>OCA\Richdocuments\Settings\Section</personal-section>
</settings>
<repair-steps>
<install>
<step>OCA\Richdocuments\Migration\InstallDefaultFonts</step>
</install>
</repair-steps>
</info>
Binary file added assets/fonts/AmaticSC-Regular.ttf
Binary file not shown.
8 changes: 8 additions & 0 deletions cypress/e2e/settings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { User } from '@nextcloud/cypress'

const usesHttps = Cypress.env('baseUrl').substr(0, 5) === 'https'
const collaboraUrl = Cypress.env('collaboraUrl')
const defaultFonts = ['AmaticSC-Regular.ttf']

describe('Office admin settings', function() {

Expand Down Expand Up @@ -77,6 +78,13 @@ describe('Office admin settings', function() {
.scrollIntoView()
.should('be.visible')

cy.get('#font-settings')
.scrollIntoView()
.should('be.visible')
defaultFonts.forEach(font => {
cy.get('.settings-entry.font-list-settings').contains(font)
})

// FIXME: Template settings only get visible after reload
cy.reload()
cy.get('#richdocuments-templates')
Expand Down
32 changes: 32 additions & 0 deletions lib/Command/InstallDefaultFonts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace OCA\Richdocuments\Command;

use OCA\Richdocuments\Service\FontService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class InstallDefaultFonts extends Command {
public function __construct(private FontService $fontService) {
parent::__construct();
}

protected function configure() {
$this
->setName('richdocuments:install-fonts')
->setDescription('Install default fonts');
}

protected function execute(InputInterface $input, OutputInterface $output) {
try {
$this->fontService->installDefaultFonts();
return 0;
} catch (\Exception $e) {
$output->writeln('<error>Failed to install default fonts</error>');
$output->writeln($e->getMessage());
$output->writeln($e->getTraceAsString());
return 1;
}
}
}
27 changes: 27 additions & 0 deletions lib/Migration/InstallDefaultFonts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace OCA\Richdocuments\Migration;

use OCA\Richdocuments\Service\FontService;
use OCP\IConfig;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;

class InstallDefaultFonts implements IRepairStep {
public function __construct(private IConfig $config, private FontService $fontService) {
}

public function getName(): string {
return 'Install default fonts';
}

public function run(IOutput $output): void {
$appVersion = $this->config->getAppValue('richdocuments', 'installed_version');

if (version_compare($appVersion, '8.2.2') < 1) {
return;
}

$this->fontService->installDefaultFonts();
}
}
38 changes: 38 additions & 0 deletions lib/Service/FontService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\Richdocuments\Service;

use Exception;
use OCA\Richdocuments\AppInfo\Application;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
Expand Down Expand Up @@ -242,4 +243,41 @@ private function generateFontOverview(ISimpleFile $fontFile): void {
// in the UI and display a fallback message
}
}

/**
* @throws Exception
*/
public function installDefaultFonts(): void {
$dirPath = __DIR__ . '/../../assets/fonts';

if (!is_dir($dirPath)) {
throw new Exception("Directory \"$dirPath\" does not exist!");
}

$handle = opendir($dirPath);

if (!$handle) {
throw new Exception("Failed opening directory \"$dirPath\"!");
}

while (false !== ($fileName = readdir($handle))) {
if ($fileName === '.' || $fileName === '..') {
continue;
}

$filePath = $dirPath . '/' . $fileName;

if (!is_file($filePath)) {
continue;
}

$fileHandle = fopen($filePath, 'r');

if (!$fileHandle) {
continue;
}

$this->uploadFontFile($fileName, $fileHandle);
}
}
}
5 changes: 5 additions & 0 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
<code>Command</code>
</UndefinedClass>
</file>
<file src="lib/Command/InstallDefaultFonts.php">
<UndefinedClass>
<code>Command</code>
</UndefinedClass>
</file>
<file src="lib/Controller/DirectViewController.php">
<InvalidScalarArgument>
<code><![CDATA[$item->getId()]]></code>
Expand Down

0 comments on commit 5de1f0b

Please sign in to comment.