From 478ca3ee1b947f2227ae8ec61b3f16fcb4a74a4d Mon Sep 17 00:00:00 2001 From: Orzu Ionut Date: Wed, 16 Jun 2021 12:54:28 +0300 Subject: [PATCH] PDFs with images --- .../Controllers/ContractImageController.php | 26 ++++++++++++++++ .../SearchAndDisplaceController.php | 2 +- app/SearchDisplace/Documents/DocumentFile.php | 30 ++++++++++--------- .../Ingest/HandleReceivedDocument.php | 18 +++++++++-- .../SearchAndDisplaceFromFiles.php | 10 +++---- routes/web.php | 2 ++ 6 files changed, 66 insertions(+), 22 deletions(-) create mode 100644 app/Http/Controllers/ContractImageController.php diff --git a/app/Http/Controllers/ContractImageController.php b/app/Http/Controllers/ContractImageController.php new file mode 100644 index 0000000..b5934e6 --- /dev/null +++ b/app/Http/Controllers/ContractImageController.php @@ -0,0 +1,26 @@ +header("Content-Type", $type); + + return $response; + } +} diff --git a/app/Http/Controllers/SearchAndDisplaceController.php b/app/Http/Controllers/SearchAndDisplaceController.php index 3300679..ee82b77 100644 --- a/app/Http/Controllers/SearchAndDisplaceController.php +++ b/app/Http/Controllers/SearchAndDisplaceController.php @@ -15,7 +15,7 @@ class SearchAndDisplaceController extends Controller $result = $handler->getAfterIngest($id); if ($result['status'] !== 'processing') { - $handler->destroy($id); +// $handler->destroy($id); } return response()->json($result, 200); diff --git a/app/SearchDisplace/Documents/DocumentFile.php b/app/SearchDisplace/Documents/DocumentFile.php index e79c54f..45fb1ec 100644 --- a/app/SearchDisplace/Documents/DocumentFile.php +++ b/app/SearchDisplace/Documents/DocumentFile.php @@ -18,15 +18,15 @@ class DocumentFile $path = $this->getPath($id); // Ingest success. - if ($this->storage->exists("$path.md")) { + if ($this->storage->exists("$path/document.md")) { return [ 'status' => 'success', - 'content' => $this->storage->get("$path.md"), + 'content' => $this->getDocumentContent($id, $path), ]; } // Ingest fail. - if ($this->storage->exists($path)) { + if ($this->storage->exists("$path/document")) { return [ 'status' => 'fail', ]; @@ -42,21 +42,23 @@ class DocumentFile { $path = $this->getPath($id); - // Ingest success. - if ($this->storage->exists("$path.md")) { - return $this->storage->delete("$path.md"); - } - - // Ingest fail. - if ($this->storage->exists($path)) { - return $this->storage->delete($path); - } - - return false; + return $this->storage->deleteDirectory($path); } protected function getPath($id) { return "contracts/$id"; } + + protected function getDocumentContent($id, $path) + { + $content = $this->storage->get("$path/document.md"); + + $imageFullPath = url('/') . '/contracts-images/' . $id . '/'; + $imageFullPath = str_replace( ' ', '%20', $imageFullPath); + + // @TODO Use preg_replace to find correctly formatted images and any wild cards for the image caption. +// return str_replace('![](./', '![](' . $imageFullPath, $content); + return str_replace('](./', '](' . $imageFullPath, $content); + } } diff --git a/app/SearchDisplace/Ingest/HandleReceivedDocument.php b/app/SearchDisplace/Ingest/HandleReceivedDocument.php index ec2bb08..147b1d5 100644 --- a/app/SearchDisplace/Ingest/HandleReceivedDocument.php +++ b/app/SearchDisplace/Ingest/HandleReceivedDocument.php @@ -22,15 +22,29 @@ class HandleReceivedDocument { $storage = Storage::disk('local'); - $fileName = $this->id; + $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->put("contracts/${fileName}", $this->content); + $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)); + } IngestDocumentReceived::dispatch($this->id); } catch (\Exception $exception) { diff --git a/app/SearchDisplace/SearchAndDisplaceFromFiles.php b/app/SearchDisplace/SearchAndDisplaceFromFiles.php index 382f966..6291f7e 100644 --- a/app/SearchDisplace/SearchAndDisplaceFromFiles.php +++ b/app/SearchDisplace/SearchAndDisplaceFromFiles.php @@ -8,7 +8,7 @@ class SearchAndDisplaceFromFiles { protected $id; protected $storage; - protected $documentFilePath; + protected $directoryPath; protected $infoFilePath; public function __construct($id) @@ -17,19 +17,19 @@ class SearchAndDisplaceFromFiles $this->storage = Storage::disk('local'); - $this->documentFilePath = "contracts/$this->id.md"; + $this->directoryPath = "contracts/$this->id"; $this->infoFilePath = "searchers/$this->id.json"; } public function execute() { - if ( ! $this->storage->exists($this->documentFilePath) || ! $this->storage->exists($this->infoFilePath)) { + if ( ! $this->storage->exists($this->directoryPath) || ! $this->storage->exists($this->infoFilePath)) { // Handle this case, must report result to user. return; } try { - $documentContent = $this->storage->get($this->documentFilePath); + $documentContent = $this->storage->get("$this->directoryPath/document.md"); $searchersContent = json_decode($this->storage->get($this->infoFilePath), true); $documentPath = $searchersContent['document_path']; @@ -54,7 +54,7 @@ class SearchAndDisplaceFromFiles return; } finally { - $this->storage->delete($this->documentFilePath); + $this->storage->deleteDirectory($this->directoryPath); $this->storage->delete($this->infoFilePath); } } diff --git a/routes/web.php b/routes/web.php index 4c7777e..076866a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -21,6 +21,8 @@ Route::get('/file/download/{path}', [ 'download' ])->where('path', '.*')->name('file.download'); +Route::get('/contracts-images/{id}/{image}', 'ContractImageController@show'); + Route::get('/search-and-displace/{id}', 'SearchAndDisplaceController@show'); Route::post('/search-and-displace', 'SearchAndDisplaceController@store');