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.
22 // WebSeed URLs. For additional options add the URLs separately with Torrent.AddWebSeeds
27 // The combination of the "xs" and "as" fields in magnet links, for now.
30 // The chunk size to use for outbound requests. Defaults to 16KiB if not set. Can only be set
31 // for new Torrents. TODO: Move into a "new" Torrent opt type.
33 // TODO: Move into a "new" Torrent opt type.
34 Storage storage.ClientImpl
36 DisableInitialPieceCheck bool
38 // Whether to allow data download or upload
39 DisallowDataUpload bool
40 DisallowDataDownload bool
43 func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
44 m, err := metainfo.ParseMagnetUri(uri)
49 Trackers: [][]string{m.Trackers},
50 DisplayName: m.DisplayName,
52 Webseeds: m.Params["ws"],
53 Sources: append(m.Params["xs"], m.Params["as"]...),
54 PeerAddrs: m.Params["x.pe"], // BEP 9
55 // TODO: What's the parameter for DHT nodes?
60 // The error will be from unmarshalling the info bytes. The TorrentSpec is still filled out as much
61 // as possible in this case.
62 func TorrentSpecFromMetaInfoErr(mi *metainfo.MetaInfo) (*TorrentSpec, error) {
63 info, err := mi.UnmarshalInfo()
65 err = fmt.Errorf("unmarshalling info: %w", err)
68 Trackers: mi.UpvertedAnnounceList(),
69 InfoHash: mi.HashInfoBytes(),
70 InfoBytes: mi.InfoBytes,
71 DisplayName: info.Name,
73 DhtNodes: func() (ret []string) {
74 ret = make([]string, 0, len(mi.Nodes))
75 for _, node := range mi.Nodes {
76 ret = append(ret, string(node))
83 // Panics if there was anything missing from the metainfo.
84 func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
85 ts, err := TorrentSpecFromMetaInfoErr(mi)