GeneratorHelper.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 img;
  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. return new img($maxPreview->getContent());
  72. }
  73. /**
  74. * @param $provider
  75. * @return IProvider
  76. */
  77. public function getProvider($provider) {
  78. return $provider();
  79. }
  80. }