This commit is contained in:
yourok
2018-08-29 12:33:14 +03:00
commit 0ca43a2c4d
54 changed files with 5669 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
package utils
import (
"sync"
)
func ParallelFor(begin, end int, fn func(i int)) {
var wg sync.WaitGroup
wg.Add(end - begin)
for i := begin; i < end; i++ {
go func(i int) {
fn(i)
wg.Done()
}(i)
}
wg.Wait()
}

View File

@@ -0,0 +1,74 @@
package utils
import (
"encoding/base32"
"errors"
"math/rand"
"time"
"server/settings"
"github.com/anacrolix/torrent"
"golang.org/x/time/rate"
)
var trackers = []string{
"http://bt4.t-ru.org/ann?magnet",
"http://retracker.mgts.by:80/announce",
"http://tracker.city9x.com:2710/announce",
"http://tracker.electro-torrent.pl:80/announce",
"http://tracker.internetwarriors.net:1337/announce",
"http://tracker2.itzmx.com:6961/announce",
"udp://46.148.18.250:2710",
"udp://opentor.org:2710",
"udp://public.popcorn-tracker.org:6969/announce",
"udp://tracker.opentrackr.org:1337/announce",
"http://bt.svao-ix.ru/announce",
}
func GetDefTrackers() []string {
return trackers
}
func PeerIDRandom(peer string) string {
randomBytes := make([]byte, 32)
_, err := rand.Read(randomBytes)
if err != nil {
panic(err)
}
return peer + base32.StdEncoding.EncodeToString(randomBytes)[:20-len(peer)]
}
func GotInfo(t *torrent.Torrent, timeout int) error {
gi := t.GotInfo()
select {
case <-gi:
return nil
case <-time.Tick(time.Second * time.Duration(timeout)):
return errors.New("timeout load torrent info")
}
}
func GetReadahead() int64 {
readahead := int64(float64(settings.Get().CacheSize) * 0.33)
if readahead < 66*1024*1024 {
readahead = int64(settings.Get().CacheSize)
if readahead > 66*1024*1024 {
readahead = 66 * 1024 * 1024
}
}
return readahead
}
func Limit(i int) *rate.Limiter {
l := rate.NewLimiter(rate.Inf, 0)
if i > 0 {
b := i
if b < 16*1024 {
b = 16 * 1024
}
l = rate.NewLimiter(rate.Limit(i), b)
}
return l
}

View File

@@ -0,0 +1,33 @@
package utils
import (
"encoding/json"
"fmt"
"server/settings"
)
func AddInfo(hash, js string) {
info := settings.GetInfo(hash)
if info != "{}" {
var jsset map[string]interface{}
var err error
if err = json.Unmarshal([]byte(js), &jsset); err == nil {
var jsdb map[string]interface{}
if err = json.Unmarshal([]byte(info), &jsdb); err == nil {
for k, v := range jsset {
jsdb[k] = v
}
jsstr, err := json.Marshal(jsdb)
if err == nil {
settings.AddInfo(hash, string(jsstr))
return
}
}
}
if err != nil {
fmt.Println(err)
}
}
settings.AddInfo(hash, js)
}

78
src/server/utils/Utils.go Normal file
View File

@@ -0,0 +1,78 @@
package utils
import (
"fmt"
"regexp"
"runtime"
"runtime/debug"
"strconv"
"strings"
)
func CleanFName(file string) string {
re := regexp.MustCompile(`[ !*'();:@&=+$,/?#\[\]~"]`)
ret := re.ReplaceAllString(file, `_`)
ret = strings.Replace(ret, "__", "_", -1)
return ret
}
func FreeOSMem() {
debug.FreeOSMemory()
}
func FreeOSMemGC() {
runtime.GC()
debug.FreeOSMemory()
}
const (
_ = 1.0 << (10 * iota) // ignore first value by assigning to blank identifier
KB
MB
GB
TB
PB
EB
)
func Format(b float64) string {
multiple := ""
value := b
switch {
case b >= EB:
value /= EB
multiple = "EB"
case b >= PB:
value /= PB
multiple = "PB"
case b >= TB:
value /= TB
multiple = "TB"
case b >= GB:
value /= GB
multiple = "GB"
case b >= MB:
value /= MB
multiple = "MB"
case b >= KB:
value /= KB
multiple = "KB"
case b == 0:
return "0"
default:
return strconv.FormatInt(int64(b), 10) + "B"
}
return fmt.Sprintf("%.2f%s", value, multiple)
}
//
//func IsCyrillic(str string) bool {
// for _, r := range str {
// if unicode.Is(unicode.Cyrillic, r) {
// return true
// }
// }
// return false
//}