preview.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package v1
  2. import (
  3. "net/http"
  4. "strconv"
  5. "github.com/allanpk716/ChineseSubFinder/pkg/decode"
  6. "github.com/allanpk716/ChineseSubFinder/pkg"
  7. "github.com/allanpk716/ChineseSubFinder/pkg/preview_queue"
  8. backend2 "github.com/allanpk716/ChineseSubFinder/pkg/types/backend"
  9. "github.com/gin-gonic/gin"
  10. )
  11. // PreviewAdd 添加需要预览的任务
  12. func (cb *ControllerBase) PreviewAdd(c *gin.Context) {
  13. var err error
  14. defer func() {
  15. // 统一的异常处理
  16. cb.ErrorProcess(c, "PreviewAdd", err)
  17. }()
  18. job := preview_queue.Job{}
  19. err = c.ShouldBindJSON(&job)
  20. if err != nil {
  21. return
  22. }
  23. // 暂时不支持蓝光的预览
  24. if pkg.IsFile(job.VideoFPath) == false {
  25. bok, _, _ := decode.IsFakeBDMVWorked(job.VideoFPath)
  26. if bok == true {
  27. c.JSON(http.StatusOK, backend2.ReplyCommon{Message: "not support blu-ray preview"})
  28. return
  29. } else {
  30. c.JSON(http.StatusOK, backend2.ReplyCommon{Message: "video file not found"})
  31. return
  32. }
  33. }
  34. cb.cronHelper.Downloader.PreviewQueue.Add(&job)
  35. c.JSON(http.StatusOK, backend2.ReplyCommon{Message: "ok"})
  36. return
  37. }
  38. // PreviewList 列举预览任务
  39. func (cb *ControllerBase) PreviewList(c *gin.Context) {
  40. var err error
  41. defer func() {
  42. // 统一的异常处理
  43. cb.ErrorProcess(c, "PreviewList", err)
  44. }()
  45. listJob := cb.cronHelper.Downloader.PreviewQueue.ListJob()
  46. c.JSON(http.StatusOK, preview_queue.Reply{
  47. Jobs: listJob,
  48. })
  49. }
  50. // PreviewIsJobInQueue 预览的任务是否在列表中,或者说是在执行中
  51. func (cb *ControllerBase) PreviewIsJobInQueue(c *gin.Context) {
  52. var err error
  53. defer func() {
  54. // 统一的异常处理
  55. cb.ErrorProcess(c, "PreviewIsJobInQueue", err)
  56. }()
  57. job := preview_queue.Job{}
  58. err = c.ShouldBindJSON(&job)
  59. if err != nil {
  60. return
  61. }
  62. found := cb.cronHelper.Downloader.PreviewQueue.IsJobInQueue(&preview_queue.Job{
  63. VideoFPath: job.VideoFPath,
  64. })
  65. c.JSON(http.StatusOK, backend2.ReplyCommon{Message: strconv.FormatBool(found)})
  66. return
  67. }
  68. // PreviewGetExportInfo 预览的任务的导出信息
  69. func (cb *ControllerBase) PreviewGetExportInfo(c *gin.Context) {
  70. var err error
  71. defer func() {
  72. // 统一的异常处理
  73. cb.ErrorProcess(c, "PreviewGetExportInfo", err)
  74. }()
  75. job := preview_queue.Job{}
  76. err = c.ShouldBindJSON(&job)
  77. if err != nil {
  78. return
  79. }
  80. m3u8, subPath, err := cb.cronHelper.Downloader.PreviewQueue.GetVideoHLSAndSubByTimeRangeExportPathInfo(job.VideoFPath, job.SubFPath, job.StartTime, job.EndTime)
  81. if err != nil {
  82. return
  83. }
  84. c.JSON(http.StatusOK, preview_queue.Job{
  85. VideoFPath: m3u8,
  86. SubFPath: subPath,
  87. })
  88. return
  89. }
  90. func (cb *ControllerBase) PreviewCleanUp(c *gin.Context) {
  91. var err error
  92. defer func() {
  93. // 统一的异常处理
  94. cb.ErrorProcess(c, "PreviewCleanUp", err)
  95. }()
  96. if len(cb.cronHelper.Downloader.PreviewQueue.ListJob()) > 0 {
  97. c.JSON(http.StatusOK, backend2.ReplyCommon{Message: "false"})
  98. return
  99. }
  100. err = pkg.ClearVideoAndSubPreviewCacheFolder()
  101. if err != nil {
  102. return
  103. }
  104. c.JSON(http.StatusOK, backend2.ReplyCommon{Message: "true"})
  105. return
  106. }