You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.3 KiB

  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. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  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
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace OC\Core\Controller;
  24. use OCP\AppFramework\Http\Response;
  25. use OCP\Collaboration\Reference\IReferenceManager;
  26. use OCP\AppFramework\Controller;
  27. use OCP\AppFramework\Http;
  28. use OCP\AppFramework\Http\DataDownloadResponse;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\Files\AppData\IAppDataFactory;
  31. use OCP\Files\NotFoundException;
  32. use OCP\Files\NotPermittedException;
  33. use OCP\IRequest;
  34. class ReferenceController extends Controller {
  35. public function __construct(string $appName,
  36. IRequest $request,
  37. private IReferenceManager $referenceManager,
  38. private IAppDataFactory $appDataFactory) {
  39. parent::__construct($appName, $request);
  40. }
  41. /**
  42. * @PublicPage
  43. * @NoCSRFRequired
  44. * @param string $referenceId the reference cache key
  45. * @return Response
  46. */
  47. public function preview(string $referenceId): Response {
  48. $reference = $this->referenceManager->getReferenceByCacheKey($referenceId);
  49. try {
  50. $appData = $this->appDataFactory->get('core');
  51. $folder = $appData->getFolder('opengraph');
  52. $file = $folder->getFile($referenceId);
  53. $contentType = $reference === null || $reference->getImageContentType() === null
  54. ? $file->getMimeType()
  55. : $reference->getImageContentType();
  56. $response = new DataDownloadResponse(
  57. $file->getContent(),
  58. $referenceId,
  59. $contentType
  60. );
  61. } catch (NotFoundException|NotPermittedException $e) {
  62. $response = new DataResponse('', Http::STATUS_NOT_FOUND);
  63. }
  64. $response->cacheFor(3600, false, true);
  65. return $response;
  66. }
  67. }