diff options
author | Daniel Kesselberg <mail@danielkesselberg.de> | 2023-11-13 20:14:54 +0100 |
---|---|---|
committer | Daniel Kesselberg <mail@danielkesselberg.de> | 2023-11-13 21:46:31 +0100 |
commit | b5241d52204e5d88e2fba388041e02e0bd56aea7 (patch) | |
tree | 1addf0af03d78592a5e1c9181b56c15e86ba936a /lib/private | |
parent | aa48a5f94f2db398c639e95a89dc438203e89ca2 (diff) | |
download | nextcloud-server-b5241d52204e5d88e2fba388041e02e0bd56aea7.tar.gz nextcloud-server-b5241d52204e5d88e2fba388041e02e0bd56aea7.zip |
feat: allow multiple libreoffice invocations
LibreOffice only allows one invocation per user profile.[^1]
The office provider set the user profile to /tmp/owncloud-instanceid and therefore only one invocation per instance is allowed. This was introduced a while ago, yet it's unclear if this was intentionally or just a side effect.[^2]
The limitation on one invocation leads to the situation that the preview generation only works for a couple of files if you upload a whole folder of emf or word files.
This commit removes the limitation by using a new user profile for each preview. That's done by using instance id plus file id as postfix for getTemporaryFolder.
This has some drawbacks:
- Overload protection: If you upload 100 emf files, you may end up with 100 LibreOffice invocations. Though, you can use preview_concurrency_new to limit the number of previews that can be generated concurrently when php-sysvsem is available.
- New profile: I assume it takes a few bits to generate a fresh LibreOffice user profile. It appears that there is no way to ask LibreOffice to not create a profile and just work with the defaults. The profile will be cleaned after use by our temp manager.
- Remove the configuration option preview_office_cl_parameters: This is not strictly necessary yet, but if you set the configuration option, the generated path for the user profile is also missing. The configuration option is not well documented (e.g., it's unclear that the last option needs to be --outdir) and actually, there should be no reason to change it after all.
[^1]: https://wiki.documentfoundation.org/UserProfile
[^2]: https://github.com/owncloud/core/pull/9784
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
Diffstat (limited to 'lib/private')
-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); |