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 // The error will be from unmarshalling the info bytes. The TorrentSpec is still filled out as much
59 // as possible in this case.
60 func TorrentSpecFromMetaInfoErr(mi *metainfo.MetaInfo) (*TorrentSpec, error) {
61 info, err := mi.UnmarshalInfo()
63 err = fmt.Errorf("unmarshalling info: %w", err)
66 Trackers: mi.UpvertedAnnounceList(),
67 InfoHash: mi.HashInfoBytes(),
68 InfoBytes: mi.InfoBytes,
69 DisplayName: info.Name,
71 DhtNodes: func() (ret []string) {
72 ret = make([]string, 0, len(mi.Nodes))
73 for _, node := range mi.Nodes {
74 ret = append(ret, string(node))
81 // Panics if there was anything missing from the metainfo.
82 func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
83 ts, err := TorrentSpecFromMetaInfoErr(mi)