TipitakaExportFileController.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace App\Http\Controllers\V3;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\V3\TipitakaExportDownload;
  5. use Illuminate\Http\Request;
  6. /**
  7. * 三藏下载的文件本身。只认签名链接(`GET /v3/tipitaka-exports/{id}` 的 `download_url`),
  8. * 不需要 token:浏览器直接跳转、mobile 交给系统下载器都能用。
  9. */
  10. class TipitakaExportFileController extends Controller
  11. {
  12. public function __construct(private readonly TipitakaExportDownload $download) {}
  13. /**
  14. * 下载文件
  15. *
  16. * 支持 `Range`(206,断点续传)与 `If-None-Match`(304,ETag 就是导出 id)。
  17. * 离线包(offline)是 NDJSON,带 `Content-Encoding: gzip`,HTTP 客户端会自动解压。
  18. * 签名无效或过期 403,文件已过期 404。
  19. *
  20. * @unauthenticated
  21. *
  22. * @urlParam export string required 导出 id。
  23. *
  24. * @queryParam expires integer required 签名的过期时间(download_url 里自带)。
  25. * @queryParam signature string required 签名(download_url 里自带)。
  26. *
  27. * @responseFile application/zip application/epub+zip application/vnd.openxmlformats-officedocument.wordprocessingml.document application/x-ndjson
  28. *
  29. * @responseStatus 206 Range 请求的部分内容
  30. * @responseStatus 304 If-None-Match 命中,本地文件就是最新的
  31. * @responseStatus 403 签名无效或已过期
  32. * @responseStatus 404 文件已过期
  33. * @responseStatus 416 Range 超出文件大小
  34. */
  35. public function __invoke(Request $request, string $export)
  36. {
  37. return $this->download->respond($export, $request->header('Range'), $request->header('If-None-Match'));
  38. }
  39. }