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.

110 lines
2.8 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
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. try {
  19. /** @var UploadedFile $file */
  20. $file = request()->file('file');
  21. $time = time();
  22. $fileId = $time . '_' . pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
  23. $sendDocument = new SendDocument();
  24. $sendDocument->execute($fileId, [
  25. 'path' => $file->getRealPath(),
  26. 'type' => $file->getMimeType(),
  27. 'name' => $file->getClientOriginalName()
  28. ]);
  29. return response()->json([
  30. 'id' => $fileId,
  31. 'file_name' => $file->getClientOriginalName(),
  32. ]);
  33. } catch (BadResponseException $e) {
  34. return response()->json([
  35. 'message' => $e->getMessage(),
  36. 'response' => $e->getResponse()
  37. ], 400);
  38. } catch (\Exception $e) {
  39. return response()->json([
  40. 'message' => $e->getMessage(),
  41. ], 400);
  42. }
  43. }
  44. /**
  45. *
  46. */
  47. public function convert(): JsonResponse
  48. {
  49. $mdFileContent = request()->input('content');
  50. $fileId = request()->input('file_id');
  51. Storage::disk('local')->put('tmp/' . $fileId . '.md', $mdFileContent);
  52. $tmpMdFilePath = Storage::path('tmp/' . $fileId . '.md');
  53. $pandoc = new Process([
  54. 'pandoc',
  55. '-f',
  56. 'markdown',
  57. '-t',
  58. 'odt',
  59. $tmpMdFilePath
  60. ]);
  61. $pandoc->start();
  62. while ($pandoc->isRunning()) {
  63. //
  64. }
  65. $odtFileContent = $pandoc->getOutput();
  66. Storage::disk('local')->put('tmp/' . $fileId . '.odt', $odtFileContent);
  67. return response()->json([
  68. 'path' => 'tmp/' . $fileId . '.odt'
  69. ]);
  70. }
  71. /**
  72. *
  73. * @param string $path
  74. * @return mixed
  75. */
  76. public function download(string $path)
  77. {
  78. return Storage::download($path);
  79. }
  80. /**
  81. * Delete a file currently in progress
  82. *
  83. * @param string $id
  84. * @return JsonResponse
  85. */
  86. public function delete(string $id): JsonResponse
  87. {
  88. $success = Storage::delete([
  89. "tmp/{$id}.md",
  90. "tmp/{$id}.odt",
  91. "contracts/{$id}.md",
  92. ]);
  93. return response()->json(['success' => $success]);
  94. }
  95. }