diff options
Diffstat (limited to 'lib/private/Preview/Office.php')
-rw-r--r-- | lib/private/Preview/Office.php | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/lib/private/Preview/Office.php b/lib/private/Preview/Office.php index 3ba7c5a21a0..bc0c554a192 100644 --- a/lib/private/Preview/Office.php +++ b/lib/private/Preview/Office.php @@ -31,6 +31,8 @@ namespace OC\Preview; use OCP\Files\File; use OCP\Files\FileInfo; use OCP\IImage; +use OCP\ITempManager; +use OCP\Server; use Psr\Log\LoggerInterface; abstract class Office extends ProviderV2 { @@ -49,15 +51,41 @@ abstract class Office extends ProviderV2 { return null; } + $tempManager = Server::get(ITempManager::class); + + // The file to generate the preview for. $absPath = $this->getLocalFile($file); - $tmpDir = \OC::$server->getTempManager()->getTempBaseDir(); + // The destination for the LibreOffice user profile. + // LibreOffice can rune once per user profile and therefore instance id and file id are included. + $profile = $tempManager->getTemporaryFolder( + 'nextcloud-office-profile-' . \OC_Util::getInstanceId() . '-' . $file->getId() + ); + + // The destination for the LibreOffice convert result. + $outdir = $tempManager->getTemporaryFolder( + 'nextcloud-office-preview-' . \OC_Util::getInstanceId() . '-' . $file->getId() + ); - $defaultParameters = ' -env:UserInstallation=file://' . escapeshellarg($tmpDir . '/owncloud-' . \OC_Util::getInstanceId() . '/') . ' --headless --nologo --nofirststartwizard --invisible --norestore --convert-to png --outdir '; - $clParameters = \OC::$server->getConfig()->getSystemValue('preview_office_cl_parameters', $defaultParameters); + if ($profile === false || $outdir === false) { + $this->cleanTmpFiles(); + return null; + } - $cmd = $this->options['officeBinary'] . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath); + $parameters = [ + $this->options['officeBinary'], + '-env:UserInstallation=file://' . escapeshellarg($profile), + '--headless', + '--nologo', + '--nofirststartwizard', + '--invisible', + '--norestore', + '--convert-to png', + '--outdir ' . escapeshellarg($outdir), + escapeshellarg($absPath), + ]; + $cmd = implode(' ', $parameters); exec($cmd, $output, $returnCode); if ($returnCode !== 0) { @@ -65,17 +93,13 @@ abstract class Office extends ProviderV2 { return null; } - //create imagick object from png - $pngPreview = null; try { - [$dirname, , , $filename] = array_values(pathinfo($absPath)); - $pngPreview = $tmpDir . '/' . $filename . '.png'; + $filename = $outdir . pathinfo($absPath, PATHINFO_FILENAME) . '.png'; - $png = new \Imagick($pngPreview . '[0]'); + $png = new \Imagick($filename . '[0]'); $png->setImageFormat('jpg'); } catch (\Exception $e) { $this->cleanTmpFiles(); - unlink($pngPreview); \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), [ 'exception' => $e, 'app' => 'core', @@ -87,7 +111,6 @@ abstract class Office extends ProviderV2 { $image->loadFromData((string) $png); $this->cleanTmpFiles(); - unlink($pngPreview); if ($image->valid()) { $image->scaleDownToFit($maxX, $maxY); |