TipitakaExportFileController.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. * 文件存在 S3 时 302 跳到 S3 的临时链接(1 小时有效,客户端跟随跳转即可);本地开发直接返回文件。
  17. * 两种情况都支持 `Range`(206,断点续传)与 `If-None-Match`(304)。
  18. * 离线包(offline)是 NDJSON,带 `Content-Encoding: gzip`,HTTP 客户端会自动解压。
  19. * 签名无效或过期 403,文件已过期 404。
  20. *
  21. * @unauthenticated
  22. *
  23. * @urlParam export string required 导出 id。
  24. *
  25. * @queryParam expires integer required 签名的过期时间(download_url 里自带)。
  26. * @queryParam signature string required 签名(download_url 里自带)。
  27. *
  28. * @responseFile application/zip application/epub+zip application/vnd.openxmlformats-officedocument.wordprocessingml.document application/x-ndjson
  29. *
  30. * @responseStatus 302 文件在 S3:跳到临时下载链接
  31. * @responseStatus 206 Range 请求的部分内容
  32. * @responseStatus 304 If-None-Match 命中,本地文件就是最新的
  33. * @responseStatus 403 签名无效或已过期
  34. * @responseStatus 404 文件已过期
  35. * @responseStatus 416 Range 超出文件大小
  36. */
  37. public function __invoke(Request $request, string $export)
  38. {
  39. return $this->download->respond($export, $request->header('Range'), $request->header('If-None-Match'));
  40. }
  41. }