refactor and to go mod

This commit is contained in:
YouROK
2021-02-18 16:56:55 +03:00
parent 0e49a98626
commit 94f212fa75
50 changed files with 13 additions and 29 deletions

89
server/utils/filetypes.go Normal file
View File

@@ -0,0 +1,89 @@
package utils
import (
"path/filepath"
"server/torr/state"
)
var extVideo = map[string]interface{}{
".3g2": nil,
".3gp": nil,
".aaf": nil,
".asf": nil,
".avchd": nil,
".avi": nil,
".drc": nil,
".flv": nil,
".m2ts": nil,
".ts": nil,
".m2v": nil,
".m4p": nil,
".m4v": nil,
".mkv": nil,
".mng": nil,
".mov": nil,
".mp2": nil,
".mp4": nil,
".mpe": nil,
".mpeg": nil,
".mpg": nil,
".mpv": nil,
".mxf": nil,
".nsv": nil,
".ogg": nil,
".ogv": nil,
".qt": nil,
".rm": nil,
".rmvb": nil,
".roq": nil,
".svi": nil,
".vob": nil,
".webm": nil,
".wmv": nil,
".yuv": nil,
}
var extAudio = map[string]interface{}{
".aac": nil,
".aiff": nil,
".ape": nil,
".au": nil,
".flac": nil,
".gsm": nil,
".it": nil,
".m3u": nil,
".m4a": nil,
".mid": nil,
".mod": nil,
".mp3": nil,
".mpa": nil,
".pls": nil,
".ra": nil,
".s3m": nil,
".sid": nil,
".wav": nil,
".wma": nil,
".xm": nil,
}
func GetMimeType(filename string) string {
ext := filepath.Ext(filename)
if _, ok := extVideo[ext]; ok {
return "video/*"
}
if _, ok := extAudio[ext]; ok {
return "audio/*"
}
return "*/*"
}
func GetPlayableFiles(st state.TorrentStatus) []*state.TorrentFileStat {
files := make([]*state.TorrentFileStat, 0)
for _, f := range st.FileStats {
if GetMimeType(f.Path) != "*/*" {
files = append(files, f)
}
}
return files
}

17
server/utils/prallel.go Normal file
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()
}

48
server/utils/strings.go Normal file
View File

@@ -0,0 +1,48 @@
package utils
import (
"fmt"
"strconv"
)
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)
}