GeneratorHelper.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Preview;
  7. use OCP\Files\File;
  8. use OCP\Files\IRootFolder;
  9. use OCP\Files\SimpleFS\ISimpleFile;
  10. use OCP\IConfig;
  11. use OCP\IImage;
  12. use OCP\Image as OCPImage;
  13. use OCP\Preview\IProvider;
  14. use OCP\Preview\IProviderV2;
  15. /**
  16. * Very small wrapper class to make the generator fully unit testable
  17. */
  18. class GeneratorHelper {
  19. /** @var IRootFolder */
  20. private $rootFolder;
  21. /** @var IConfig */
  22. private $config;
  23. public function __construct(IRootFolder $rootFolder, IConfig $config) {
  24. $this->rootFolder = $rootFolder;
  25. $this->config = $config;
  26. }
  27. /**
  28. * @param IProviderV2 $provider
  29. * @param File $file
  30. * @param int $maxWidth
  31. * @param int $maxHeight
  32. *
  33. * @return bool|IImage
  34. */
  35. public function getThumbnail(IProviderV2 $provider, File $file, $maxWidth, $maxHeight, bool $crop = false) {
  36. if ($provider instanceof Imaginary) {
  37. return $provider->getCroppedThumbnail($file, $maxWidth, $maxHeight, $crop) ?? false;
  38. }
  39. return $provider->getThumbnail($file, $maxWidth, $maxHeight) ?? false;
  40. }
  41. /**
  42. * @param ISimpleFile $maxPreview
  43. * @return IImage
  44. */
  45. public function getImage(ISimpleFile $maxPreview) {
  46. $image = new OCPImage();
  47. $image->loadFromData($maxPreview->getContent());
  48. return $image;
  49. }
  50. /**
  51. * @param callable $providerClosure
  52. * @return IProviderV2
  53. */
  54. public function getProvider($providerClosure) {
  55. $provider = $providerClosure();
  56. if ($provider instanceof IProvider) {
  57. $provider = new ProviderV1Adapter($provider);
  58. }
  59. return $provider;
  60. }
  61. }