argument('path'); $searchers = $this->argument('filters'); $id = md5(uniqid(rand(), true)); $pathDetails = pathinfo($documentPath); $resultedDocumentPath = $pathDetails['dirname'] . '/' . $pathDetails['filename'] . '-displaced.md'; try { $this->storeSearchers($id, $searchers, $resultedDocumentPath); $sendToIngest = new SendDocument(); $sendToIngest->execute($id, [ 'path' => $documentPath, 'name' => $pathDetails['basename'], 'type' => $this->getFileMimeType($documentPath), ]); $this->info('Processing document..'); $this->info('After the processing will be done the result will show up at the same path as the input.'); } catch (\Exception $exception) { $this->error('Something went wrong. (' . $exception->getMessage() . ')'); } } protected function storeSearchers($id, $searchers, $storeResultPath) { $data = [ 'searchers' => $this->getSearchers($searchers), 'document_path' => $storeResultPath, ]; $storage = Storage::disk('local'); $storage->put("searchers/$id.json", json_encode($data)); } protected function getSearchers($searchers) { if (count($searchers) === 1 && str_contains($searchers[0], '.json')) { return $this->getSearchersFromFile($searchers[0]); } return $this->getSearchersFromList($searchers); } protected function getSearchersFromList($searchers) { $storage = Storage::disk('local'); $list = []; foreach ($searchers as $searcher) { $result = explode(':', $searcher); $searcherPath = 'searchers/' . $result[0] . '.json'; if ( ! $storage->exists($searcherPath)) { throw new \Exception('Searcher does not exist'); } $list[] = [ 'content' => json_decode($storage->get($searcherPath), true), 'replace_with' => count($result) > 1 ? $result[1] : '', ]; } return $list; } protected function getSearchersFromFile($argument) { $result = explode(':', $argument); if (count($result) > 1) { $path = $result[0]; $replaceWith = $result[1]; } else { $path = $argument; $replaceWith = ''; } $contents = file_get_contents($path); if ( ! $contents) { throw new \Exception('There is no data in the searcher JSON file.'); } return [ [ 'content' => json_decode($contents), 'replace_with' => $replaceWith, ], ]; } protected function getFileMimeType($file) { if (function_exists('finfo_file')) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $type = finfo_file($finfo, $file); finfo_close($finfo); } else { require_once 'upgradephp/ext/mime.php'; $type = mime_content_type($file); } if (!$type || in_array($type, array('application/octet-stream', 'text/plain'))) { $secondOpinion = exec('file -b --mime-type ' . escapeshellarg($file), $foo, $returnCode); if ($returnCode === 0 && $secondOpinion) { $type = $secondOpinion; } } if (!$type || in_array($type, array('application/octet-stream', 'text/plain'))) { require_once 'upgradephp/ext/mime.php'; $exifImageType = exif_imagetype($file); if ($exifImageType !== false) { $type = image_type_to_mime_type($exifImageType); } } return $type; } }