api.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package v1
  2. import (
  3. "errors"
  4. "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
  5. "github.com/allanpk716/ChineseSubFinder/internal/types/backend"
  6. "github.com/allanpk716/ChineseSubFinder/internal/types/common"
  7. TTaskqueue "github.com/allanpk716/ChineseSubFinder/internal/types/task_queue"
  8. "github.com/gin-gonic/gin"
  9. "net/http"
  10. )
  11. // AddJobHandler 外部 API 接口添加任务的处理
  12. func (cb ControllerBase) AddJobHandler(c *gin.Context) {
  13. var err error
  14. defer func() {
  15. // 统一的异常处理
  16. cb.ErrorProcess(c, "AddJobHandler", err)
  17. }()
  18. videoListAdd := backend.ReqVideoListAdd{}
  19. err = c.ShouldBindJSON(&videoListAdd)
  20. if err != nil {
  21. return
  22. }
  23. // 这里视频文件得要存在
  24. if my_util.IsFile(videoListAdd.PhysicalVideoFileFullPath) == false {
  25. c.JSON(http.StatusOK, backend.ReplyJobThings{
  26. Message: "physical video file not found",
  27. })
  28. return
  29. }
  30. videoType := common.Movie
  31. if videoListAdd.VideoType == 1 {
  32. videoType = common.Series
  33. }
  34. nowJob := TTaskqueue.NewOneJob(
  35. videoType, videoListAdd.PhysicalVideoFileFullPath, videoListAdd.TaskPriorityLevel,
  36. videoListAdd.MediaServerInsideVideoID,
  37. )
  38. bok, err := cb.cronHelper.DownloadQueue.Add(*nowJob)
  39. if err != nil {
  40. return
  41. }
  42. if bok == false {
  43. c.JSON(http.StatusOK, backend.ReplyJobThings{
  44. JobID: nowJob.Id,
  45. Message: "job is already in queue",
  46. })
  47. } else {
  48. c.JSON(http.StatusOK, backend.ReplyJobThings{
  49. JobID: nowJob.Id,
  50. Message: "ok",
  51. })
  52. }
  53. }
  54. // GetJobStatusHandler 外部 API 接口获取任务的状态
  55. func (cb ControllerBase) GetJobStatusHandler(c *gin.Context) {
  56. var err error
  57. defer func() {
  58. // 统一的异常处理
  59. cb.ErrorProcess(c, "GetJobStatusHandler", err)
  60. }()
  61. jobID := c.DefaultQuery("job_id", "")
  62. if jobID == "" {
  63. err = errors.New("job_id is empty")
  64. return
  65. }
  66. found, oneJob := cb.cronHelper.DownloadQueue.GetOneJobByID(jobID)
  67. if found == false {
  68. err = errors.New("GetOneJobByID failed, id=" + jobID)
  69. return
  70. }
  71. }