Office.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Olivier Paroz <github@oparoz.com>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Robin McCorkell <robin@mccorkell.me.uk>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Tor Lillqvist <tml@collabora.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Preview;
  30. use OCP\Files\File;
  31. use OCP\Files\FileInfo;
  32. use OCP\IImage;
  33. use Psr\Log\LoggerInterface;
  34. abstract class Office extends ProviderV2 {
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function isAvailable(FileInfo $file): bool {
  39. return is_string($this->options['officeBinary']);
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  45. if (!$this->isAvailable($file)) {
  46. return null;
  47. }
  48. $absPath = $this->getLocalFile($file);
  49. $tmpDir = \OC::$server->getTempManager()->getTempBaseDir();
  50. $defaultParameters = ' -env:UserInstallation=file://' . escapeshellarg($tmpDir . '/owncloud-' . \OC_Util::getInstanceId() . '/') . ' --headless --nologo --nofirststartwizard --invisible --norestore --convert-to png --outdir ';
  51. $clParameters = \OC::$server->getConfig()->getSystemValue('preview_office_cl_parameters', $defaultParameters);
  52. $cmd = $this->options['officeBinary'] . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath);
  53. exec($cmd, $output, $returnCode);
  54. if ($returnCode !== 0) {
  55. $this->cleanTmpFiles();
  56. return null;
  57. }
  58. //create imagick object from png
  59. $pngPreview = null;
  60. try {
  61. [$dirname, , , $filename] = array_values(pathinfo($absPath));
  62. $pngPreview = $tmpDir . '/' . $filename . '.png';
  63. $png = new \imagick($pngPreview . '[0]');
  64. $png->setImageFormat('jpg');
  65. } catch (\Exception $e) {
  66. $this->cleanTmpFiles();
  67. unlink($pngPreview);
  68. \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), [
  69. 'exception' => $e,
  70. 'app' => 'core',
  71. ]);
  72. return null;
  73. }
  74. $image = new \OCP\Image();
  75. $image->loadFromData((string) $png);
  76. $this->cleanTmpFiles();
  77. unlink($pngPreview);
  78. if ($image->valid()) {
  79. $image->scaleDownToFit($maxX, $maxY);
  80. return $image;
  81. }
  82. return null;
  83. }
  84. }