office.php 2.6 KB

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