6 "github.com/anacrolix/torrent/metainfo"
7 pp "github.com/anacrolix/torrent/peer_protocol"
8 "github.com/anacrolix/torrent/storage"
9 "github.com/anacrolix/torrent/webseed"
12 // Specifies a new torrent for adding to a client, or additions to an existing Torrent. There are
13 // constructor functions for magnet URIs and torrent metainfo files. TODO: This type should be
14 // dismantled into a new Torrent option type, and separate Torrent mutate method(s).
15 type TorrentSpec struct {
16 // The tiered tracker URIs.
18 // TODO: Move into a "new" Torrent opt type.
19 InfoHash metainfo.Hash
21 // The name to use if the Name field from the Info isn't available.
26 // The combination of the "xs" and "as" fields in magnet links, for now.
29 // The chunk size to use for outbound requests. Defaults to 16KiB if not set. Can only be set
30 // for new Torrents. TODO: Move into a "new" Torrent opt type.
32 // TODO: Move into a "new" Torrent opt type.
33 Storage storage.ClientImpl
35 DisableInitialPieceCheck bool
37 // Whether to allow data download or upload
38 DisallowDataUpload bool
39 DisallowDataDownload bool
41 // Custom encoder for webseed URLs
42 // Leave nil to use the default (url.QueryEscape)
43 DefaultWebseedEscapePath webseed.PathEscaper
46 func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
47 m, err := metainfo.ParseMagnetUri(uri)
52 Trackers: [][]string{m.Trackers},
53 DisplayName: m.DisplayName,
55 Webseeds: m.Params["ws"],
56 Sources: append(m.Params["xs"], m.Params["as"]...),
57 PeerAddrs: m.Params["x.pe"], // BEP 9
58 // TODO: What's the parameter for DHT nodes?
63 // The error will be from unmarshalling the info bytes. The TorrentSpec is still filled out as much
64 // as possible in this case.
65 func TorrentSpecFromMetaInfoErr(mi *metainfo.MetaInfo) (*TorrentSpec, error) {
66 info, err := mi.UnmarshalInfo()
68 err = fmt.Errorf("unmarshalling info: %w", err)
71 Trackers: mi.UpvertedAnnounceList(),
72 InfoHash: mi.HashInfoBytes(),
73 InfoBytes: mi.InfoBytes,
74 DisplayName: info.Name,
76 DhtNodes: func() (ret []string) {
77 ret = make([]string, 0, len(mi.Nodes))
78 for _, node := range mi.Nodes {
79 ret = append(ret, string(node))
86 // Panics if there was anything missing from the metainfo.
87 func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
88 ts, err := TorrentSpecFromMetaInfoErr(mi)