Browse Source

PDFs with images

master
Orzu Ionut 3 years ago
parent
commit
478ca3ee1b
  1. 26
      app/Http/Controllers/ContractImageController.php
  2. 2
      app/Http/Controllers/SearchAndDisplaceController.php
  3. 30
      app/SearchDisplace/Documents/DocumentFile.php
  4. 18
      app/SearchDisplace/Ingest/HandleReceivedDocument.php
  5. 10
      app/SearchDisplace/SearchAndDisplaceFromFiles.php
  6. 2
      routes/web.php

26
app/Http/Controllers/ContractImageController.php

@ -0,0 +1,26 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Response;
class ContractImageController extends Controller
{
public function show($id, $image)
{
$path = storage_path('app/contracts/' . $id . '/' . $image);
if ( ! File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
}
}

2
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);

30
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);
}
}

18
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) {

10
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);
}
}

2
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');

Loading…
Cancel
Save