mirror of
https://github.com/simon-ding/polaris.git
synced 2026-02-06 23:21:00 +08:00
83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"polaris/db"
|
|
|
|
"polaris/log"
|
|
"polaris/pkg/storage"
|
|
"polaris/pkg/utils"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func (s *Server) GetAllStorage(c *gin.Context) (interface{}, error) {
|
|
data := s.db.GetAllStorage()
|
|
return data, nil
|
|
}
|
|
|
|
func (s *Server) AddStorage(c *gin.Context) (interface{}, error) {
|
|
var in db.StorageInfo
|
|
if err := c.ShouldBindJSON(&in); err != nil {
|
|
return nil, errors.Wrap(err, "bind json")
|
|
}
|
|
utils.TrimFields(&in)
|
|
|
|
if in.Implementation == "webdav" {
|
|
//test webdav
|
|
wd := in.ToWebDavSetting()
|
|
st, err := storage.NewWebdavStorage(wd.URL, wd.User, wd.Password, in.TvPath, false)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "new webdav")
|
|
}
|
|
fs, err := st.ReadDir(".")
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "test read")
|
|
}
|
|
for _, f := range fs {
|
|
log.Infof("file name: %v", f.Name())
|
|
}
|
|
}
|
|
log.Infof("received add storage input: %v", in)
|
|
err := s.db.AddStorage(&in)
|
|
return nil, err
|
|
}
|
|
|
|
func (s *Server) DeleteStorage(c *gin.Context) (interface{}, error) {
|
|
ids := c.Param("id")
|
|
id, err := strconv.Atoi(ids)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("id is not int: %v", ids)
|
|
}
|
|
err = s.db.DeleteStorage(id)
|
|
return nil, err
|
|
}
|
|
|
|
func (s *Server) SuggestedSeriesFolderName(c *gin.Context) (interface{}, error) {
|
|
ids := c.Param("tmdb_id")
|
|
id, err := strconv.Atoi(ids)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("id is not int: %v", ids)
|
|
}
|
|
name, err := s.core.SuggestedSeriesFolderName(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return gin.H{"name": name}, nil
|
|
}
|
|
|
|
func (s *Server) SuggestedMovieFolderName(c *gin.Context) (interface{}, error) {
|
|
ids := c.Param("tmdb_id")
|
|
id, err := strconv.Atoi(ids)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("id is not int: %v", ids)
|
|
}
|
|
name, err := s.core.SuggestedMovieFolderName(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return gin.H{"name": name}, nil
|
|
}
|