From 58e65b21fb2236dd0b0ead224e0fa669af50401a Mon Sep 17 00:00:00 2001 From: Simon Ding Date: Thu, 25 Jul 2024 14:19:03 +0800 Subject: [PATCH] feat: check connect before add download client --- pkg/transmission/transmission.go | 4 +++ server/resources.go | 36 ------------------------ server/setting.go | 47 ++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 36 deletions(-) diff --git a/pkg/transmission/transmission.go b/pkg/transmission/transmission.go index a045ec7..85b0a29 100644 --- a/pkg/transmission/transmission.go +++ b/pkg/transmission/transmission.go @@ -28,6 +28,10 @@ func NewClient(c Config) (*Client, error) { if err != nil { return nil, errors.Wrap(err, "connect transmission") } + _, err = tbt.TorrentGetAll(context.TODO()) + if err != nil { + return nil, errors.Wrap(err, "transmission cannot connect") + } return &Client{c: tbt, cfg: c}, nil } diff --git a/server/resources.go b/server/resources.go index 42ff42d..55c409c 100644 --- a/server/resources.go +++ b/server/resources.go @@ -278,39 +278,3 @@ func (s *Server) DownloadMovieTorrent(c *gin.Context) (interface{}, error) { } -type downloadClientIn struct { - Name string `json:"name" binding:"required"` - URL string `json:"url" binding:"required"` - User string `json:"user"` - Password string `json:"password"` - Implementation string `json:"implementation" binding:"required"` -} - -func (s *Server) AddDownloadClient(c *gin.Context) (interface{}, error) { - var in downloadClientIn - if err := c.ShouldBindJSON(&in); err != nil { - return nil, errors.Wrap(err, "bind json") - } - if err := s.db.SaveTransmission(in.Name, in.URL, in.User, in.Password); err != nil { - return nil, errors.Wrap(err, "save transmission") - } - return nil, nil -} - -func (s *Server) GetAllDonloadClients(c *gin.Context) (interface{}, error) { - res := s.db.GetAllDonloadClients() - if len(res) == 0 { - return nil, nil - } - return res, nil -} - -func (s *Server) DeleteDownloadCLient(c *gin.Context) (interface{}, error) { - var ids = c.Param("id") - id, err := strconv.Atoi(ids) - if err != nil { - return nil, fmt.Errorf("id is not correct: %v", ids) - } - s.db.DeleteDownloadCLient(id) - return "success", nil -} diff --git a/server/setting.go b/server/setting.go index 7f1b680..675eabf 100644 --- a/server/setting.go +++ b/server/setting.go @@ -96,3 +96,50 @@ func (s *Server) getDownloadClient() (*transmission.Client, error) { } return trc, nil } + + +type downloadClientIn struct { + Name string `json:"name" binding:"required"` + URL string `json:"url" binding:"required"` + User string `json:"user"` + Password string `json:"password"` + Implementation string `json:"implementation" binding:"required"` +} + +func (s *Server) AddDownloadClient(c *gin.Context) (interface{}, error) { + var in downloadClientIn + if err := c.ShouldBindJSON(&in); err != nil { + return nil, errors.Wrap(err, "bind json") + } + //test connection + _, err := transmission.NewClient(transmission.Config{ + URL: in.URL, + User: in.User, + Password: in.Password, + }) + if err != nil { + return nil, errors.Wrap(err, "tranmission setting") + } + if err := s.db.SaveTransmission(in.Name, in.URL, in.User, in.Password); err != nil { + return nil, errors.Wrap(err, "save transmission") + } + return nil, nil +} + +func (s *Server) GetAllDonloadClients(c *gin.Context) (interface{}, error) { + res := s.db.GetAllDonloadClients() + if len(res) == 0 { + return nil, nil + } + return res, nil +} + +func (s *Server) DeleteDownloadCLient(c *gin.Context) (interface{}, error) { + var ids = c.Param("id") + id, err := strconv.Atoi(ids) + if err != nil { + return nil, fmt.Errorf("id is not correct: %v", ids) + } + s.db.DeleteDownloadCLient(id) + return "success", nil +}