Repo for the search and displace core module including the interface to select files and search and displace operations to run on them. https://searchanddisplace.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

99 lines
2.9 KiB

<?php
namespace App\Http\Controllers;
use App\SearchDisplace\SearchAndDisplaceOriginalDocument;
use GuzzleHttp\Exception\BadResponseException;
class SearchAndDisplaceOriginalDocumentController extends Controller
{
public function store()
{
request()->validate([
'document' => [
'required',
'file',
'max:10000',
// 'mimes:doc,dot,docx,dotx,docm,dotm,odt,rtf,pdf,txt',
'mimes:doc,docx,odt,rtf,pdf,txt',
],
// Searchers is encoded.
// 'searchers' => 'required|array',
// 'searchers.*.key' => 'required',
// 'searchers.*.type' => 'required|in:replace,displace',
// 'searchers.*.value' => 'nullable',
]);
// Send document to Ingest to be processed as docx in order to get original data.
// After we get the response from Ingest apply S&D on the result.
// After that send back to Ingest to recreate the original document.
// After that Ingest will send webhook that it is done.
// Download file from Ingest.
// The interface will keep asking if the file is ready.
// Once is ready return the response with the file.
try {
$handler = new SearchAndDisplaceOriginalDocument();
$id = $handler->start(
request()->file('document'),
json_decode(request()->get('searchers'), true)
);
return response()->json([
'status' => 'ok',
'id' => $id,
]);
} catch (BadResponseException $e) {
return response()->json([
'message' => $e->getMessage(),
'response' => $e->getResponse()
], 400);
} catch (\Exception $e) {
return response()->json([
'message' => $e->getMessage(),
], 400);
}
}
public function show($id)
{
try {
$handler = new SearchAndDisplaceOriginalDocument();
if ($handler->hasFailed($id)) {
return response()->json([
'status' => 'fail',
], 200);
}
if ($handler->isInProgress($id)) {
return response()->json([
'status' => 'processing',
], 200);
}
return response()->json([
'status' => 'success',
], 200);
} catch (\Exception $exception) {
return response()->json([
'message' => $exception->getMessage(),
], 400);
}
}
public function download($id)
{
try {
$handler = new SearchAndDisplaceOriginalDocument();
return response()->download($handler->getDownloadPath($id))
->deleteFileAfterSend(true);
} catch (\Exception $exception) {
abort(404);
}
}
}