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 } var size uint64 = uint64(info.Length) if size == 0 { for _, file := range info.Files { size += uint64(file.Length) } } magnet, err := minfo.MagnetV2() if err != nil { return "", "", err } return magnet.String(), BytesToSize(size), nil } func SubSizeStrings(sizes []string) (string, error) { size := uint64(0) 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 } size += uint64(addSize * 1024 * 1024 * 1024) } 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 } size += uint64(addSize * 1024 * 1024) } 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 } size += uint64(addSize * 1024) } } return BytesToSize(size), nil }