6 "github.com/anacrolix/torrent/metainfo"
7 "github.com/anacrolix/torrent/storage"
10 // Specifies a new torrent for adding to a client. There are helpers for magnet URIs and torrent
12 type TorrentSpec struct {
13 // The tiered tracker URIs.
15 InfoHash metainfo.Hash
17 // The name to use if the Name field from the Info isn't available.
22 // The combination of the "xs" and "as" fields in magnet links, for now.
25 // The chunk size to use for outbound requests. Defaults to 16KiB if not set.
27 DisableInitialPieceCheck bool
28 Storage storage.ClientImpl
30 // Whether to allow data download or upload
31 DisallowDataUpload bool
32 DisallowDataDownload bool
35 func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
36 m, err := metainfo.ParseMagnetUri(uri)
41 Trackers: [][]string{m.Trackers},
42 DisplayName: m.DisplayName,
44 Webseeds: m.Params["ws"],
45 Sources: append(m.Params["xs"], m.Params["as"]...),
46 PeerAddrs: m.Params["x.pe"], // BEP 9
47 // TODO: What's the parameter for DHT nodes?
52 func TorrentSpecFromMetaInfoErr(mi *metainfo.MetaInfo) (*TorrentSpec, error) {
53 info, err := mi.UnmarshalInfo()
55 return nil, fmt.Errorf("unmarshalling info: %w", err)
58 Trackers: mi.UpvertedAnnounceList(),
59 InfoHash: mi.HashInfoBytes(),
60 InfoBytes: mi.InfoBytes,
61 DisplayName: info.Name,
63 DhtNodes: func() (ret []string) {
64 ret = make([]string, 0, len(mi.Nodes))
65 for _, node := range mi.Nodes {
66 ret = append(ret, string(node))
73 func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
74 ts, err := TorrentSpecFromMetaInfoErr(mi)