ImageExportPlugin.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\DAV\CardDAV;
  24. use OCP\Files\NotFoundException;
  25. use Sabre\CardDAV\Card;
  26. use Sabre\DAV\Server;
  27. use Sabre\DAV\ServerPlugin;
  28. use Sabre\HTTP\RequestInterface;
  29. use Sabre\HTTP\ResponseInterface;
  30. class ImageExportPlugin extends ServerPlugin {
  31. /** @var Server */
  32. protected $server;
  33. /** @var PhotoCache */
  34. private $cache;
  35. /**
  36. * ImageExportPlugin constructor.
  37. *
  38. * @param PhotoCache $cache
  39. */
  40. public function __construct(PhotoCache $cache) {
  41. $this->cache = $cache;
  42. }
  43. /**
  44. * Initializes the plugin and registers event handlers
  45. *
  46. * @param Server $server
  47. * @return void
  48. */
  49. public function initialize(Server $server) {
  50. $this->server = $server;
  51. $this->server->on('method:GET', [$this, 'httpGet'], 90);
  52. }
  53. /**
  54. * Intercepts GET requests on addressbook urls ending with ?photo.
  55. *
  56. * @param RequestInterface $request
  57. * @param ResponseInterface $response
  58. * @return bool
  59. */
  60. public function httpGet(RequestInterface $request, ResponseInterface $response) {
  61. $queryParams = $request->getQueryParameters();
  62. // TODO: in addition to photo we should also add logo some point in time
  63. if (!array_key_exists('photo', $queryParams)) {
  64. return true;
  65. }
  66. $size = isset($queryParams['size']) ? (int)$queryParams['size'] : -1;
  67. $path = $request->getPath();
  68. $node = $this->server->tree->getNodeForPath($path);
  69. if (!($node instanceof Card)) {
  70. return true;
  71. }
  72. $this->server->transactionType = 'carddav-image-export';
  73. // Checking ACL, if available.
  74. if ($aclPlugin = $this->server->getPlugin('acl')) {
  75. /** @var \Sabre\DAVACL\Plugin $aclPlugin */
  76. $aclPlugin->checkPrivileges($path, '{DAV:}read');
  77. }
  78. // Fetch addressbook
  79. $addressbookpath = explode('/', $path);
  80. array_pop($addressbookpath);
  81. $addressbookpath = implode('/', $addressbookpath);
  82. /** @var AddressBook $addressbook */
  83. $addressbook = $this->server->tree->getNodeForPath($addressbookpath);
  84. $response->setHeader('Cache-Control', 'private, max-age=3600, must-revalidate');
  85. $response->setHeader('Etag', $node->getETag() );
  86. $response->setHeader('Pragma', 'public');
  87. try {
  88. $file = $this->cache->get($addressbook->getResourceId(), $node->getName(), $size, $node);
  89. $response->setHeader('Content-Type', $file->getMimeType());
  90. $response->setHeader('Content-Disposition', 'attachment');
  91. $response->setStatus(200);
  92. $response->setBody($file->getContent());
  93. } catch (NotFoundException $e) {
  94. $response->setStatus(404);
  95. }
  96. return false;
  97. }
  98. }