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.

112 lines
3.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\SearchDisplace\Ingest\SendDocument;
  4. use Illuminate\Http\JsonResponse;
  5. use Illuminate\Support\Facades\Storage;
  6. use Illuminate\Http\UploadedFile;
  7. use GuzzleHttp\Exception\BadResponseException;
  8. use App\SearchDisplace\Convertor\Convertor;
  9. use App\SearchDisplace\SearchAndDisplaceXML;
  10. class FileController extends Controller
  11. {
  12. public function __construct()
  13. {
  14. $this->storage = Storage::disk('local');
  15. }
  16. /**
  17. *
  18. * @return JsonResponse
  19. */
  20. public function create(): JsonResponse
  21. {
  22. request()->validate([
  23. 'file' => [
  24. 'required',
  25. 'file',
  26. 'max:10000',
  27. 'mimes:doc,dot,docx,dotx,docm,dotm,odt,rtf,pdf,txt',
  28. ],
  29. ]);
  30. try {
  31. /** @var UploadedFile $file */
  32. $file = request()->file('file');
  33. $time = time();
  34. $fileId = $time . '_' . pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
  35. $sendDocument = new SendDocument();
  36. $sendDocument->execute($fileId, [
  37. 'path' => $file->getRealPath(),
  38. 'type' => $file->getMimeType(),
  39. 'name' => $file->getClientOriginalName()
  40. ]);
  41. $originalFile = $fileId . "/document." . $file->extension();
  42. $this->storage->put("contracts/{$originalFile}", file_get_contents($file)); // keep the original file
  43. $xml = Convertor::convert('xml', $this->storage->path("contracts/" . $originalFile));
  44. $this->storage->delete("contracts/{$originalFile}");
  45. return response()->json([
  46. 'id' => $fileId,
  47. 'file_name' => $file->getClientOriginalName(),
  48. ]);
  49. } catch (BadResponseException $e) {
  50. return response()->json([
  51. 'message' => $e->getMessage(),
  52. 'response' => $e->getResponse()
  53. ], 400);
  54. } catch (\Exception $e) {
  55. return response()->json([
  56. 'message' => $e->getMessage(),
  57. ], 400);
  58. }
  59. }
  60. /**
  61. *
  62. */
  63. public function convert(): JsonResponse
  64. {
  65. $file = (object) request()->input('file');
  66. $xml = $this->storage->path("contracts/{$file->id}/document_sdapplied.xml");
  67. $original = SearchAndDisplaceXML::prepareForDownload($file->type, $xml);
  68. return response()->json([
  69. 'path' => "tmp/$original"
  70. ]);
  71. }
  72. /**
  73. *
  74. * @param string $path
  75. * @return mixed
  76. */
  77. public function download(string $path)
  78. {
  79. return $this->storage->download($path);
  80. }
  81. /**
  82. * Clear contracts and tmp directory
  83. *
  84. * @param string $id
  85. * @return JsonResponse
  86. */
  87. public function delete(string $id): JsonResponse
  88. {
  89. $this->storage->deleteDirectory("contracts/${id}");
  90. $tmpFiles = $this->storage->allFiles("tmp");
  91. $success = $this->storage->delete($tmpFiles);
  92. return response()->json(['success' => $success]);
  93. }
  94. }