ReferenceController.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. * @author Kate Döen <kate.doeen@nextcloud.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  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
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. namespace OC\Core\Controller;
  25. use OCP\AppFramework\Controller;
  26. use OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http\Attribute\FrontpageRoute;
  28. use OCP\AppFramework\Http\DataDownloadResponse;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\Collaboration\Reference\IReferenceManager;
  31. use OCP\Files\AppData\IAppDataFactory;
  32. use OCP\Files\NotFoundException;
  33. use OCP\Files\NotPermittedException;
  34. use OCP\IRequest;
  35. class ReferenceController extends Controller {
  36. public function __construct(
  37. string $appName,
  38. IRequest $request,
  39. private IReferenceManager $referenceManager,
  40. private IAppDataFactory $appDataFactory,
  41. ) {
  42. parent::__construct($appName, $request);
  43. }
  44. /**
  45. * @PublicPage
  46. * @NoCSRFRequired
  47. *
  48. * Get a preview for a reference
  49. *
  50. * @param string $referenceId the reference cache key
  51. * @return DataDownloadResponse<Http::STATUS_OK, string, array{}>|DataResponse<Http::STATUS_NOT_FOUND, '', array{}>
  52. *
  53. * 200: Preview returned
  54. * 404: Reference not found
  55. */
  56. #[FrontpageRoute(verb: 'GET', url: '/core/references/preview/{referenceId}')]
  57. public function preview(string $referenceId): DataDownloadResponse|DataResponse {
  58. $reference = $this->referenceManager->getReferenceByCacheKey($referenceId);
  59. try {
  60. $appData = $this->appDataFactory->get('core');
  61. $folder = $appData->getFolder('opengraph');
  62. $file = $folder->getFile($referenceId);
  63. $contentType = $reference === null || $reference->getImageContentType() === null
  64. ? $file->getMimeType()
  65. : $reference->getImageContentType();
  66. $response = new DataDownloadResponse(
  67. $file->getContent(),
  68. $referenceId,
  69. $contentType
  70. );
  71. } catch (NotFoundException|NotPermittedException $e) {
  72. $response = new DataResponse('', Http::STATUS_NOT_FOUND);
  73. }
  74. $response->cacheFor(3600, false, true);
  75. return $response;
  76. }
  77. }