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.

106 lines
2.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Contracts\Container\BindingResolutionException;
  4. use Illuminate\Http\JsonResponse;
  5. use Illuminate\Support\Facades\Storage;
  6. use Illuminate\Http\UploadedFile;
  7. use GuzzleHttp\Client;
  8. use GuzzleHttp\Exception\BadResponseException;
  9. use Illuminate\Support\Str;
  10. use Symfony\Component\Process\Process;
  11. class FileController extends Controller
  12. {
  13. /**
  14. *
  15. * @return JsonResponse
  16. *
  17. * @throws BindingResolutionException
  18. */
  19. public function create(): JsonResponse
  20. {
  21. try {
  22. /** @var UploadedFile $file */
  23. $file = request()->file('file');
  24. $time = time();
  25. $fileName = $time . '-' . $file->getClientOriginalName();
  26. $fileId = $time . pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
  27. $path = Storage::putFileAs('files/', $file, $fileName);
  28. $guzzle = new Client();
  29. /** @var \Psr\Http\Message\ResponseInterface $response */
  30. $response = $guzzle->request('POST', 'http://ingest.sandd/ingest', [
  31. 'multipart' => [
  32. [
  33. 'name' => 'id',
  34. 'contents' => $fileId,
  35. 'headers' => [ 'Content-Type' => 'application/json' ]
  36. ],
  37. [
  38. 'name' => 'document',
  39. 'contents' => fopen($file->getPathname(), 'r')
  40. ]
  41. ]
  42. ]);
  43. return response()->json([
  44. 'file' => $file->getClientOriginalName(),
  45. 'id' => $fileId,
  46. 'path' => $path,
  47. ]);
  48. } catch (BadResponseException $e) {
  49. return response()->json([
  50. 'message' => $e->getMessage(),
  51. 'response' => $e->getResponse()
  52. ], 400);
  53. }
  54. }
  55. /**
  56. *
  57. */
  58. public function convert(): JsonResponse
  59. {
  60. $mdFileContent = request()->input('content');
  61. $tmpFileId = (string) Str::uuid();
  62. Storage::disk('local')->put('tmp/' . $tmpFileId . '.md', $mdFileContent);
  63. $tmpMdFilePath = Storage::path('tmp/' . $tmpFileId . '.md');
  64. $pandoc = new Process([
  65. 'pandoc',
  66. '-f',
  67. 'markdown',
  68. '-t',
  69. 'odt',
  70. $tmpMdFilePath
  71. ]);
  72. $pandoc->start();
  73. while ($pandoc->isRunning()) {
  74. //
  75. }
  76. $odtFileContent = $pandoc->getOutput();
  77. Storage::disk('local')->put('tmp/' . $tmpFileId . '.odt', $odtFileContent);
  78. return response()->json([
  79. 'path' => 'tmp/' . $tmpFileId . '.odt'
  80. ]);
  81. }
  82. /**
  83. *
  84. * @param string $path
  85. * @return mixed
  86. */
  87. public function download(string $path)
  88. {
  89. return Storage::download($path);
  90. }
  91. }