tmdb_api.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package base
  2. import (
  3. "net/http"
  4. "github.com/allanpk716/ChineseSubFinder/pkg/local_http_proxy_server"
  5. "github.com/allanpk716/ChineseSubFinder/pkg/settings"
  6. "github.com/allanpk716/ChineseSubFinder/pkg/tmdb_api"
  7. "github.com/allanpk716/ChineseSubFinder/pkg/types/backend"
  8. "github.com/gin-gonic/gin"
  9. )
  10. func (cb *ControllerBase) CheckTmdbApiHandler(c *gin.Context) {
  11. var err error
  12. defer func() {
  13. // 统一的异常处理
  14. cb.ErrorProcess(c, "CheckTmdbApiHandler", err)
  15. }()
  16. req := tmdb_api.Req{}
  17. err = c.ShouldBindJSON(&req)
  18. if err != nil {
  19. return
  20. }
  21. if req.ApiKey == "" {
  22. c.JSON(http.StatusOK, backend.ReplyCommon{Message: "false"})
  23. return
  24. }
  25. // 备份一份
  26. bkProxySettings := settings.Get().AdvancedSettings.ProxySettings.CopyOne()
  27. // 赋值 Web 传递过来的需要测试的代理参数
  28. settings.Get().AdvancedSettings.ProxySettings = &req.ProxySettings
  29. // 设置代理
  30. err = local_http_proxy_server.SetProxyInfo(settings.Get().AdvancedSettings.ProxySettings.GetInfos())
  31. if err != nil {
  32. return
  33. }
  34. // 开始测试 tmdb api
  35. tmdbApi, err := tmdb_api.NewTmdbHelper(
  36. cb.fileDownloader.Log,
  37. req.ApiKey)
  38. if err != nil {
  39. cb.fileDownloader.Log.Errorln("NewTmdbHelper", err)
  40. return
  41. }
  42. defer func() {
  43. // 还原
  44. settings.Get().AdvancedSettings.ProxySettings = bkProxySettings
  45. cb.proxyCheckLocker.Unlock()
  46. err = local_http_proxy_server.SetProxyInfo(settings.Get().AdvancedSettings.ProxySettings.GetInfos())
  47. if err != nil {
  48. return
  49. }
  50. // 启动代理
  51. local_http_proxy_server.GetProxyUrl()
  52. }()
  53. if tmdbApi.Alive() == false {
  54. cb.fileDownloader.Log.Errorln("tmdbApi.Alive() == false")
  55. c.JSON(http.StatusOK, backend.ReplyCommon{Message: "false"})
  56. return
  57. } else {
  58. cb.fileDownloader.Log.Infoln("tmdbApi.Alive() == true")
  59. c.JSON(http.StatusOK, backend.ReplyCommon{Message: "true"})
  60. return
  61. }
  62. }