GeneratorHelper.php 2.3 KB

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