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

feat: Implement assetconfiguration endpoint #4300

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
['name' => 'settings#checkSettings', 'url' => 'settings/check', 'verb' => 'GET'],
['name' => 'settings#demoServers', 'url' => 'settings/demo', 'verb' => 'GET'],
['name' => 'settings#getFontNames', 'url' => 'settings/fonts', 'verb' => 'GET'],
['name' => 'settings#getJsonFontList', 'url' => 'settings/fonts.json', 'verb' => 'GET'],
['name' => 'settings#getFontFile', 'url' => 'settings/fonts/{name}', 'verb' => 'GET'],
['name' => 'settings#getFontFileOverview', 'url' => 'settings/fonts/{name}/overview', 'verb' => 'GET'],
['name' => 'settings#deleteFontFile', 'url' => 'settings/fonts/{name}', 'verb' => 'DELETE'],
['name' => 'settings#uploadFontFile', 'url' => 'settings/fonts', 'verb' => 'POST'],
Expand Down
1 change: 1 addition & 0 deletions composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
'OCA\\Richdocuments\\Controller\\FederationController' => $baseDir . '/../lib/Controller/FederationController.php',
'OCA\\Richdocuments\\Controller\\MentionController' => $baseDir . '/../lib/Controller/MentionController.php',
'OCA\\Richdocuments\\Controller\\OCSController' => $baseDir . '/../lib/Controller/OCSController.php',
'OCA\\Richdocuments\\Controller\\RemoteAssetController' => $baseDir . '/../lib/Controller/RemoteAssetController.php',
'OCA\\Richdocuments\\Controller\\SettingsController' => $baseDir . '/../lib/Controller/SettingsController.php',
'OCA\\Richdocuments\\Controller\\TargetController' => $baseDir . '/../lib/Controller/TargetController.php',
'OCA\\Richdocuments\\Controller\\TemplateFieldController' => $baseDir . '/../lib/Controller/TemplateFieldController.php',
Expand Down
1 change: 1 addition & 0 deletions composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class ComposerStaticInitRichdocuments
'OCA\\Richdocuments\\Controller\\FederationController' => __DIR__ . '/..' . '/../lib/Controller/FederationController.php',
'OCA\\Richdocuments\\Controller\\MentionController' => __DIR__ . '/..' . '/../lib/Controller/MentionController.php',
'OCA\\Richdocuments\\Controller\\OCSController' => __DIR__ . '/..' . '/../lib/Controller/OCSController.php',
'OCA\\Richdocuments\\Controller\\RemoteAssetController' => __DIR__ . '/..' . '/../lib/Controller/RemoteAssetController.php',
'OCA\\Richdocuments\\Controller\\SettingsController' => __DIR__ . '/..' . '/../lib/Controller/SettingsController.php',
'OCA\\Richdocuments\\Controller\\TargetController' => __DIR__ . '/..' . '/../lib/Controller/TargetController.php',
'OCA\\Richdocuments\\Controller\\TemplateFieldController' => __DIR__ . '/..' . '/../lib/Controller/TemplateFieldController.php',
Expand Down
130 changes: 130 additions & 0 deletions lib/Controller/RemoteAssetController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Richdocuments\Controller;

use OC\AppFramework\Http;
use OCA\Richdocuments\Controller\Attribute\RestrictToWopiServer;
use OCA\Richdocuments\Service\FontService;
use OCA\Richdocuments\TemplateManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\DataDownloadResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\Files\File;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IRequest;
use OCP\IURLGenerator;

class RemoteAssetController extends Controller {

public const SUPPORTED_PRESENTATION_MIMES = [
'application/vnd.oasis.opendocument.presentation',
'application/vnd.oasis.opendocument.presentation-template',
];

public function __construct(
string $appName,
IRequest $request,
private IURLGenerator $urlGenerator,
private FontService $fontService,
private TemplateManager $templateManager,
) {
parent::__construct($appName, $request);
}

#[NoAdminRequired]
#[PublicPage]
#[NoCSRFRequired]
#[RestrictToWopiServer]
#[FrontpageRoute(verb: 'GET', url: '/settings/assets')]
public function getRemoteAssets(): DataResponse {
return new DataResponse($this->getRemoteAssetData());
}

#[NoAdminRequired]
#[PublicPage]
#[NoCSRFRequired]
#[RestrictToWopiServer]
#[FrontpageRoute(verb: 'GET', url: '/settings/assets/{type}/{identifier}')]
public function downloadRemoteAsset(string $type, string $identifier): DataResponse|DataDownloadResponse {
if ($type === 'template-presentation') {
try {
$file = $this->templateManager->get((int)$identifier);
} catch (NotFoundException) {
}
}

if ($type === 'font') {
try {
$file = $this->fontService->getFontFile($identifier);
} catch (NotFoundException) {
}
}

if ($file instanceof File || $file instanceof ISimpleFile) {
$response = new DataDownloadResponse($file->getContent(), $file->getName(), $file->getMimeType());
return $response;
}

return new DataResponse([], Http::STATUS_NOT_FOUND);
}


/**
* @deprecated To remove once collabora no longer supports a searate remote font config (also cleanup fontsOnly param then)
*/
#[NoAdminRequired]
#[PublicPage]
#[NoCSRFRequired]
#[RestrictToWopiServer]
#[FrontpageRoute(verb: 'GET', url: '/settings/fonts.json')]
public function getJsonFontList(): DataResponse {
return new DataResponse($this->getRemoteAssetData(true));
}

private function getRemoteAssetData(bool $fontsOnly = false): array {
$data = [
'kind' => 'assetconfiguration',
'server' => $this->request->getServerHost(),

'fonts' => array_values(array_map(function ($font) {
return [
'uri' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.remoteasset.downloadRemoteAsset', [
'type' => 'font',
'identifier' => $font->getName(),
]),
'version' => $font->getEtag(),
];
}, $this->fontService->getFontFiles())),
];

if ($fontsOnly) {
return $data;
}

$data['templates'] = [
'presentation' => array_values(array_map(function ($template) {
return [
'uri' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.remoteasset.downloadRemoteAsset', [
'type' => 'template-presentation',
'identifier' => $template->getId(),
]),
'version' => $template->getEtag(),
];
}, array_filter($this->templateManager->getSystem('presentation'), function ($template) {
return in_array($template->getMimeType(), self::SUPPORTED_PRESENTATION_MIMES, true);
}))),
];

return $data;
}

}
55 changes: 0 additions & 55 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
Expand Down Expand Up @@ -313,60 +312,6 @@ public function getFontNames() {
return $response;
}

/**
* @NoAdminRequired
* @PublicPage
* @NoCSRFRequired
*
* @return JSONResponse|DataResponse
* @throws \OCP\Files\NotPermittedException
*/
public function getJsonFontList() {
$files = $this->fontService->getFontFiles();
$etags = array_map(
static fn (ISimpleFile $f) => $f->getETag(),
$files
);
$etag = md5(implode(',', $etags));
$ifNoneMatchHeader = $this->request->getHeader('If-None-Match');
if ($ifNoneMatchHeader && $ifNoneMatchHeader === $etag) {
return new DataResponse([], HTTP::STATUS_NOT_MODIFIED);
}

$fontList = $this->fontService->getFontList($files);
$response = new JSONResponse($fontList);
$response->addHeader('Etag', $etag);
return $response;
}

/**
* @NoAdminRequired
* @PublicPage
* @NoCSRFRequired
*
* @param string $name
* @return DataDisplayResponse|DataResponse
* @throws \OCP\Files\NotPermittedException
*/
public function getFontFile(string $name) {
try {
$fontFile = $this->fontService->getFontFile($name);
$etag = $fontFile->getETag();
$ifNoneMatchHeader = $this->request->getHeader('If-None-Match');
if ($ifNoneMatchHeader && $ifNoneMatchHeader === $etag) {
return new DataResponse([], HTTP::STATUS_NOT_MODIFIED);
}

return new DataDisplayResponse(
$fontFile->getContent(),
Http::STATUS_OK,
['Content-Type' => $fontFile->getMimeType(), 'Etag' => $etag]
);
} catch (NotFoundException) {
return new DataDisplayResponse('', Http::STATUS_NOT_FOUND);
}
}

/**
* @NoAdminRequired
* @PublicPage
Expand Down
3 changes: 3 additions & 0 deletions lib/TemplateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ class TemplateManager {

/** Accepted templates mime types */
public const MIMES_DOCUMENTS = [
'application/vnd.oasis.opendocument.text',
'application/vnd.oasis.opendocument.text-template',
'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
'application/msword'
];
public const MIMES_SHEETS = [
'application/vnd.oasis.opendocument.spreadsheet',
'application/vnd.oasis.opendocument.spreadsheet-template',
'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
'application/vnd.ms-excel'
];
public const MIMES_PRESENTATIONS = [
'application/vnd.oasis.opendocument.presentation',
'application/vnd.oasis.opendocument.presentation-template',
'application/vnd.openxmlformats-officedocument.presentationml.template',
'application/vnd.ms-powerpoint'
Expand Down
Loading