path = $path; $this->type = $type; $this->storage = Storage::disk('local'); $this->parserDocx = new ParseDocx(); $this->parserXml = new ParseXml(); $this->parserHtml = new ParseHtml(); $this->parseHtmlArray = new ParseHtmlArray(); } /** * Execute the job. * * @return void */ public function handle() { $convertor = new Convertor($this->path, $this->type); try { $this->path = $convertor->execute(); } catch (\Exception $exception) { $this->failed(); } // @TODO Replace later, the convertor will create the .md file. if ($this->type !== 'pdf') { $content = $this->getContent(); if ( ! $content) { $this->failed(); return; } $content = $this->convertToUTF8($content); try { $filePath = $this->storeContent($content); } catch (\Exception $e) { Log::error('Error writing in to the file: ' . $e->getMessage()); // report($e); } } else { $filePath = $this->path; } SendToCore::dispatch($filePath); } public function failed() { if ( ! $this->storage) { $this->storage = Storage::disk('local'); } Log::error('Ingest documents failed.'); // // @TODO Delete docx, txt and md files. // if ($this->storage->exists($this->path)) { // $this->storage->delete($this->path); // } SendToCore::dispatch($this->path, true); } protected function getContent() { if ($this->type === 'pdf') { // Wait while it finishes. while (!$this->storage->exists($this->path)) { sleep(1); } $textParser = new ParseTextArray(true); return $textParser->fromFile($this->storage->path($this->path)); } $textParser = new ParseTextArray(); return $textParser->fromFile($this->storage->path($this->path)); } protected function convertToUTF8($content) { array_walk_recursive( $content, function (&$entry) { $entry = mb_convert_encoding( $entry, 'UTF-8' ); } ); return $content; } protected function storeContent($content) { $result = explode('.', $this->path); $name = $result[0]; // Or json? $filePath = $this->storeMD($name, $content); // Delete converted file. We now have the .md file. $this->storage->delete($this->path); return $filePath; } protected function storeMD($name, $content) { $fileName = "$name.md"; $convertor = new MDConvertor($content); $this->storage->put($fileName, $convertor->execute()); return $fileName; } protected function storeJson($name, $content) { $fileName = "$name.json"; $this->storage->put($fileName, $content); return $fileName; } }