ProcessPdfJob.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Jobs;
  3. use App\Domain\Import\ImportPipeline;
  4. use App\Models\MarkdownImport;
  5. use App\Models\PreQuestionCandidate;
  6. use App\Models\SourceFile;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Support\Str;
  13. class ProcessPdfJob implements ShouldQueue
  14. {
  15. use Dispatchable;
  16. use InteractsWithQueue;
  17. use Queueable;
  18. use SerializesModels;
  19. public function __construct(public readonly string $path)
  20. {
  21. }
  22. public function handle(ImportPipeline $pipeline): void
  23. {
  24. $sourceFile = SourceFile::create([
  25. 'uuid' => (string) Str::uuid(),
  26. 'original_filename' => basename($this->path),
  27. 'normalized_filename' => basename($this->path),
  28. 'extension' => 'pdf',
  29. 'storage_path' => $this->path,
  30. 'raw_markdown' => '',
  31. 'source_type' => 'pdf',
  32. 'filename' => basename($this->path),
  33. 'path' => $this->path,
  34. 'status' => 'pending',
  35. ]);
  36. $import = MarkdownImport::create([
  37. 'file_name' => $sourceFile->original_filename ?: $sourceFile->filename ?: 'pdf',
  38. 'original_markdown' => '',
  39. 'source_type' => 'pdf',
  40. 'source_name' => $this->path,
  41. 'status' => MarkdownImport::STATUS_PROCESSING,
  42. 'progress_stage' => MarkdownImport::STAGE_SPLITTING,
  43. ]);
  44. $payload = $pipeline->run('pdf', [
  45. 'path' => $this->path,
  46. ]);
  47. foreach ($payload['blocks'] ?? [] as $block) {
  48. PreQuestionCandidate::create([
  49. 'import_id' => $import->id,
  50. 'source_file_id' => $sourceFile->id,
  51. 'order_index' => (int) ($block['sequence'] ?? 0),
  52. 'index' => (int) ($block['index'] ?? 0),
  53. 'raw_markdown' => (string) ($block['raw_markdown'] ?? ''),
  54. 'raw_text' => '',
  55. 'is_question_candidate' => false,
  56. 'status' => PreQuestionCandidate::STATUS_PENDING,
  57. 'meta' => $block['meta'] ?? [],
  58. ]);
  59. }
  60. }
  61. }