2017-05-10 12:03:49 +03:00
package common
2017-05-14 19:31:17 -04:00
import (
2017-06-20 02:06:01 +02:00
"math"
"time"
2017-06-13 08:31:11 -03:00
humanize "github.com/dustin/go-humanize"
2017-05-14 19:31:17 -04:00
"strconv"
"strings"
2017-06-18 00:30:12 +02:00
"github.com/NyaaPantsu/nyaa/config"
2017-06-18 02:11:58 +02:00
catUtil "github.com/NyaaPantsu/nyaa/util/categories"
2017-05-14 19:31:17 -04:00
)
2017-05-10 12:03:49 +03:00
type Status uint8
const (
2017-06-20 21:59:00 -04:00
ShowAll Status = 0
FilterRemakes = 2
Trusted = 3
APlus = 4
2017-05-10 12:03:49 +03:00
)
2017-05-25 19:48:14 -04:00
func ( st * Status ) ToString ( ) string {
switch * st {
case FilterRemakes :
return "2"
2017-06-20 21:59:00 -04:00
case Trusted :
2017-05-25 19:48:14 -04:00
return "3"
2017-06-20 21:59:00 -04:00
case APlus :
return "4"
2017-05-25 19:48:14 -04:00
}
return ""
}
2017-05-14 19:31:17 -04:00
func ( st * Status ) Parse ( s string ) {
switch s {
case "2" :
2017-06-20 21:59:00 -04:00
* st = FilterRemakes
2017-05-14 19:31:17 -04:00
case "3" :
2017-06-20 21:59:00 -04:00
* st = Trusted
case "4" :
2017-05-14 19:31:17 -04:00
* st = APlus
default :
* st = ShowAll
}
}
2017-05-10 12:03:49 +03:00
type SortMode uint8
const (
ID SortMode = iota
Name
Date
Downloads
Size
2017-05-11 07:40:50 -04:00
Seeders
Leechers
Completed
2017-05-10 12:03:49 +03:00
)
2017-05-14 19:31:17 -04:00
func ( s * SortMode ) Parse ( str string ) {
switch str {
case "1" :
* s = Name
case "2" :
* s = Date
case "3" :
* s = Downloads
case "4" :
* s = Size
case "5" :
* s = Seeders
case "6" :
* s = Leechers
case "7" :
* s = Completed
default :
2017-06-20 21:28:56 -04:00
* s = Date
2017-05-14 19:31:17 -04:00
}
}
2017-05-25 19:48:14 -04:00
/ * INFO Always need to keep in sync with the field that are used in the
* elasticsearch index .
* TODO Verify the field in postgres database
* /
func ( s * SortMode ) ToESField ( ) string {
switch * s {
case ID :
return "id"
case Name :
2017-05-31 22:04:49 -04:00
return "name.raw"
2017-05-25 19:48:14 -04:00
case Date :
return "date"
case Downloads :
return "downloads"
case Size :
return "filesize"
case Seeders :
return "seeders"
case Leechers :
return "leechers"
case Completed :
return "completed"
}
return "id"
}
2017-06-13 08:31:11 -03:00
func ( s * SortMode ) ToDBField ( ) string {
switch * s {
case ID :
return "torrent_id"
case Name :
return "torrent_name"
case Date :
return "date"
case Downloads :
return "downloads"
case Size :
return "filesize"
case Seeders :
return "seeders"
case Leechers :
return "leechers"
case Completed :
return "completed"
}
2017-06-18 00:30:12 +02:00
return config . Conf . Torrents . Order
2017-06-13 08:31:11 -03:00
}
2017-05-10 12:03:49 +03:00
type Category struct {
Main , Sub uint8
}
func ( c Category ) String ( ) ( s string ) {
if c . Main != 0 {
s += strconv . Itoa ( int ( c . Main ) )
}
s += "_"
if c . Sub != 0 {
s += strconv . Itoa ( int ( c . Sub ) )
}
return
}
2017-05-14 19:31:17 -04:00
func ( c Category ) IsSet ( ) bool {
2017-05-25 19:48:14 -04:00
return c . IsMainSet ( ) && c . IsSubSet ( )
}
func ( c Category ) IsMainSet ( ) bool {
return c . Main != 0
}
func ( c Category ) IsSubSet ( ) bool {
return c . Sub != 0
2017-05-14 19:31:17 -04:00
}
// Parse sets category by string
// returns true if string is valid otherwise returns false
2017-06-18 00:30:12 +02:00
func ParseCategories ( s string ) [ ] * Category {
if s != "" {
parts := strings . Split ( s , "," )
2017-06-18 02:11:58 +02:00
var categories [ ] * Category
for _ , val := range parts {
2017-06-18 00:30:12 +02:00
partsCat := strings . Split ( val , "_" )
if len ( partsCat ) == 2 {
tmp , err := strconv . ParseUint ( partsCat [ 0 ] , 10 , 8 )
if err == nil {
c := uint8 ( tmp )
tmp , err = strconv . ParseUint ( partsCat [ 1 ] , 10 , 8 )
var sub uint8
if err == nil {
sub = uint8 ( tmp )
}
2017-06-18 02:11:58 +02:00
if catUtil . CategoryExists ( partsCat [ 0 ] + "_" + partsCat [ 1 ] ) {
categories = append ( categories , & Category {
Main : c ,
Sub : sub ,
} )
2017-06-18 00:30:12 +02:00
}
}
2017-05-14 19:31:17 -04:00
}
}
2017-06-18 00:30:12 +02:00
return categories
2017-05-14 19:31:17 -04:00
}
2017-06-18 00:30:12 +02:00
return Categories { }
2017-05-14 19:31:17 -04:00
}
2017-06-13 08:31:11 -03:00
type SizeBytes uint64
2017-06-20 02:06:01 +02:00
func ( sz * SizeBytes ) Parse ( s string , sizeType string ) bool {
if s == "" {
* sz = 0
return false
}
var multiplier uint64
switch sizeType {
case "b" :
multiplier = 1
case "k" :
multiplier = uint64 ( math . Exp2 ( 10 ) )
case "m" :
multiplier = uint64 ( math . Exp2 ( 20 ) )
case "g" :
multiplier = uint64 ( math . Exp2 ( 30 ) )
}
2017-06-13 08:31:11 -03:00
size64 , err := humanize . ParseBytes ( s )
if err != nil {
* sz = 0
return false
}
2017-06-20 02:06:01 +02:00
* sz = SizeBytes ( size64 * multiplier )
return true
}
type DateFilter string
func ( d * DateFilter ) Parse ( s string , dateType string ) bool {
if s == "" {
* d = ""
return false
}
dateInt , err := strconv . Atoi ( s )
if err != nil {
* d = ""
return false
}
switch dateType {
case "m" :
* d = DateFilter ( time . Now ( ) . AddDate ( 0 , - dateInt , 0 ) . Format ( "2006-01-02" ) )
case "y" :
* d = DateFilter ( time . Now ( ) . AddDate ( - dateInt , 0 , 0 ) . Format ( "2006-01-02" ) )
default :
* d = DateFilter ( time . Now ( ) . AddDate ( 0 , 0 , - dateInt ) . Format ( "2006-01-02" ) )
}
2017-06-13 08:31:11 -03:00
return true
}
2017-06-18 00:30:12 +02:00
type Categories [ ] * Category
2017-05-14 19:31:17 -04:00
// deprecated for TorrentParam
2017-05-10 12:03:49 +03:00
type SearchParam struct {
2017-05-30 00:28:21 +02:00
TorrentID uint
2017-05-30 14:12:42 +02:00
FromID uint // Search for torrentID > FromID
2017-05-30 00:28:21 +02:00
Order bool // True means acsending
2017-06-21 03:58:54 +02:00
Hidden bool // True means filter hidden torrents
2017-05-30 00:28:21 +02:00
Status Status
Sort SortMode
2017-06-18 00:30:12 +02:00
Category Categories
2017-06-20 02:06:01 +02:00
FromDate DateFilter
ToDate DateFilter
2017-05-30 00:28:21 +02:00
Page int
UserID uint
Max uint
NotNull string
2017-06-11 20:14:26 -03:00
Language string
2017-06-13 08:31:11 -03:00
MinSize SizeBytes
MaxSize SizeBytes
2017-05-30 00:28:21 +02:00
Query string
2017-05-10 12:03:49 +03:00
}