GeneratorHelper.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Preview;
  27. use OCP\Files\File;
  28. use OCP\Files\IRootFolder;
  29. use OCP\Files\SimpleFS\ISimpleFile;
  30. use OCP\IConfig;
  31. use OCP\IImage;
  32. use OCP\Image as OCPImage;
  33. use OCP\Preview\IProvider;
  34. use OCP\Preview\IProviderV2;
  35. /**
  36. * Very small wrapper class to make the generator fully unit testable
  37. */
  38. class GeneratorHelper {
  39. /** @var IRootFolder */
  40. private $rootFolder;
  41. /** @var IConfig */
  42. private $config;
  43. public function __construct(IRootFolder $rootFolder, IConfig $config) {
  44. $this->rootFolder = $rootFolder;
  45. $this->config = $config;
  46. }
  47. /**
  48. * @param IProviderV2 $provider
  49. * @param File $file
  50. * @param int $maxWidth
  51. * @param int $maxHeight
  52. *
  53. * @return bool|IImage
  54. */
  55. public function getThumbnail(IProviderV2 $provider, File $file, $maxWidth, $maxHeight, bool $crop = false) {
  56. if ($provider instanceof Imaginary) {
  57. return $provider->getCroppedThumbnail($file, $maxWidth, $maxHeight, $crop) ?? false;
  58. }
  59. return $provider->getThumbnail($file, $maxWidth, $maxHeight) ?? false;
  60. }
  61. /**
  62. * @param ISimpleFile $maxPreview
  63. * @return IImage
  64. */
  65. public function getImage(ISimpleFile $maxPreview) {
  66. $image = new OCPImage();
  67. $image->loadFromData($maxPreview->getContent());
  68. return $image;
  69. }
  70. /**
  71. * @param callable $providerClosure
  72. * @return IProviderV2
  73. */
  74. public function getProvider($providerClosure) {
  75. $provider = $providerClosure();
  76. if ($provider instanceof IProvider) {
  77. $provider = new ProviderV1Adapter($provider);
  78. }
  79. return $provider;
  80. }
  81. }