bt2qbt/pkg/helpers/helpers.go
Alexey Kostin 0b305dd2f9
Fix windows prohibited simbols (#46)
* handle windows fs prohibited symbols

* fix abs path symbols replace

* more readable code

* better replacer
new replacer function with tests

* return zero files from torrent info file tree if there are single file torrent

* don't harm torrent file function, handle filelist size in another places

* more accurate handling single files

* use mapped files for directories or torrent names with space on ending

* normalize backslases too

* Don't normalize cesu8\prohibited symbols on torrent name if torrent file contain several files. But normalize for single file torrent and normalize last space character for both

* Better torrent name and torrent file paths handling
helpers
tests

* move torrents functions to torrents functions
cache single flag

* update README.md

* fix deprecation warnings
2023-12-05 21:17:43 +03:00

128 lines
2.6 KiB
Go

package helpers
import (
"bufio"
"bytes"
"github.com/crazytyper/go-cesu8"
"github.com/zeebo/bencode"
"io"
"os"
"regexp"
"strconv"
"strings"
)
func ASCIIConvert(s string) string {
var buffer bytes.Buffer
for _, c := range s {
if c > 127 {
buffer.WriteString(`\x` + strconv.FormatUint(uint64(c), 16))
} else {
buffer.WriteString(string(c))
}
}
return buffer.String()
}
// CheckExists return true and string if string exists in array, else false and string
func CheckExists(s string, arr []string) (bool, string) {
for _, value := range arr {
if value == s {
return true, s
}
}
return false, s
}
func DecodeTorrentFile(path string, decodeTo interface{}) error {
dat, err := os.ReadFile(path)
if err != nil {
return err
}
if err := bencode.DecodeBytes(dat, &decodeTo); err != nil {
return err
}
return nil
}
func EncodeTorrentFile(path string, content interface{}) error {
var err error
var file *os.File
if _, err := os.Stat(path); os.IsNotExist(err) {
file, err = os.Create(path)
if err != nil {
panic(err)
return err
}
defer file.Close()
} else {
file, err = os.OpenFile(path, os.O_WRONLY, 0666)
if err != nil {
return err
}
defer file.Close()
}
bufferedWriter := bufio.NewWriter(file)
enc := bencode.NewEncoder(bufferedWriter)
if err = enc.Encode(content); err != nil {
return err
}
err = bufferedWriter.Flush()
if err := enc.Encode(content); err != nil {
return err
}
return nil
}
func CopyFile(src string, dst string) error {
originalFile, err := os.Open(src)
if err != nil {
return err
}
defer originalFile.Close()
newFile, err := os.Create(dst)
if err != nil {
return err
}
defer newFile.Close()
if _, err := io.Copy(newFile, originalFile); err != nil {
return err
}
if err := newFile.Sync(); err != nil {
return err
}
return nil
}
func GetStrings(trackers interface{}) []string {
ntrackers := []string{}
switch strct := trackers.(type) {
case []string:
for _, str := range strct {
ntrackers = append(ntrackers, strings.Fields(str)...)
}
case string:
ntrackers = append(ntrackers, strings.Fields(strct)...)
case []interface{}:
for _, st := range strct {
ntrackers = append(ntrackers, GetStrings(st)...)
}
}
return ntrackers
}
func HandleCesu8(str string) string {
if strings.Contains(str, "\xed\xa0") {
return cesu8.DecodeString([]byte(str))
}
return str
}
// ReplaceAllSymbols Replace all symbols in set to replacer
func ReplaceAllSymbols(str string, set string, replacer string) string {
re := regexp.MustCompilePOSIX(`[` + regexp.QuoteMeta(set) + `]`)
return re.ReplaceAllString(str, replacer)
}