Skip to content
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
30 changes: 20 additions & 10 deletions collabora_online.module
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
*/

use Drupal\collabora_online\CollaboraUrl;
use Drupal\collabora_online\Cool\CoolUtils;
use Drupal\collabora_online\Discovery\DiscoveryFetcherInterface;
use Drupal\collabora_online\Exception\CollaboraNotAvailableException;
use Drupal\collabora_online\MediaHelperInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Utility\Error;
use Drupal\media\MediaInterface;

/**
Expand Down Expand Up @@ -116,15 +118,23 @@ function collabora_online_entity_operation(EntityInterface $entity): array {
],
];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make this function complete, we should also check for viewability, not just editability.
However we would have to implement the fallback logic from 'view' to 'edit' and 'view_comment', to get a desirable outcome with Collabora.


if (
CoolUtils::canEditMimeType($type) &&
$media->access('edit in collabora')
) {
$entries['collabora_online_edit'] = [
'title' => t("Edit in Collabora Online"),
'weight' => 50,
'url' => CollaboraUrl::editMedia($media),
];
if ($media->access('edit in collabora')) {
/** @var \Drupal\collabora_online\Discovery\DiscoveryFetcherInterface $discovery_fetcher */
$discovery_fetcher = \Drupal::service(DiscoveryFetcherInterface::class);
try {
$discovery = $discovery_fetcher->getDiscovery();
$wopi_client_edit_url = $discovery->getWopiClientURL($type, 'edit');
if ($wopi_client_edit_url !== NULL) {
$entries['collabora_online_edit'] = [
'title' => t("Edit in Collabora Online"),
'weight' => 50,
'url' => CollaboraUrl::editMedia($media),
];
}
}
catch (CollaboraNotAvailableException $e) {
Error::logException(\Drupal::logger('cool'), $e);
}
}

return $entries;
Expand Down
23 changes: 21 additions & 2 deletions src/Controller/ViewerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Drupal\collabora_online\Discovery\DiscoveryFetcherInterface;
use Drupal\collabora_online\Exception\CollaboraNotAvailableException;
use Drupal\collabora_online\Jwt\JwtTranscoderInterface;
use Drupal\collabora_online\MediaHelperInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\AutowireTrait;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
Expand All @@ -43,6 +44,7 @@ public function __construct(
protected readonly DiscoveryFetcherInterface $discoveryFetcher,
protected readonly JwtTranscoderInterface $jwtTranscoder,
protected readonly RendererInterface $renderer,
protected readonly MediaHelperInterface $mediaHelper,
#[Autowire('logger.channel.collabora_online')]
protected readonly LoggerInterface $logger,
protected readonly ConfigFactoryInterface $configFactory,
Expand All @@ -67,10 +69,27 @@ public function __construct(
* Response suitable for iframe, without the usual page decorations.
*/
public function editor(MediaInterface $media, Request $request, $edit = FALSE): Response {
$file = $this->mediaHelper->getFileForMedia($media);
// Treat "no file" and "file without MIME type" the same.
$mimetype = $file?->getMimeType();
if ($mimetype === NULL) {
return new Response(
(string) $this->t('The Collabora Online editor/viewer is not available for media without a file attached.'),
Response::HTTP_BAD_REQUEST,
['content-type' => 'text/plain'],
);
}
try {
// @todo Get client url for the correct MIME type.
$discovery = $this->discoveryFetcher->getDiscovery();
$wopi_client_url = $discovery->getWopiClientURL();

$wopi_client_url = $edit
? $discovery->getWopiClientURL($mimetype, 'edit')
: ($discovery->getWopiClientURL($mimetype, 'view')
// With the typical discovery.xml from Collabora, some MIME types that
// are viewable have an 'edit' or 'view_comment' action but no 'view'
// action.
?? $discovery->getWopiClientURL($mimetype, 'edit')
?? $discovery->getWopiClientURL($mimetype, 'view_comment'));
}
catch (CollaboraNotAvailableException $e) {
$this->logger->warning(
Expand Down
43 changes: 0 additions & 43 deletions src/Cool/CoolUtils.php

This file was deleted.

8 changes: 6 additions & 2 deletions src/Discovery/Discovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function getWopiClientURL(string $mimetype = 'text/plain'): ?string {
$result = $this->parsedXml->xpath(sprintf('/wopi-discovery/net-zone/app[@name=\'%s\']/action', $mimetype));
public function getWopiClientURL(string $mimetype, string $action): ?string {
$result = $this->parsedXml->xpath(sprintf(
"/wopi-discovery/net-zone/app[@name='%s']/action[@name='%s']",
$mimetype,
$action,
));
if (empty($result[0]['urlsrc'][0])) {
return NULL;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Discovery/DiscoveryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ interface DiscoveryInterface {
* @param string $mimetype
* Mime type for which to get the WOPI client URL.
* This refers to config entries in the discovery.xml file.
* @param string $action
* Name of the action/operation for which to get the url.
* Typical values are 'view', 'edit' or 'view_comment'.
*
* @return string|null
* The WOPI client URL, or NULL if none provided for the MIME type.
* The WOPI client URL, or NULL if none provided for the MIME type and
* operation.
*/
public function getWopiClientURL(string $mimetype = 'text/plain'): ?string;
public function getWopiClientURL(string $mimetype, string $action): ?string;

/**
* Gets the public key used for proofing.
Expand Down
8 changes: 7 additions & 1 deletion tests/fixtures/discovery.mimetypes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
<app name="text/csv">
<action default="true" ext="" name="edit" urlsrc="http://csv.collabora.test:9980/browser/61cf2b4/cool.html?"/>
</app>
<app name="text/csv">
<action default="true" ext="" name="view" urlsrc="http://view.csv.collabora.test:9980/browser/61cf2b4/cool.html?"/>
</app>
<app name="text/plain">
<action default="true" ext="" name="edit" urlsrc="http://collabora.test:9980/browser/61cf2b4/cool.html?"/>
<action default="true" ext="" name="view" urlsrc="http://collabora.test:9980/browser/61cf2b4/cool.html?"/>
</app>
<app name="image/png">
<action urlsrc="http://png.collabora.test:9980/browser/61cf2b4/cool.html?"/>
</app>
</net-zone>
</wopi-discovery>
Loading
Loading