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.

119 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
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 Illuminate\Support\Str;
  9. use Symfony\Component\Process\Process;
  10. class FileController extends Controller
  11. {
  12. /**
  13. *
  14. * @return JsonResponse
  15. */
  16. public function create(): JsonResponse
  17. {
  18. request()->validate([
  19. 'file' => [
  20. 'required',
  21. 'file',
  22. 'max:10000',
  23. 'mimes:doc,dot,docx,dotx,docm,dotm,odt,rtf,pdf,txt',
  24. ],
  25. ]);
  26. try {
  27. /** @var UploadedFile $file */
  28. $file = request()->file('file');
  29. $time = time();
  30. $fileId = $time . '_' . pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
  31. $sendDocument = new SendDocument();
  32. $sendDocument->execute($fileId, [
  33. 'path' => $file->getRealPath(),
  34. 'type' => $file->getMimeType(),
  35. 'name' => $file->getClientOriginalName()
  36. ]);
  37. return response()->json([
  38. 'id' => $fileId,
  39. 'file_name' => $file->getClientOriginalName(),
  40. ]);
  41. } catch (BadResponseException $e) {
  42. return response()->json([
  43. 'message' => $e->getMessage(),
  44. 'response' => $e->getResponse()
  45. ], 400);
  46. } catch (\Exception $e) {
  47. return response()->json([
  48. 'message' => $e->getMessage(),
  49. ], 400);
  50. }
  51. }
  52. /**
  53. *
  54. */
  55. public function convert(): JsonResponse
  56. {
  57. $mdFileContent = request()->input('content');
  58. $fileId = request()->input('file_id');
  59. Storage::disk('local')->put('tmp/' . $fileId . '.md', $mdFileContent);
  60. $tmpMdFilePath = Storage::path('tmp/' . $fileId . '.md');
  61. $pandoc = new Process([
  62. 'pandoc',
  63. '-f',
  64. 'markdown',
  65. '-t',
  66. 'odt',
  67. $tmpMdFilePath
  68. ]);
  69. $pandoc->start();
  70. while ($pandoc->isRunning()) {
  71. //
  72. }
  73. $odtFileContent = $pandoc->getOutput();
  74. Storage::disk('local')->put('tmp/' . $fileId . '.odt', $odtFileContent);
  75. return response()->json([
  76. 'path' => 'tmp/' . $fileId . '.odt'
  77. ]);
  78. }
  79. /**
  80. *
  81. * @param string $path
  82. * @return mixed
  83. */
  84. public function download(string $path)
  85. {
  86. return Storage::download($path);
  87. }
  88. /**
  89. * Delete a file currently in progress
  90. *
  91. * @param string $id
  92. * @return JsonResponse
  93. */
  94. public function delete(string $id): JsonResponse
  95. {
  96. $success = Storage::delete([
  97. "tmp/{$id}.md",
  98. "tmp/{$id}.odt",
  99. "contracts/{$id}.md",
  100. ]);
  101. return response()->json(['success' => $success]);
  102. }
  103. }