Bundled.php 990 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Preview;
  7. use OC\Archive\ZIP;
  8. use OCP\Files\File;
  9. use OCP\IImage;
  10. /**
  11. * Extracts a preview from files that embed them in an ZIP archive
  12. */
  13. abstract class Bundled extends ProviderV2 {
  14. protected function extractThumbnail(File $file, string $path): ?IImage {
  15. if ($file->getSize() === 0) {
  16. return null;
  17. }
  18. $sourceTmp = \OC::$server->getTempManager()->getTemporaryFile();
  19. $targetTmp = \OC::$server->getTempManager()->getTemporaryFile();
  20. $this->tmpFiles[] = $sourceTmp;
  21. $this->tmpFiles[] = $targetTmp;
  22. try {
  23. $content = $file->fopen('r');
  24. file_put_contents($sourceTmp, $content);
  25. $zip = new ZIP($sourceTmp);
  26. $zip->extractFile($path, $targetTmp);
  27. $image = new \OCP\Image();
  28. $image->loadFromFile($targetTmp);
  29. $image->fixOrientation();
  30. return $image;
  31. } catch (\Throwable $e) {
  32. return null;
  33. }
  34. }
  35. }