diff --git a/internal/boilerplate/config/configresolver/coerce.go b/internal/boilerplate/config/configresolver/coerce.go index 96da9818..25be5edc 100644 --- a/internal/boilerplate/config/configresolver/coerce.go +++ b/internal/boilerplate/config/configresolver/coerce.go @@ -6,23 +6,15 @@ import ( "strconv" "strings" "time" - - "github.com/bitmagnet-io/bitmagnet/internal/database/search" ) var durationType = reflect.TypeOf(time.Duration(0)) -var orderByType = reflect.TypeOf(search.TorrentContentOrderByRelevance) -var orderDirectionType = reflect.TypeOf(search.OrderDirectionDescending) func coerceStringValue(stringValue string, valueType reflect.Type) (interface{}, error) { // todo Fill this out switch valueType { case durationType: return time.ParseDuration(stringValue) - case orderByType: - return search.ParseTorrentContentOrderBy(stringValue) - case orderDirectionType: - return search.ParseOrderDirection(stringValue) } switch valueType.Kind() { case reflect.String: diff --git a/internal/torznab/adapter/search.go b/internal/torznab/adapter/search.go index 36f913db..c434454e 100644 --- a/internal/torznab/adapter/search.go +++ b/internal/torznab/adapter/search.go @@ -59,7 +59,11 @@ func (a adapter) searchRequestOptions(r torznab.SearchRequest) ([]query.Option, } } if r.Query != "" { - options = append(options, query.QueryString(r.Query), query.OrderBy(r.OrderBy.Clauses(r.OrderDirection)...)) + order := search.TorrentContentOrderByRelevance + if r.Profile.DisableOrderByRelevance { + order = search.TorrentContentOrderByPublishedAt + } + options = append(options, query.QueryString(r.Query), query.OrderBy(order.Clauses(search.OrderDirectionDescending)...)) } var catsCriteria []query.Criteria for _, cat := range r.Cats { @@ -169,8 +173,8 @@ func (a adapter) searchRequestOptions(r torznab.SearchRequest) ([]query.Option, options = append(options, query.Offset(r.Offset.Uint)) } - if len(r.Tags) > 0 { - options = append(options, query.Where(search.TorrentTagCriteria(r.Tags...))) + if len(r.Profile.Tags) > 0 { + options = append(options, query.Where(search.TorrentTagCriteria(r.Profile.Tags...))) } return options, nil } diff --git a/internal/torznab/config.go b/internal/torznab/config.go index ad3c5ac2..1e8611c7 100644 --- a/internal/torznab/config.go +++ b/internal/torznab/config.go @@ -1,14 +1,10 @@ package torznab -import ( - "github.com/bitmagnet-io/bitmagnet/internal/database/search" -) - type Profile struct { - Name string `validate:"required"` - OrderBy search.TorrentContentOrderBy - OrderDirection search.OrderDirection - Tags []string + Name string `validate:"required"` + DisableOrderByRelevance bool + LogRequest bool + Tags []string } type Config struct { @@ -16,27 +12,9 @@ type Config struct { Profiles []Profile } -// force in defaulting of []Profiles. Name is mandated by validator -func (p Profile) completeProfile() Profile { - orderBy := search.TorrentContentOrderByRelevance - direction := search.OrderDirectionDescending - if p.OrderBy != "" { - orderBy = p.OrderBy - } - if p.OrderDirection != "" { - direction = p.OrderDirection - } - return Profile{ - Name: p.Name, - OrderBy: orderBy, - OrderDirection: direction, - Tags: p.Tags, - } -} - func NewDefaultConfig() Config { return Config{ - DefaultProfile: Profile{Name: "default"}.completeProfile(), + DefaultProfile: Profile{Name: "default"}, } } @@ -44,7 +22,7 @@ func NewDefaultConfig() Config { func (c *Config) Map() map[string]Profile { profileMap := make(map[string]Profile, len(c.Profiles)) for _, profile := range c.Profiles { - profileMap[profile.Name] = profile.completeProfile() + profileMap[profile.Name] = profile } return profileMap diff --git a/internal/torznab/httpserver/httpserver.go b/internal/torznab/httpserver/httpserver.go index 159d4130..904e5d97 100644 --- a/internal/torznab/httpserver/httpserver.go +++ b/internal/torznab/httpserver/httpserver.go @@ -12,12 +12,14 @@ import ( "github.com/bitmagnet-io/bitmagnet/internal/torznab" "github.com/gin-gonic/gin" "go.uber.org/fx" + "go.uber.org/zap" ) type Params struct { fx.In Client lazy.Lazy[torznab.Client] Config torznab.Config + Log *zap.SugaredLogger } type Result struct { @@ -30,6 +32,7 @@ func New(p Params) Result { Option: builder{ client: p.Client, config: p.Config, + log: p.Log, }, } } @@ -37,6 +40,7 @@ func New(p Params) Result { type builder struct { client lazy.Lazy[torznab.Client] config torznab.Config + log *zap.SugaredLogger } func (builder) Key() string { @@ -46,6 +50,7 @@ func (builder) Key() string { type torznabworker struct { client torznab.Client profile torznab.Profile + log *zap.SugaredLogger } func (w torznabworker) writeInternalError(c *gin.Context, err error) { @@ -73,11 +78,10 @@ func (w torznabworker) writeErr(c *gin.Context, err error) { } } -func (w torznabworker) permaLinkBase() string { - return "/webui/torrents/permalink/" -} - func (w torznabworker) get(c *gin.Context) { + if w.profile.LogRequest { + w.log.Infof("[%s] %s", c.ClientIP(), c.Request.URL.RawQuery) + } tp := c.Query(torznab.ParamType) if tp == "" { w.writeErr(c, torznab.Error{ @@ -138,19 +142,16 @@ func (w torznabworker) get(c *gin.Context) { offset.Uint = uint(intOffset) } result, searchErr := w.client.Search(c, torznab.SearchRequest{ - Query: c.Query(torznab.ParamQuery), - Type: tp, - Cats: cats, - ImdbId: imdbId, - TmdbId: tmdbId, - Season: season, - Episode: episode, - Limit: limit, - Offset: offset, - OrderBy: w.profile.OrderBy, - OrderDirection: w.profile.OrderDirection, - Tags: w.profile.Tags, - PermaLinkBase: w.permaLinkBase(), + Query: c.Query(torznab.ParamQuery), + Type: tp, + Cats: cats, + ImdbId: imdbId, + TmdbId: tmdbId, + Season: season, + Episode: episode, + Limit: limit, + Offset: offset, + Profile: w.profile, }) if searchErr != nil { w.writeErr(c, fmt.Errorf("failed to search: %w", searchErr)) @@ -167,12 +168,11 @@ func (w torznabworker) getDefault(profile torznab.Profile) gin.HandlerFunc { func (w torznabworker) getWithProfile(profiles map[string]torznab.Profile) gin.HandlerFunc { handler := func(c *gin.Context) { - profileName := c.Param("profile") - profile, ok := profiles[profileName] + profile, ok := profiles[c.Param("profile")] if !ok { w.writeErr(c, torznab.Error{ Code: 200, - Description: fmt.Sprintf("profile not found (%s)", profileName), + Description: fmt.Sprintf("profile not found (%s)", c.Param("profile")), }) return } @@ -190,6 +190,7 @@ func (b builder) Apply(e *gin.Engine) error { } worker := torznabworker{ client: client, + log: b.log, } e.GET("/torznab/api/*any", worker.getDefault(b.config.DefaultProfile)) diff --git a/internal/torznab/request.go b/internal/torznab/request.go index c955c503..3693c7d2 100644 --- a/internal/torznab/request.go +++ b/internal/torznab/request.go @@ -1,24 +1,20 @@ package torznab import ( - "github.com/bitmagnet-io/bitmagnet/internal/database/search" "github.com/bitmagnet-io/bitmagnet/internal/model" ) type SearchRequest struct { - Query string - Type string - Cats []int - ImdbId model.NullString - TmdbId model.NullString - Season model.NullInt - Episode model.NullInt - Attrs []string - Extended bool - Limit model.NullUint - Offset model.NullUint - OrderBy search.TorrentContentOrderBy - OrderDirection search.OrderDirection - Tags []string - PermaLinkBase string + Query string + Type string + Cats []int + ImdbId model.NullString + TmdbId model.NullString + Season model.NullInt + Episode model.NullInt + Attrs []string + Extended bool + Limit model.NullUint + Offset model.NullUint + Profile Profile }