api.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package v1
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/internal/dao"
  4. "github.com/allanpk716/ChineseSubFinder/internal/models"
  5. "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
  6. "github.com/allanpk716/ChineseSubFinder/internal/types/backend"
  7. "github.com/allanpk716/ChineseSubFinder/internal/types/common"
  8. TTaskqueue "github.com/allanpk716/ChineseSubFinder/internal/types/task_queue"
  9. "github.com/gin-gonic/gin"
  10. "net/http"
  11. "path/filepath"
  12. )
  13. // AddJobHandler 外部 API 接口添加任务的处理
  14. func (cb *ControllerBase) AddJobHandler(c *gin.Context) {
  15. var err error
  16. defer func() {
  17. // 统一的异常处理
  18. cb.ErrorProcess(c, "AddJobHandler", err)
  19. }()
  20. videoListAdd := backend.ReqVideoListAdd{}
  21. err = c.ShouldBindJSON(&videoListAdd)
  22. if err != nil {
  23. return
  24. }
  25. // 这里视频文件得要存在
  26. if my_util.IsFile(videoListAdd.PhysicalVideoFileFullPath) == false {
  27. c.JSON(http.StatusOK, backend.ReplyJobThings{
  28. Message: "physical video file not found",
  29. })
  30. return
  31. }
  32. videoType := common.Movie
  33. if videoListAdd.VideoType == 1 {
  34. videoType = common.Series
  35. }
  36. nowJob := TTaskqueue.NewOneJob(
  37. videoType, videoListAdd.PhysicalVideoFileFullPath, videoListAdd.TaskPriorityLevel,
  38. videoListAdd.MediaServerInsideVideoID,
  39. )
  40. bok, err := cb.cronHelper.DownloadQueue.Add(*nowJob)
  41. if err != nil {
  42. return
  43. }
  44. if bok == false {
  45. c.JSON(http.StatusOK, backend.ReplyJobThings{
  46. JobID: nowJob.Id,
  47. Message: "job is already in queue",
  48. })
  49. } else {
  50. c.JSON(http.StatusOK, backend.ReplyJobThings{
  51. JobID: nowJob.Id,
  52. Message: "ok",
  53. })
  54. }
  55. }
  56. // GetJobStatusHandler 外部 API 接口获取任务的状态
  57. func (cb *ControllerBase) GetJobStatusHandler(c *gin.Context) {
  58. var err error
  59. defer func() {
  60. // 统一的异常处理
  61. cb.ErrorProcess(c, "GetJobStatusHandler", err)
  62. }()
  63. jobID := c.DefaultQuery("job_id", "")
  64. if jobID == "" {
  65. c.JSON(http.StatusOK, backend.ReplyJobThings{
  66. Message: "job_id is empty",
  67. })
  68. return
  69. }
  70. found, nowOneJob := cb.cronHelper.DownloadQueue.GetOneJobByID(jobID)
  71. if found == false {
  72. c.JSON(http.StatusOK, backend.ReplyJobThings{
  73. JobID: jobID,
  74. Message: "job not found",
  75. })
  76. return
  77. }
  78. c.JSON(http.StatusOK, backend.ReplyJobThings{
  79. JobID: jobID,
  80. JobStatus: nowOneJob.JobStatus,
  81. Message: "ok",
  82. })
  83. }
  84. // AddVideoPlayedInfoHandler 外部 API 接口添加已观看视频的信息
  85. func (cb *ControllerBase) AddVideoPlayedInfoHandler(c *gin.Context) {
  86. var err error
  87. defer func() {
  88. // 统一的异常处理
  89. cb.ErrorProcess(c, "AddVideoPlayedInfoHandler", err)
  90. }()
  91. videoPlayedInfo := backend.ReqVideoPlayedInfo{}
  92. err = c.ShouldBindJSON(&videoPlayedInfo)
  93. if err != nil {
  94. return
  95. }
  96. // 这里视频文件得要存在
  97. if my_util.IsFile(videoPlayedInfo.PhysicalVideoFileFullPath) == false {
  98. c.JSON(http.StatusOK, backend.ReplyJobThings{
  99. Message: "physical video file not found",
  100. })
  101. return
  102. }
  103. // 查询字幕是否存在
  104. videoDirFPath := filepath.Dir(videoPlayedInfo.PhysicalVideoFileFullPath)
  105. subFileFullPath := filepath.Join(videoDirFPath, videoPlayedInfo.SubName)
  106. if my_util.IsFile(subFileFullPath) == false {
  107. c.JSON(http.StatusOK, backend.ReplyJobThings{
  108. Message: "sub file not found",
  109. })
  110. return
  111. }
  112. var videoPlayedInfos []models.ThirdPartSetVideoPlayedInfo
  113. dao.GetDb().Where("physical_video_file_full_path = ?", videoPlayedInfo.PhysicalVideoFileFullPath).Find(&videoPlayedInfos)
  114. if len(videoPlayedInfos) == 0 {
  115. // 没有则新增
  116. nowVideoPlayedInfo := models.ThirdPartSetVideoPlayedInfo{
  117. PhysicalVideoFileFullPath: videoPlayedInfo.PhysicalVideoFileFullPath,
  118. SubName: videoPlayedInfo.SubName,
  119. }
  120. dao.GetDb().Create(&nowVideoPlayedInfo)
  121. } else {
  122. // 有则更新
  123. videoPlayedInfos[0].SubName = videoPlayedInfo.SubName
  124. dao.GetDb().Save(&videoPlayedInfos[0])
  125. }
  126. c.JSON(http.StatusOK, backend.ReplyJobThings{
  127. Message: "ok",
  128. })
  129. }
  130. // DelVideoPlayedInfoHandler 外部 API 接口删除已观看视频的信息
  131. func (cb *ControllerBase) DelVideoPlayedInfoHandler(c *gin.Context) {
  132. var err error
  133. defer func() {
  134. // 统一的异常处理
  135. cb.ErrorProcess(c, "DelVideoPlayedInfoHandler", err)
  136. }()
  137. videoPlayedInfo := backend.ReqVideoPlayedInfo{}
  138. err = c.ShouldBindJSON(&videoPlayedInfo)
  139. if err != nil {
  140. return
  141. }
  142. // 这里视频文件得要存在
  143. if my_util.IsFile(videoPlayedInfo.PhysicalVideoFileFullPath) == false {
  144. c.JSON(http.StatusOK, backend.ReplyJobThings{
  145. Message: "physical video file not found",
  146. })
  147. return
  148. }
  149. var videoPlayedInfos []models.ThirdPartSetVideoPlayedInfo
  150. dao.GetDb().Where("physical_video_file_full_path = ?", videoPlayedInfo.PhysicalVideoFileFullPath).Find(&videoPlayedInfos)
  151. if len(videoPlayedInfos) == 0 {
  152. // 没有则也返回成功
  153. c.JSON(http.StatusOK, backend.ReplyJobThings{
  154. Message: "ok",
  155. })
  156. return
  157. } else {
  158. // 有则更新,因为这个物理路径是主键,所以不用担心会查询出多个
  159. dao.GetDb().Delete(&videoPlayedInfos[0])
  160. c.JSON(http.StatusOK, backend.ReplyJobThings{
  161. Message: "ok",
  162. })
  163. return
  164. }
  165. }