1
0

GeneratorHelper.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\IImage;
  29. use OCP\Image as OCPImage;
  30. use OCP\Preview\IProvider;
  31. /**
  32. * Very small wrapper class to make the generator fully unit testable
  33. */
  34. class GeneratorHelper {
  35. /** @var IRootFolder */
  36. private $rootFolder;
  37. public function __construct(IRootFolder $rootFolder) {
  38. $this->rootFolder = $rootFolder;
  39. }
  40. /**
  41. * @param IProvider $provider
  42. * @param File $file
  43. * @param int $maxWidth
  44. * @param int $maxHeight
  45. * @return bool|IImage
  46. */
  47. public function getThumbnail(IProvider $provider, File $file, $maxWidth, $maxHeight) {
  48. list($view, $path) = $this->getViewAndPath($file);
  49. return $provider->getThumbnail($path, $maxWidth, $maxHeight, false, $view);
  50. }
  51. /**
  52. * @param File $file
  53. * @return array
  54. * This is required to create the old view and path
  55. */
  56. private function getViewAndPath(File $file) {
  57. $absPath = ltrim($file->getPath(), '/');
  58. $owner = explode('/', $absPath)[0];
  59. $userFolder = $this->rootFolder->getUserFolder($owner)->getParent();
  60. $nodes = $userFolder->getById($file->getId());
  61. $file = $nodes[0];
  62. $view = new View($userFolder->getPath());
  63. $path = $userFolder->getRelativePath($file->getPath());
  64. return [$view, $path];
  65. }
  66. /**
  67. * @param ISimpleFile $maxPreview
  68. * @return IImage
  69. */
  70. public function getImage(ISimpleFile $maxPreview) {
  71. $image = new OCPImage();
  72. $image->loadFromData($maxPreview->getContent());
  73. return $image;
  74. }
  75. /**
  76. * @param $provider
  77. * @return IProvider
  78. */
  79. public function getProvider($provider) {
  80. return $provider();
  81. }
  82. }