Skip to content

Commit

Permalink
WIP: size limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-ding committed Nov 19, 2024
1 parent be07e45 commit b69881d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 16 deletions.
15 changes: 6 additions & 9 deletions db/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ const (
SettingNfoSupportEnabled = "nfo_support_enabled"
SettingAllowQiangban = "filter_qiangban"
SettingEnableTmdbAdultContent = "tmdb_adult_content"
SetttingSizeLimiter = "size_limiter"
SettingTvNamingFormat = "tv_naming_format"
SettingMovieNamingFormat = "movie_naming_format"
SettingProwlarrInfo = "prowlarr_info"
Setting720pSizeLimiter = "720p_size_limiter"
Setting1080ppSizeLimiter = "1080p_size_limiter"
Setting2160ppSizeLimiter = "2160p_size_limiter"
)

const (
Expand Down Expand Up @@ -52,14 +54,9 @@ type ResolutionType string
const JwtSerectKey = "jwt_secrect_key"

type SizeLimiter struct {
R720p Limiter `json:"720p"`
R1080p Limiter `json:"1080p"`
R2160p Limiter `json:"2160p"`
}

type Limiter struct {
Max int `json:"max"`
Min int `json:"min"`
MaxSIze int64 `json:"max_size"`
MinSize int64 `json:"min_size"`
PreferSIze int64 `json:"prefer_size"`
}

type ProwlarrSetting struct {
Expand Down
30 changes: 26 additions & 4 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,19 +631,41 @@ func (c *Client) DeleteImportlist(id int) error {
return c.ent.ImportList.DeleteOneID(id).Exec(context.TODO())
}

func (c *Client) GetSizeLimiter() (*SizeLimiter, error) {
v := c.GetSetting(SetttingSizeLimiter)
func (c *Client) GetSizeLimiter(r media.Resolution) (*SizeLimiter, error) {
var v string
if r == media.Resolution720p {
v = c.GetSetting(Setting720pSizeLimiter)
} else if r == media.Resolution1080p {
v = c.GetSetting(Setting1080ppSizeLimiter)
} else if r == media.Resolution2160p {
v = c.GetSetting(Setting2160ppSizeLimiter)
} else {
return nil, errors.Errorf("resolution not supported: %v", r)
}

var limiter SizeLimiter
if v == "" {
return &limiter, nil
}

err := json.Unmarshal([]byte(v), &limiter)
return &limiter, err
}

func (c *Client) SetSizeLimiter(limiter *SizeLimiter) error {
func (c *Client) SetSizeLimiter(r media.Resolution, limiter *SizeLimiter) error {
data, err := json.Marshal(limiter)
if err != nil {
return err
}
return c.SetSetting(SetttingSizeLimiter, string(data))
if r == media.Resolution720p {
return c.SetSetting(Setting720pSizeLimiter, string(data))
} else if r == media.Resolution1080p {
return c.SetSetting(Setting1080ppSizeLimiter, string(data))
} else if r == media.Resolution2160p {
return c.SetSetting(Setting2160ppSizeLimiter, string(data))
} else {
return errors.Errorf("resolution not supported: %v", r)
}
}

func (c *Client) GetTvNamingFormat() string {
Expand Down
2 changes: 2 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func (s *Server) Serve() error {
setting.POST("/cron/trigger", HttpHandler(s.TriggerCronJob))
setting.GET("/prowlarr", HttpHandler(s.GetProwlarrSetting))
setting.POST("/prowlarr", HttpHandler(s.SaveProwlarrSetting))
setting.GET("/limiter", HttpHandler(s.GetSizeLimiter))
setting.POST("/limiter", HttpHandler(s.SetSizeLimiter))
}
activity := api.Group("/activity")
{
Expand Down
52 changes: 49 additions & 3 deletions server/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"polaris/db"
"polaris/ent"
"polaris/ent/downloadclients"
"polaris/ent/media"
"polaris/log"
"polaris/pkg/prowlarr"
"polaris/pkg/qbittorrent"
Expand Down Expand Up @@ -104,7 +105,6 @@ func (s *Server) SetSetting(c *gin.Context) (interface{}, error) {
return nil, nil
}


func (s *Server) GetSetting(c *gin.Context) (interface{}, error) {
tmdb := s.db.GetSetting(db.SettingTmdbApiKey)
downloadDir := s.db.GetSetting(db.SettingDownloadDir)
Expand Down Expand Up @@ -306,7 +306,7 @@ func (s *Server) TriggerCronJob(c *gin.Context) (interface{}, error) {
}

func (s *Server) GetProwlarrSetting(c *gin.Context) (interface{}, error) {
se, err :=s.db.GetProwlarrSetting()
se, err := s.db.GetProwlarrSetting()
if err != nil {
return &db.ProwlarrSetting{}, nil
}
Expand All @@ -321,11 +321,57 @@ func (s *Server) SaveProwlarrSetting(c *gin.Context) (interface{}, error) {
client := prowlarr.New(in.ApiKey, in.URL)
if _, err := client.GetIndexers(prowlarr.TV); err != nil {
return nil, errors.Wrap(err, "connect to prowlarr error")
}
}
}
err := s.db.SaveProwlarrSetting(&in)
if err != nil {
return nil, err
}
return "success", nil
}

type ResolutionSizeLimiter struct {
P720 db.SizeLimiter `json:"720p"`
P1080 db.SizeLimiter `json:"1080p"`
P2160 db.SizeLimiter `json:"2160p"`
}

func (s *Server) GetSizeLimiter(c *gin.Context) (interface{}, error) {
p720, err := s.db.GetSizeLimiter(media.Resolution720p)
if err != nil {
return nil, errors.Wrap(err, "db")
}
p1080, err := s.db.GetSizeLimiter(media.Resolution1080p)
if err != nil {
return nil, errors.Wrap(err, "db")
}

p2160, err := s.db.GetSizeLimiter(media.Resolution2160p)
if err != nil {
return nil, errors.Wrap(err, "db")
}
r := ResolutionSizeLimiter{
P720: *p720,
P1080: *p1080,
P2160: *p2160,
}
return r, nil
}

func (s *Server) SetSizeLimiter(c *gin.Context) (interface{}, error) {
var in ResolutionSizeLimiter
if err := c.ShouldBindJSON(&in); err != nil {
return nil, err
}
if err := s.db.SetSizeLimiter(media.Resolution720p, &in.P720); err != nil {
return nil, errors.Wrap(err, "db")
}
if err := s.db.SetSizeLimiter(media.Resolution1080p, &in.P1080); err != nil {
return nil, errors.Wrap(err, "db")
}

if err := s.db.SetSizeLimiter(media.Resolution2160p, &in.P2160); err != nil {
return nil, errors.Wrap(err, "db")
}
return "success", nil
}

0 comments on commit b69881d

Please sign in to comment.