pcgamedb/utils/magnet.go

30 lines
574 B
Go
Raw Permalink Normal View History

2024-09-24 06:17:11 -04:00
package utils
import (
"bytes"
"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)
2024-09-24 06:17:11 -04:00
if size == 0 {
for _, file := range info.Files {
size += uint64(file.Length)
2024-09-24 06:17:11 -04:00
}
}
magnet, err := minfo.MagnetV2()
if err != nil {
return "", "", err
2024-09-24 06:17:11 -04:00
}
return magnet.String(), BytesToSize(size), nil
2024-09-24 06:17:11 -04:00
}