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.

94 lines
2.8 KiB

3 years ago
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\SearchDisplace\SearchAndDisplaceOriginalDocument;
  4. use GuzzleHttp\Exception\BadResponseException;
  5. class SearchAndDisplaceOriginalDocumentController extends Controller
  6. {
  7. public function store()
  8. {
  9. request()->validate([
  10. 'document' => [
  11. 'required',
  12. 'file',
  13. 'max:10000',
  14. 'mimes:doc,dot,docx,dotx,docm,dotm,odt,rtf,pdf,txt',
  15. ],
  16. // Searchers is encoded.
  17. // 'searchers' => 'required|array',
  18. // 'searchers.*.key' => 'required',
  19. // 'searchers.*.type' => 'required|in:replace,displace',
  20. // 'searchers.*.value' => 'nullable',
  21. ]);
  22. // Send document to Ingest to be processed as docx in order to get original data.
  23. // After we get the response from Ingest apply S&D on the result.
  24. // After that send back to Ingest to recreate the original document.
  25. // After that Ingest will send webhook that it is done.
  26. // Download file from Ingest.
  27. // The interface will keep asking if the file is ready.
  28. // Once is ready return the response with the file.
  29. try {
  30. $handler = new SearchAndDisplaceOriginalDocument();
  31. $id = $handler->start(request()->file('document'), json_decode(request()->get('searchers'), true));
  32. return response()->json([
  33. 'status' => 'ok',
  34. 'id' => $id,
  35. ]);
  36. } catch (BadResponseException $e) {
  37. return response()->json([
  38. 'message' => $e->getMessage(),
  39. 'response' => $e->getResponse()
  40. ], 400);
  41. } catch (\Exception $e) {
  42. return response()->json([
  43. 'message' => $e->getMessage(),
  44. ], 400);
  45. }
  46. }
  47. public function show($id)
  48. {
  49. try {
  50. $handler = new SearchAndDisplaceOriginalDocument();
  51. if ($handler->hasFailed($id)) {
  52. return response()->json([
  53. 'status' => 'fail',
  54. ], 200);
  55. }
  56. if ($handler->isInProgress($id)) {
  57. return response()->json([
  58. 'status' => 'processing',
  59. ], 200);
  60. }
  61. return response()->json([
  62. 'status' => 'success',
  63. ], 200);
  64. } catch (\Exception $exception) {
  65. return response()->json([
  66. 'message' => $exception->getMessage(),
  67. ], 400);
  68. }
  69. }
  70. public function download($id)
  71. {
  72. try {
  73. $handler = new SearchAndDisplaceOriginalDocument();
  74. return $handler->streamFile($id);
  75. } catch (\Exception $exception) {
  76. abort(404);
  77. }
  78. }
  79. }