Skip to content

Commit

Permalink
Merge pull request #20 from cidilabs/scottcooper/CLSP-19-processes-no…
Browse files Browse the repository at this point in the history
…t-being-killed

make changes so that soffice will launch multiple instances
  • Loading branch information
cidilabs authored Feb 10, 2023
2 parents 3b4ec25 + 1d35d5f commit ea670dc
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/PhpLibre.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ class PhpLibre
'errors' => []
];

private $sOfficePort = null;
private $tempDir;

public function __construct($bin = 'soffice', $outputDir = 'alternates')
{
$this->bin = $bin;
$this->outputDir = $outputDir;
$this->tempDir = sys_get_temp_dir();
}

public function supports()
Expand Down Expand Up @@ -129,7 +133,7 @@ public function convertToHtml($filepath)
$shell = $this->exec($this->makeCommand($extension, 'html', $filepath));

if (0 != $shell['return']) {
throw new \Exception("Conversion Failure! Contact your institution's UDOIT admin. Error: " . $shell['return']);
throw new \Exception("Conversion Failure! Contact your institution's UDOIT admin. Error: " . $shell['return'] . ": output :" . $shell['stdout']. ": error :" . $shell['stderr']);
}

$htmlFilepath = str_replace($extension, 'html', $filepath);
Expand Down Expand Up @@ -194,7 +198,14 @@ protected function makeCommand($inputExtension, $outputExtension, $filename)
$infilterArg = !empty($this->infilterOptions[$inputExtension]) ? $this->infilterOptions[$inputExtension] : '';
$infilter = "--infilter=\"" . $infilterArg . "\" ";

return "{$this->bin} --headless " . $infilter . "--convert-to \"{$outputExtension}\" {$oriFile} --outdir {$dirname}";
while (is_null($this->sOfficePort)){
$tmpPort = rand(8100,8999);
if (!is_dir("{$this->tempDir}/SOffice_Process{$this->sOfficePort}")) {
$this->sOfficePort = $tmpPort;
}
}

return "{$this->bin} --headless --headless --norestore --nolockcheck -env:SingleAppInstance=\"false\" -env:UserInstallation=\"file:///{$this->tempDir}/SOffice_Process{$this->sOfficePort}\" --accept=\"socket,host=localhost,port={$this->sOfficePort};urp;\" " . $infilter . "--convert-to \"{$outputExtension}\" {$oriFile} --outdir {$dirname}";
}

protected function open($filename)
Expand Down Expand Up @@ -302,10 +313,35 @@ private function exec($cmd, $input = '')
fclose($pipes[2]);
$rtn = proc_close($process);

$this->deleteDirectory("{$this->tempDir}/SOffice_Process{$this->sOfficePort}");

return [
'stdout' => $stdout,
'stderr' => $stderr,
'return' => $rtn,
];
}

private function deleteDirectory($dir) {
if (!file_exists($dir)) {
return true;
}

if (!is_dir($dir)) {
return unlink($dir);
}

foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') {
continue;
}

if (!$this->deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
return false;
}

}

return rmdir($dir);
}
}

0 comments on commit ea670dc

Please sign in to comment.