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 Storage storage.ClientImpl
29 // Whether to allow data download or upload
30 DisallowDataUpload bool
31 DisallowDataDownload bool
34 func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
35 m, err := metainfo.ParseMagnetUri(uri)
40 Trackers: [][]string{m.Trackers},
41 DisplayName: m.DisplayName,
43 Webseeds: m.Params["ws"],
44 Sources: append(m.Params["xs"], m.Params["as"]...),
45 PeerAddrs: m.Params["x.pe"], // BEP 9
46 // TODO: What's the parameter for DHT nodes?
51 func TorrentSpecFromMetaInfoErr(mi *metainfo.MetaInfo) (*TorrentSpec, error) {
52 info, err := mi.UnmarshalInfo()
54 return nil, fmt.Errorf("unmarshalling info: %w", err)
57 Trackers: mi.UpvertedAnnounceList(),
58 InfoHash: mi.HashInfoBytes(),
59 InfoBytes: mi.InfoBytes,
60 DisplayName: info.Name,
62 DhtNodes: func() (ret []string) {
63 ret = make([]string, len(mi.Nodes))
64 for _, node := range mi.Nodes {
65 ret = append(ret, string(node))
72 func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
73 ts, err := TorrentSpecFromMetaInfoErr(mi)