|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Breadlesscode\Office; |
| 4 | + |
| 5 | +use Spatie\TemporaryDirectory\TemporaryDirectory; |
| 6 | +use Symfony\Component\Process\Exception\ProcessFailedException; |
| 7 | +use Symfony\Component\Process\Process; |
| 8 | +use Breadlesscode\Office\Exception\ConverterException; |
| 9 | + |
| 10 | +class Converter |
| 11 | +{ |
| 12 | + public static $converters = [ |
| 13 | + \Breadlesscode\Office\Programs\WriterProgram::class, |
| 14 | + \Breadlesscode\Office\Programs\CalcProgram::class, |
| 15 | + \Breadlesscode\Office\Programs\ImpressProgram::class, |
| 16 | + \Breadlesscode\Office\Programs\DrawProgram::class |
| 17 | + ]; |
| 18 | + |
| 19 | + protected $paramters = ['--headless']; |
| 20 | + protected $fileInfo = null; |
| 21 | + protected $file = null; |
| 22 | + protected $converter = null; |
| 23 | + protected $binaryPath = 'libreoffice'; |
| 24 | + protected $temporaryPath = 'temp'; |
| 25 | + protected $timeout = 2000; |
| 26 | + |
| 27 | + public function __construct(string $file, string $fileType = null) |
| 28 | + { |
| 29 | + if (!file_exists($file)) { |
| 30 | + throw new ConverterException("File ".$file." not found!", 1); |
| 31 | + } |
| 32 | + |
| 33 | + $fileExtension = $fileType ?: pathinfo(realpath($file), PATHINFO_EXTENSION); |
| 34 | + |
| 35 | + foreach (self::$converters as $converter) { |
| 36 | + if (!$converter::canHandleExtension($fileExtension)) { |
| 37 | + continue; |
| 38 | + } |
| 39 | + |
| 40 | + $this->fileInfo = pathinfo($file); |
| 41 | + $this->file = $file; |
| 42 | + $this->converter = $converter; |
| 43 | + break; |
| 44 | + } |
| 45 | + |
| 46 | + if ($this->file === null) { |
| 47 | + throw new ConverterException('Can not handle file type '.$fileExtension); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public static function canHandleExtension(string $fileExtension): bool |
| 52 | + { |
| 53 | + foreach (self::$converters as $converter) { |
| 54 | + if ($converter::canHandleExtension($fileExtension)) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return false; |
| 60 | + } |
| 61 | + public static function file(string $file, string $fileType = null) |
| 62 | + { |
| 63 | + return new static($file, $fileType); |
| 64 | + } |
| 65 | + |
| 66 | + public function setLibreofficeBinaryPath(string $path) |
| 67 | + { |
| 68 | + $this->binaryPath = $path; |
| 69 | + |
| 70 | + return $this; |
| 71 | + } |
| 72 | + |
| 73 | + public function setTemporaryPath(string $path) |
| 74 | + { |
| 75 | + $this->temporaryPath = $path; |
| 76 | + |
| 77 | + return $this; |
| 78 | + } |
| 79 | + |
| 80 | + public function setTimeout(int $timeout) |
| 81 | + { |
| 82 | + $this->timeout = $timeout; |
| 83 | + |
| 84 | + return $this; |
| 85 | + } |
| 86 | + |
| 87 | + public function thumbnail(string $extension = 'jpg') |
| 88 | + { |
| 89 | + $this->save($this->fileInfo['dirname'].DIRECTORY_SEPARATOR.$this->getNewFilename($extension)); |
| 90 | + } |
| 91 | + |
| 92 | + public function text() |
| 93 | + { |
| 94 | + return trim($this->content('txt')); |
| 95 | + } |
| 96 | + |
| 97 | + public function save(string $path, string $extension = null): bool |
| 98 | + { |
| 99 | + $pathInfo = pathinfo($path); |
| 100 | + $rename = is_null($extension); |
| 101 | + |
| 102 | + if (is_null($extension)) { |
| 103 | + $extension = $pathInfo['extension'] ? $pathInfo['extension'] : $extension; |
| 104 | + } |
| 105 | + |
| 106 | + if (!$this->isConvertable($extension)) { |
| 107 | + throw new ConverterException("Invalid conversion. Can not convert ".$this->fileInfo['extension']." to ".$extension, 1); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + $this->setFilter($extension); |
| 112 | + $this->setOutputDir($rename ? $pathInfo['dirname'] : $path); |
| 113 | + $this->callLibreofficeBinary(); |
| 114 | + |
| 115 | + if ($rename) { |
| 116 | + // rename to new name |
| 117 | + $oldName = $pathInfo['dirname'].DIRECTORY_SEPARATOR.$this->getNewFilename($extension); |
| 118 | + $newName = $pathInfo['dirname'].DIRECTORY_SEPARATOR.$pathInfo['basename']; |
| 119 | + rename($oldName, $newName); |
| 120 | + } |
| 121 | + |
| 122 | + return true; |
| 123 | + } |
| 124 | + |
| 125 | + public function content(string $extension = null): string |
| 126 | + { |
| 127 | + if (!$this->isConvertable($extension)) { |
| 128 | + throw new ConverterException("Invalid conversion. Can not convert ".$this->fileInfo['extension']." to ".$extension, 1); |
| 129 | + } |
| 130 | + |
| 131 | + $tempDir = (new TemporaryDirectory(__DIR__)) |
| 132 | + ->name('temp') |
| 133 | + ->force() |
| 134 | + ->create() |
| 135 | + ->empty(); |
| 136 | + |
| 137 | + $this->setFilter($extension); |
| 138 | + $this->setOutputDir($tempDir->path()); |
| 139 | + $this->callLibreofficeBinary(); |
| 140 | + |
| 141 | + $content = file_get_contents($tempDir->path($this->getNewFilename($extension))); |
| 142 | + $tempDir->delete(); |
| 143 | + |
| 144 | + return $content; |
| 145 | + } |
| 146 | + |
| 147 | + protected function getNewFilename(string $extension) |
| 148 | + { |
| 149 | + $path = $this->file; |
| 150 | + |
| 151 | + return str_replace($this->fileInfo['extension'], $extension, $this->fileInfo['basename']); |
| 152 | + } |
| 153 | + |
| 154 | + protected function setOutputDir(string $dir) |
| 155 | + { |
| 156 | + $this->paramters[] = '--outdir '.$this->escapeArgument($dir); |
| 157 | + } |
| 158 | + |
| 159 | + protected function setFilter(string $extension, string $filter = '') |
| 160 | + { |
| 161 | + $arg = empty($filter) ? $extension : $extension.':'.$filter; |
| 162 | + |
| 163 | + $this->paramters[] = '--convert-to '.$this->escapeArgument($arg); |
| 164 | + } |
| 165 | + |
| 166 | + protected function escapeArgument(string $arg): string |
| 167 | + { |
| 168 | + return $arg; |
| 169 | + } |
| 170 | + |
| 171 | + protected function callLibreofficeBinary(): string |
| 172 | + { |
| 173 | + // add file to convert |
| 174 | + $filePath = $this->file; |
| 175 | + $this->paramters[] = $filePath; |
| 176 | + // glue parameters |
| 177 | + $cliStr = $this->binaryPath; |
| 178 | + $cliStr.= ' '.implode(' ', $this->paramters); |
| 179 | + // start convert process |
| 180 | + $process = (new Process($cliStr))->setTimeout($this->timeout); |
| 181 | + $process->run(); |
| 182 | + |
| 183 | + if (! $process->isSuccessful()) { |
| 184 | + throw new ProcessFailedException($process); |
| 185 | + } |
| 186 | + |
| 187 | + return $process->getOutput(); |
| 188 | + } |
| 189 | + |
| 190 | + protected function isConvertable(string $extension): bool |
| 191 | + { |
| 192 | + if (is_null($extension)) { |
| 193 | + throw new ConverterException('No extension is set.'); |
| 194 | + } |
| 195 | + |
| 196 | + return $this->converter::canConvertTo($extension); |
| 197 | + } |
| 198 | +} |
0 commit comments