1
0

api.go 5.7 KB

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