1
0

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