6 "github.com/anacrolix/torrent/metainfo"
7 pp "github.com/anacrolix/torrent/peer_protocol"
8 "github.com/anacrolix/torrent/storage"
11 // Specifies a new torrent for adding to a client, or additions to an existing Torrent. There are
12 // constructor functions for magnet URIs and torrent metainfo files. TODO: This type should be
13 // dismantled into a new Torrent option type, and separate Torrent mutate method(s).
14 type TorrentSpec struct {
15 // The tiered tracker URIs.
17 // TODO: Move into a "new" Torrent opt type.
18 InfoHash metainfo.Hash
20 // The name to use if the Name field from the Info isn't available.
25 // The combination of the "xs" and "as" fields in magnet links, for now.
28 // The chunk size to use for outbound requests. Defaults to 16KiB if not set. Can only be set
29 // for new Torrents. TODO: Move into a "new" Torrent opt type.
31 // TODO: Move into a "new" Torrent opt type.
32 Storage storage.ClientImpl
34 DisableInitialPieceCheck bool
36 // Whether to allow data download or upload
37 DisallowDataUpload bool
38 DisallowDataDownload bool
41 func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
42 m, err := metainfo.ParseMagnetUri(uri)
47 Trackers: [][]string{m.Trackers},
48 DisplayName: m.DisplayName,
50 Webseeds: m.Params["ws"],
51 Sources: append(m.Params["xs"], m.Params["as"]...),
52 PeerAddrs: m.Params["x.pe"], // BEP 9
53 // TODO: What's the parameter for DHT nodes?
58 func TorrentSpecFromMetaInfoErr(mi *metainfo.MetaInfo) (*TorrentSpec, error) {
59 info, err := mi.UnmarshalInfo()
61 return nil, fmt.Errorf("unmarshalling info: %w", err)
64 Trackers: mi.UpvertedAnnounceList(),
65 InfoHash: mi.HashInfoBytes(),
66 InfoBytes: mi.InfoBytes,
67 DisplayName: info.Name,
69 DhtNodes: func() (ret []string) {
70 ret = make([]string, 0, len(mi.Nodes))
71 for _, node := range mi.Nodes {
72 ret = append(ret, string(node))
79 func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
80 ts, err := TorrentSpecFromMetaInfoErr(mi)