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

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