30 lines
567 B
Go
30 lines
567 B
Go
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(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
|
|
}
|