TipitakaDownloadController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Http\Controllers\Library;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\TipitakaCatalogService;
  5. use App\Services\V3\TipitakaExportService;
  6. use Illuminate\Support\Facades\Cookie;
  7. use Illuminate\View\View;
  8. /**
  9. * library 三藏的下载页:一部作品,或整个 channel 打包。
  10. *
  11. * 页面只负责展示四种格式现在的状态(已就绪的直接给链接);点击后由页面里的脚本调
  12. * `POST /api/v3/tipitaka-exports` 发起、轮询 `GET /api/v3/tipitaka-exports/{id}`,就绪后自动下载。
  13. */
  14. class TipitakaDownloadController extends Controller
  15. {
  16. /**
  17. * 下载页上的格式及顺序。离线包(offline)是给 mobile 的,不在网页上出现。
  18. */
  19. private const FORMATS = ['epub', 'html', 'docx', 'markdown'];
  20. public function __construct(
  21. private readonly TipitakaCatalogService $catalog,
  22. private readonly TipitakaExportService $exports,
  23. ) {}
  24. public function work(string $channel, int $sn): View
  25. {
  26. $channelModel = $this->catalog->channel($channel);
  27. abort_unless($channelModel, 404);
  28. $scope = $this->exports->scope($channel, $sn, null);
  29. $work = $scope['works'][0];
  30. $progress = $this->catalog->channelProgress($channel)["{$work['book']}-{$work['para']}"] ?? null;
  31. $channelInfo = $this->channelInfo($channelModel);
  32. return view('library.tipitaka.download', [
  33. 'mode' => 'work',
  34. 'channelInfo' => $channelInfo,
  35. 'book' => $this->catalog->bookCard($work, $channelInfo, $progress) + ['pali_title' => $work['title'], 'updated_at' => $progress['updated_at'] ?? null],
  36. 'request' => ['channel' => $channel, 'work' => $sn],
  37. 'formats' => $this->formats($this->exports->describe($channel, $scope, null), true),
  38. 'backUrl' => route('library.tipitaka.channels.work', ['channel' => $channel, 'sn' => $sn]),
  39. ]);
  40. }
  41. /**
  42. * 整个 channel 打包下载。只有「全部」一种范围(用户定:不按藏 / 分类分开下)。
  43. */
  44. public function channel(string $channel): View
  45. {
  46. $channelModel = $this->catalog->channel($channel);
  47. abort_unless($channelModel, 404);
  48. $scope = $this->exports->scope($channel, null, null);
  49. $names = $this->catalog->localNames($this->catalog->categories(), Cookie::get('language') ?? 'en');
  50. $groups = [];
  51. foreach ($scope['works'] as $work) {
  52. $path = $work['category_id'] === null ? [] : $this->catalog->categoryPath($work['category_id']);
  53. $label = implode(' · ', array_map(fn ($c) => $names[$c['name']] ?? $c['name'], $path)) ?: __('library.export_uncategorized');
  54. $groups[$label][] = [
  55. 'title' => $work['local_title'],
  56. 'progress' => (int) round($work['progress'] * 100),
  57. 'url' => route('library.tipitaka.channels.work', ['channel' => $channel, 'sn' => $work['sn']]),
  58. ];
  59. }
  60. return view('library.tipitaka.download', [
  61. 'mode' => 'channel',
  62. 'channelInfo' => $this->channelInfo($channelModel),
  63. 'groups' => $groups,
  64. 'request' => ['channel' => $channel],
  65. 'formats' => $this->formats($this->exports->describe($channel, $scope, null), false),
  66. 'backUrl' => route('library.tipitaka.channels.category', ['channel' => $channel]),
  67. ]);
  68. }
  69. /**
  70. * @return array{uid: string, name: string, type: string, lang: string, owner: mixed}
  71. */
  72. private function channelInfo(object $channel): array
  73. {
  74. return ['uid' => $channel->uid, 'name' => $channel->name, 'type' => $channel->type, 'lang' => $channel->lang, 'owner' => $channel->owner];
  75. }
  76. /**
  77. * 格式卡片:名字、说明(单部作品 / 打包两种口径)与当前状态。
  78. *
  79. * @param array<string, array<string, mixed>> $states
  80. * @return list<array<string, mixed>>
  81. */
  82. private function formats(array $states, bool $single): array
  83. {
  84. return array_map(fn (string $format) => [
  85. 'key' => $format,
  86. 'name' => __("library.download_format_{$format}"),
  87. 'description' => __($single ? "library.download_format_{$format}_desc" : "library.download_format_{$format}_bundle"),
  88. 'state' => $states[$format],
  89. ], self::FORMATS);
  90. }
  91. }