pcgamedb/utils/magnet.go
nite07 434dbb1dc2
All checks were successful
docker / prepare-and-build (push) Successful in 2m37s
release / goreleaser (push) Successful in 24m0s
refactor DeduplicateGameItems
update games.json
2024-11-22 01:30:26 +08:00

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
}