1
0

api.go 5.7 KB

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