-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathConvert.php
148 lines (124 loc) · 4.85 KB
/
Convert.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* @copyright Copyright (c) 2018 Joas Schilling <[email protected]>
*
* @author Joas Schilling <[email protected]>
* @author Arthur Schiwon <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\WorkflowPDFConverter\BackgroundJobs;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\ILogger;
use OCP\ITempManager;
class Convert extends \OC\BackgroundJob\QueuedJob {
/** @var IConfig */
protected $config;
/** @var ITempManager */
protected $tempManager;
/** @var ILogger */
protected $logger;
/**
* BackgroundJob constructor.
*
* @param IConfig $config
* @param ITempManager $tempManager
* @param ILogger $logger
*/
public function __construct(IConfig $config, ITempManager $tempManager, ILogger $logger) {
$this->config = $config;
$this->tempManager = $tempManager;
$this->logger = $logger;
}
/**
* @param mixed $argument
*/
protected function run($argument) {
$command = $this->getCommand();
if ($command === null) {
$this->logger->error('Can not find office path. Please make sure to configure "preview_libreoffice_path" in your config file.');
}
$path = (string)$argument['path'];
$originalFileMode = (string)$argument['originalFileMode'];
$targetPdfMode = (string)$argument['targetPdfMode'];
$pathSegments = explode('/', $path, 4);
$dir = dirname($path);
$file = basename($path);
\OC\Files\Filesystem::init($pathSegments[1], '/' . $pathSegments[1] . '/files');
try {
$node = \OC::$server->getLazyRootFolder()->get($path);
} catch (NotFoundException $e) {
return;
}
$view = new \OC\Files\View($dir);
$tmpPath = $view->toTmpFile($file);
$tmpDir = $this->tempManager->getTempBaseDir();
$defaultParameters = ' -env:UserInstallation=file://' . escapeshellarg($tmpDir . '/nextcloud-' . $this->config->getSystemValue('instanceid') . '/') . ' --headless --nologo --nofirststartwizard --invisible --norestore --convert-to pdf --outdir ';
$clParameters = $this->config->getSystemValue('preview_office_cl_parameters', $defaultParameters);
// FIXME if passing an ogg, for instance, libreoffice would just run on infinitely, causing the background job to hang
// Thus, for one, we should blacklist some mimetypes (e.g. audio and video – DONE), but with application it is tricky
// since all the office things have their own custom mime types, as well as several applications
// pickin raisins is thus not really feasible
// so we should switch from exec to proc_open etc.
$exec = $command . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($tmpPath);
$exitCode = 0;
exec($exec, $out, $exitCode);
if ($exitCode !== 0) {
$this->logger->error("could not convert {file}, reason: {out}",
[
'app' => 'workflow_pdf_converter',
'file' => $node->getPath(),
'out' => $out
]
);
return;
}
$newTmpPath = pathinfo($tmpPath, PATHINFO_FILENAME) . '.pdf';
$newFileBaseName = pathinfo($file, PATHINFO_FILENAME);
$newFileName = $newFileBaseName . '.pdf';
$folder = $node->getParent();
$index = 0;
while ($targetPdfMode === 'preserve' && $folder->nodeExists($newFileName)) {
$index++;
$newFileName = $newFileBaseName . ' (' . $index . ').pdf';
}
$view->fromTmpFile($tmpDir . '/' . $newTmpPath, $newFileName);
if ($originalFileMode === 'delete') {
// FIXME: sometimes causes "unable to rename, destination directory is not writable" because the trashbin url
// looses the user part in \OC\Files\Storage\Local::moveFromStorage() line 460
// return $rootStorage->rename($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
// ^
$node->delete();
}
}
protected function getCommand() {
$libreOfficePath = $this->config->getSystemValue('preview_libreoffice_path', null);
if (is_string($libreOfficePath)) {
return escapeshellcmd($libreOfficePath);
}
$whichLibreOffice = shell_exec('command -v libreoffice');
if (!empty($whichLibreOffice)) {
return 'libreoffice';
}
$whichOpenOffice = shell_exec('command -v openoffice');
if (!empty($whichOpenOffice)) {
return 'openoffice';
}
return null;
}
}