ImageExportPlugin.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\CardDAV;
  8. use OCP\Files\NotFoundException;
  9. use Sabre\CardDAV\Card;
  10. use Sabre\DAV\Server;
  11. use Sabre\DAV\ServerPlugin;
  12. use Sabre\HTTP\RequestInterface;
  13. use Sabre\HTTP\ResponseInterface;
  14. class ImageExportPlugin extends ServerPlugin {
  15. /** @var Server */
  16. protected $server;
  17. /**
  18. * ImageExportPlugin constructor.
  19. *
  20. * @param PhotoCache $cache
  21. */
  22. public function __construct(
  23. private PhotoCache $cache,
  24. ) {
  25. }
  26. /**
  27. * Initializes the plugin and registers event handlers
  28. *
  29. * @param Server $server
  30. * @return void
  31. */
  32. public function initialize(Server $server) {
  33. $this->server = $server;
  34. $this->server->on('method:GET', [$this, 'httpGet'], 90);
  35. }
  36. /**
  37. * Intercepts GET requests on addressbook urls ending with ?photo.
  38. *
  39. * @param RequestInterface $request
  40. * @param ResponseInterface $response
  41. * @return bool
  42. */
  43. public function httpGet(RequestInterface $request, ResponseInterface $response) {
  44. $queryParams = $request->getQueryParameters();
  45. // TODO: in addition to photo we should also add logo some point in time
  46. if (!array_key_exists('photo', $queryParams)) {
  47. return true;
  48. }
  49. $size = isset($queryParams['size']) ? (int)$queryParams['size'] : -1;
  50. $path = $request->getPath();
  51. $node = $this->server->tree->getNodeForPath($path);
  52. if (!($node instanceof Card)) {
  53. return true;
  54. }
  55. $this->server->transactionType = 'carddav-image-export';
  56. // Checking ACL, if available.
  57. if ($aclPlugin = $this->server->getPlugin('acl')) {
  58. /** @var \Sabre\DAVACL\Plugin $aclPlugin */
  59. $aclPlugin->checkPrivileges($path, '{DAV:}read');
  60. }
  61. // Fetch addressbook
  62. $addressbookpath = explode('/', $path);
  63. array_pop($addressbookpath);
  64. $addressbookpath = implode('/', $addressbookpath);
  65. /** @var AddressBook $addressbook */
  66. $addressbook = $this->server->tree->getNodeForPath($addressbookpath);
  67. $response->setHeader('Cache-Control', 'private, max-age=3600, must-revalidate');
  68. $response->setHeader('Etag', $node->getETag());
  69. try {
  70. $file = $this->cache->get($addressbook->getResourceId(), $node->getName(), $size, $node);
  71. $response->setHeader('Content-Type', $file->getMimeType());
  72. $fileName = $node->getName() . '.' . PhotoCache::ALLOWED_CONTENT_TYPES[$file->getMimeType()];
  73. $response->setHeader('Content-Disposition', "attachment; filename=$fileName");
  74. $response->setStatus(200);
  75. $response->setBody($file->getContent());
  76. } catch (NotFoundException $e) {
  77. $response->setStatus(404);
  78. }
  79. return false;
  80. }
  81. }