diff --git a/composer.json b/composer.json index 16ef742..ff8c78b 100644 --- a/composer.json +++ b/composer.json @@ -17,8 +17,9 @@ ], "require": { "php": "^8.3", + "illuminate/contracts": "^10.0||^11.0", "spatie/laravel-package-tools": "^1.16", - "illuminate/contracts": "^10.0||^11.0" + "spatie/temporary-directory": "^2.2" }, "require-dev": { "larastan/larastan": "^2.9", diff --git a/src/Markitdown.php b/src/Markitdown.php index 0548a9a..bcaf071 100755 --- a/src/Markitdown.php +++ b/src/Markitdown.php @@ -7,7 +7,9 @@ use Illuminate\Process\PendingProcess; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Process; +use Illuminate\Support\Str; use Innobrain\Markitdown\Exceptions\MarkitdownException; +use Spatie\TemporaryDirectory\TemporaryDirectory; class Markitdown { @@ -39,16 +41,20 @@ public function convert(string $filename): string public function convertString(string $content): string { - $processResult = $this->buildProcess() - ->command($this->executable) - ->input($content) - ->run(); + $temporaryDirectory = (new TemporaryDirectory('ib_markitdown')) + ->deleteWhenDestroyed() + ->create(); - if (! $processResult->successful()) { - throw MarkitdownException::processFailed($this->executable, $processResult->errorOutput()); - } + $tempPath = $temporaryDirectory + ->path(Str::random(40).'.tmp'); - return $processResult->output(); + try { + file_put_contents($tempPath, $content); + + return $this->convert($tempPath); + } finally { + $temporaryDirectory->delete(); + } } private function buildProcess(): PendingProcess