2024-09-24 06:17:11 -04:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ConvertTorrentToMagnet(torrent []byte) (string, string, error) {
|
|
|
|
minfo, err := metainfo.Load(bytes.NewReader(torrent))
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
info, err := minfo.UnmarshalInfo()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2024-11-18 07:21:08 -05:00
|
|
|
var size uint64 = uint64(info.Length)
|
2024-09-24 06:17:11 -04:00
|
|
|
if size == 0 {
|
|
|
|
for _, file := range info.Files {
|
2024-11-18 07:21:08 -05:00
|
|
|
size += uint64(file.Length)
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|
|
|
|
}
|
2024-11-18 07:21:08 -05:00
|
|
|
magnet, err := minfo.MagnetV2()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|
2024-11-18 07:21:08 -05:00
|
|
|
return magnet.String(), BytesToSize(size), nil
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func SubSizeStrings(sizes []string) (string, error) {
|
2024-11-18 07:21:08 -05:00
|
|
|
size := uint64(0)
|
2024-09-24 06:17:11 -04:00
|
|
|
for _, sizeStr := range sizes {
|
|
|
|
sizeStr := strings.ToLower(sizeStr)
|
|
|
|
if strings.Contains(sizeStr, "gb") {
|
|
|
|
sizeStr = strings.ReplaceAll(sizeStr, "gb", "")
|
|
|
|
sizeStr = strings.TrimSpace(sizeStr)
|
|
|
|
addSize, err := strconv.ParseFloat(sizeStr, 64)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-11-18 07:21:08 -05:00
|
|
|
size += uint64(addSize * 1024 * 1024 * 1024)
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|
|
|
|
if strings.Contains(sizeStr, "mb") {
|
|
|
|
sizeStr = strings.ReplaceAll(sizeStr, "mb", "")
|
|
|
|
sizeStr = strings.TrimSpace(sizeStr)
|
|
|
|
addSize, err := strconv.ParseFloat(sizeStr, 64)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-11-18 07:21:08 -05:00
|
|
|
size += uint64(addSize * 1024 * 1024)
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|
|
|
|
if strings.Contains(sizeStr, "kb") {
|
|
|
|
sizeStr = strings.ReplaceAll(sizeStr, "kb", "")
|
|
|
|
sizeStr = strings.TrimSpace(sizeStr)
|
|
|
|
addSize, err := strconv.ParseFloat(sizeStr, 64)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-11-18 07:21:08 -05:00
|
|
|
size += uint64(addSize * 1024)
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|
|
|
|
}
|
2024-11-18 07:21:08 -05:00
|
|
|
return BytesToSize(size), nil
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|