ExportController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Api\Mq;
  4. use App\Services\AuthService;
  5. use App\Tools\ExportDownload;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Http\Response;
  8. use Illuminate\Support\Facades\Storage;
  9. use Illuminate\Support\Str;
  10. class ExportController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return Response
  16. */
  17. public function index(Request $request)
  18. {
  19. $queryId = Str::uuid();
  20. $token = AuthService::getToken($request);
  21. switch ($request->input('type', 'chapter')) {
  22. case 'chapter':
  23. $data = [
  24. 'book' => $request->input('book'),
  25. 'para' => $request->input('par'),
  26. 'channel' => $request->input('channel'),
  27. 'format' => $request->input('format'),
  28. 'origin' => $request->input('origin'),
  29. 'translation' => $request->input('translation'),
  30. 'queryId' => $queryId,
  31. ];
  32. if ($token) {
  33. $data['token'] = $token;
  34. }
  35. Mq::publish('export_pali_chapter', $data);
  36. break;
  37. case 'article':
  38. $data = [
  39. 'id' => $request->input('id'),
  40. 'channel' => $request->input('channel'),
  41. 'format' => $request->input('format'),
  42. 'origin' => $request->input('origin'),
  43. 'translation' => $request->input('translation'),
  44. 'queryId' => $queryId,
  45. 'anthology' => $request->input('anthology'),
  46. 'channel' => $request->input('channel'),
  47. ];
  48. if ($token) {
  49. $data['token'] = $token;
  50. }
  51. Mq::publish('export_article', $data);
  52. break;
  53. default:
  54. return $this->error('unknown type '.$request->input('type'), 400, 400);
  55. break;
  56. }
  57. return $this->ok($queryId);
  58. }
  59. /**
  60. * Store a newly created resource in storage.
  61. *
  62. * @return Response
  63. */
  64. public function store(Request $request)
  65. {
  66. //
  67. }
  68. /**
  69. * Display the specified resource.
  70. *
  71. * @param string $id
  72. * @return Response
  73. */
  74. public function show($filename)
  75. {
  76. //
  77. $exportChapter = new ExportDownload(['queryId' => $filename]);
  78. $exportStatus = $exportChapter->getStatus();
  79. if (empty($exportStatus)) {
  80. return $this->error('no file', 200, 200);
  81. }
  82. $output = [];
  83. $output['status'] = $exportStatus;
  84. if ($exportStatus['progress'] === 1) {
  85. $output['url'] = $exportStatus['url'];
  86. }
  87. return $this->ok($output);
  88. }
  89. /**
  90. * Update the specified resource in storage.
  91. *
  92. * @param int $id
  93. * @return Response
  94. */
  95. public function update(Request $request, $id)
  96. {
  97. //
  98. }
  99. /**
  100. * Remove the specified resource from storage.
  101. *
  102. * @param int $id
  103. * @return Response
  104. */
  105. public function destroy($id)
  106. {
  107. //
  108. }
  109. }