You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.1 KiB

3 years ago
  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\IImage;
  32. use OCP\ILogger;
  33. abstract class Office extends ProviderV2 {
  34. private $cmd;
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  39. $this->initCmd();
  40. if (is_null($this->cmd)) {
  41. return null;
  42. }
  43. $absPath = $this->getLocalFile($file);
  44. $tmpDir = \OC::$server->getTempManager()->getTempBaseDir();
  45. $defaultParameters = ' -env:UserInstallation=file://' . escapeshellarg($tmpDir . '/owncloud-' . \OC_Util::getInstanceId() . '/') . ' --headless --nologo --nofirststartwizard --invisible --norestore --convert-to png --outdir ';
  46. $clParameters = \OC::$server->getConfig()->getSystemValue('preview_office_cl_parameters', $defaultParameters);
  47. $exec = $this->cmd . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath);
  48. shell_exec($exec);
  49. //create imagick object from png
  50. $pngPreview = null;
  51. try {
  52. list($dirname, , , $filename) = array_values(pathinfo($absPath));
  53. $pngPreview = $tmpDir . '/' . $filename . '.png';
  54. $png = new \imagick($pngPreview . '[0]');
  55. $png->setImageFormat('jpg');
  56. } catch (\Exception $e) {
  57. $this->cleanTmpFiles();
  58. unlink($pngPreview);
  59. \OC::$server->getLogger()->logException($e, [
  60. 'level' => ILogger::ERROR,
  61. 'app' => 'core',
  62. ]);
  63. return null;
  64. }
  65. $image = new \OC_Image();
  66. $image->loadFromData($png);
  67. $this->cleanTmpFiles();
  68. unlink($pngPreview);
  69. if ($image->valid()) {
  70. $image->scaleDownToFit($maxX, $maxY);
  71. return $image;
  72. }
  73. return null;
  74. }
  75. private function initCmd() {
  76. $cmd = '';
  77. $libreOfficePath = \OC::$server->getConfig()->getSystemValue('preview_libreoffice_path', null);
  78. if (is_string($libreOfficePath)) {
  79. $cmd = $libreOfficePath;
  80. }
  81. $whichLibreOffice = shell_exec('command -v libreoffice');
  82. if ($cmd === '' && !empty($whichLibreOffice)) {
  83. $cmd = 'libreoffice';
  84. }
  85. $whichOpenOffice = shell_exec('command -v openoffice');
  86. if ($cmd === '' && !empty($whichOpenOffice)) {
  87. $cmd = 'openoffice';
  88. }
  89. if ($cmd === '') {
  90. $cmd = null;
  91. }
  92. $this->cmd = $cmd;
  93. }
  94. }