Office.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 Thomas Müller <thomas.mueller@tmit.eu>
  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. abstract class Office extends Provider {
  28. private $cmd;
  29. /**
  30. * {@inheritDoc}
  31. */
  32. public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
  33. $this->initCmd();
  34. if (is_null($this->cmd)) {
  35. return false;
  36. }
  37. $absPath = $fileview->toTmpFile($path);
  38. $tmpDir = \OC::$server->getTempManager()->getTempBaseDir();
  39. $defaultParameters = ' -env:UserInstallation=file://' . escapeshellarg($tmpDir . '/owncloud-' . \OC_Util::getInstanceId() . '/') . ' --headless --nologo --nofirststartwizard --invisible --norestore --convert-to pdf --outdir ';
  40. $clParameters = \OCP\Config::getSystemValue('preview_office_cl_parameters', $defaultParameters);
  41. $exec = $this->cmd . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath);
  42. shell_exec($exec);
  43. //create imagick object from pdf
  44. $pdfPreview = null;
  45. try {
  46. list($dirname, , , $filename) = array_values(pathinfo($absPath));
  47. $pdfPreview = $dirname . '/' . $filename . '.pdf';
  48. $pdf = new \imagick($pdfPreview . '[0]');
  49. $pdf->setImageFormat('jpg');
  50. } catch (\Exception $e) {
  51. unlink($absPath);
  52. unlink($pdfPreview);
  53. \OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::ERROR);
  54. return false;
  55. }
  56. $image = new \OC_Image();
  57. $image->loadFromData($pdf);
  58. unlink($absPath);
  59. unlink($pdfPreview);
  60. if ($image->valid()) {
  61. $image->scaleDownToFit($maxX, $maxY);
  62. return $image;
  63. }
  64. return false;
  65. }
  66. private function initCmd() {
  67. $cmd = '';
  68. $libreOfficePath = \OC::$server->getConfig()->getSystemValue('preview_libreoffice_path', null);
  69. if (is_string($libreOfficePath)) {
  70. $cmd = $libreOfficePath;
  71. }
  72. $whichLibreOffice = shell_exec('command -v libreoffice');
  73. if ($cmd === '' && !empty($whichLibreOffice)) {
  74. $cmd = 'libreoffice';
  75. }
  76. $whichOpenOffice = shell_exec('command -v openoffice');
  77. if ($cmd === '' && !empty($whichOpenOffice)) {
  78. $cmd = 'openoffice';
  79. }
  80. if ($cmd === '') {
  81. $cmd = null;
  82. }
  83. $this->cmd = $cmd;
  84. }
  85. }