1
0

ImageExportPlugin.php 3.3 KB

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