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.

90 lines
2.2 KiB

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($file->getRealPath(), $fileId);
  25. return response()->json([
  26. 'id' => $fileId,
  27. 'file_name' => $file->getClientOriginalName(),
  28. ]);
  29. } catch (BadResponseException $e) {
  30. return response()->json([
  31. 'message' => $e->getMessage(),
  32. 'response' => $e->getResponse()
  33. ], 400);
  34. } catch (\Exception $e) {
  35. return response()->json([
  36. 'message' => $e->getMessage(),
  37. ], 400);
  38. }
  39. }
  40. /**
  41. *
  42. */
  43. public function convert(): JsonResponse
  44. {
  45. $mdFileContent = request()->input('content');
  46. $tmpFileId = (string) Str::uuid();
  47. Storage::disk('local')->put('tmp/' . $tmpFileId . '.md', $mdFileContent);
  48. $tmpMdFilePath = Storage::path('tmp/' . $tmpFileId . '.md');
  49. $pandoc = new Process([
  50. 'pandoc',
  51. '-f',
  52. 'markdown',
  53. '-t',
  54. 'odt',
  55. $tmpMdFilePath
  56. ]);
  57. $pandoc->start();
  58. while ($pandoc->isRunning()) {
  59. //
  60. }
  61. $odtFileContent = $pandoc->getOutput();
  62. Storage::disk('local')->put('tmp/' . $tmpFileId . '.odt', $odtFileContent);
  63. return response()->json([
  64. 'path' => 'tmp/' . $tmpFileId . '.odt'
  65. ]);
  66. }
  67. /**
  68. *
  69. * @param string $path
  70. * @return mixed
  71. */
  72. public function download(string $path)
  73. {
  74. return Storage::download($path);
  75. }
  76. }