api.go 5.1 KB

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