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.

103 lines
2.6 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. 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. $tmpFileId = (string) Str::uuid();
  59. Storage::disk('local')->put('tmp/' . $tmpFileId . '.md', $mdFileContent);
  60. $tmpMdFilePath = Storage::path('tmp/' . $tmpFileId . '.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/' . $tmpFileId . '.odt', $odtFileContent);
  75. return response()->json([
  76. 'path' => 'tmp/' . $tmpFileId . '.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. }