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.

111 lines
3.0 KiB

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