id = $payload['data']['id']; $this->content = $payload['data']['content']; $this->fileResultType = $payload['data']['file_result_type']; $this->status = $payload['data']['status']; if (isset($payload['data']['document_format'])) { $this->documentFormat = $payload['data']['document_format']; } } /** * @throws \Exception|\GuzzleHttp\Exception\GuzzleException */ public function handle() { if ($this->fileResultType === 'md') { $this->handleDocumentMD(); return; } if ($this->fileResultType === 'document-recreated') { $this->downloadDocumentFromIngest(); return; } $this->handleDocumentJson(); } protected function handleDocumentMD() { $storage = Storage::disk('local'); $fileName = 'document'; // The .md extension signals the success status, the lack of signals the fail status. if ($this->status === 'success') { $fileName = $fileName . '.md'; } $dir = "contracts/$this->id"; try { $storage->makeDirectory($dir); $storage->put("$dir/$fileName", $this->content['document']); foreach ($this->content['images'] as $image) { $name = $image['name']; $type = $image['type']; $contents = $image['contents']; $contents = str_replace('data:image/' . $type . ';base64,', '', $contents); $storage->put("$dir/$name.$type", base64_decode($contents)); } // Emit event so other sections of the app can work on it. IngestDocumentReceived::dispatch($this->id); } catch (\Exception $exception) { \Illuminate\Support\Facades\Log::info('Exception: ' . $exception->getTraceAsString()); } } /** * @throws \Exception|\GuzzleHttp\Exception\GuzzleException */ protected function downloadDocumentFromIngest() { $storage = Storage::disk('local'); if ($this->status === 'fail') { $storage->deleteDirectory("contracts/$this->id"); return; } if ( ! $this->documentFormat) { throw new \Exception('No document format specified.'); } $filePath = "document.$this->documentFormat"; $storeDocumentAtPath = $storage->path('contracts/' . $this->id . '-' . $filePath); try { // Change the path where to store the document if the operation was run from CLI. $storeAtPathFromJsonFile = (new SearchAndDisplaceOriginalDocument())->getStoreAtPathFromJsonFile($this->id); if ($storeAtPathFromJsonFile) { $storeDocumentAtPath = $storeAtPathFromJsonFile . '/' . $this->id . '-displaced.' . $this->documentFormat; } } catch (\Exception $exception) { \Illuminate\Support\Facades\Log::info('Exception thrown when tried reading the storeDocumentAtPath from JSON file.'); } $client = new Client(); $url = env('SD_INGEST_URL') . '/recreate-document/' . $this->id . '?file_path=' . $filePath; try { $client->request('GET', $url, [ 'sink' => $storeDocumentAtPath, ]); } catch (ClientException $clientException) { $error = json_decode($clientException->getResponse()->getBody()->getContents(), true); throw new \Exception($error['message']); } finally { $storage->deleteDirectory("contracts/$this->id"); } SDAppliedOnOriginalDocument::dispatch($this->id); } /** * @throws \Exception|\GuzzleHttp\Exception\GuzzleException */ protected function handleDocumentJson() { $handler = new SearchAndDisplaceOriginalDocument(); if ($this->status !== 'success') { $handler->onIngestFail($this->id); return; } if ( ! $this->documentFormat) { throw new \Exception('No document format specified.'); } $handler->applySD($this->id, $this->content, $this->documentFormat); } }