Office.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 OCP\ITempManager;
  34. use OCP\Server;
  35. abstract class Office extends ProviderV2 {
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function isAvailable(FileInfo $file): bool {
  40. return is_string($this->options['officeBinary']);
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  46. if (!$this->isAvailable($file)) {
  47. return null;
  48. }
  49. $tempManager = Server::get(ITempManager::class);
  50. // The file to generate the preview for.
  51. $absPath = $this->getLocalFile($file);
  52. // The destination for the LibreOffice user profile.
  53. // LibreOffice can rune once per user profile and therefore instance id and file id are included.
  54. $profile = $tempManager->getTemporaryFolder(
  55. 'nextcloud-office-profile-' . \OC_Util::getInstanceId() . '-' . $file->getId()
  56. );
  57. // The destination for the LibreOffice convert result.
  58. $outdir = $tempManager->getTemporaryFolder(
  59. 'nextcloud-office-preview-' . \OC_Util::getInstanceId() . '-' . $file->getId()
  60. );
  61. if ($profile === false || $outdir === false) {
  62. $this->cleanTmpFiles();
  63. return null;
  64. }
  65. $parameters = [
  66. $this->options['officeBinary'],
  67. '-env:UserInstallation=file://' . escapeshellarg($profile),
  68. '--headless',
  69. '--nologo',
  70. '--nofirststartwizard',
  71. '--invisible',
  72. '--norestore',
  73. '--convert-to png',
  74. '--outdir ' . escapeshellarg($outdir),
  75. escapeshellarg($absPath),
  76. ];
  77. $cmd = implode(' ', $parameters);
  78. exec($cmd, $output, $returnCode);
  79. if ($returnCode !== 0) {
  80. $this->cleanTmpFiles();
  81. return null;
  82. }
  83. $preview = $outdir . pathinfo($absPath, PATHINFO_FILENAME) . '.png';
  84. $image = new \OCP\Image();
  85. $image->loadFromFile($preview);
  86. $this->cleanTmpFiles();
  87. if ($image->valid()) {
  88. $image->scaleDownToFit($maxX, $maxY);
  89. return $image;
  90. }
  91. return null;
  92. }
  93. }