Office.php 3.0 KB

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