]> Sergey Matveev's repositories - btrtrc.git/blob - spec.go
Drop support for go 1.20
[btrtrc.git] / spec.go
1 package torrent
2
3 import (
4         "fmt"
5
6         "github.com/anacrolix/torrent/metainfo"
7         pp "github.com/anacrolix/torrent/peer_protocol"
8         "github.com/anacrolix/torrent/storage"
9 )
10
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.
16         Trackers [][]string
17         // TODO: Move into a "new" Torrent opt type.
18         InfoHash  metainfo.Hash
19         InfoBytes []byte
20         // The name to use if the Name field from the Info isn't available.
21         DisplayName string
22         // WebSeed URLs. For additional options add the URLs separately with Torrent.AddWebSeeds
23         // instead.
24         Webseeds  []string
25         DhtNodes  []string
26         PeerAddrs []string
27         // The combination of the "xs" and "as" fields in magnet links, for now.
28         Sources []string
29
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.
32         ChunkSize pp.Integer
33         // TODO: Move into a "new" Torrent opt type.
34         Storage storage.ClientImpl
35
36         DisableInitialPieceCheck bool
37
38         // Whether to allow data download or upload
39         DisallowDataUpload   bool
40         DisallowDataDownload bool
41 }
42
43 func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
44         m, err := metainfo.ParseMagnetUri(uri)
45         if err != nil {
46                 return
47         }
48         spec = &TorrentSpec{
49                 Trackers:    [][]string{m.Trackers},
50                 DisplayName: m.DisplayName,
51                 InfoHash:    m.InfoHash,
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?
56         }
57         return
58 }
59
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()
64         if err != nil {
65                 err = fmt.Errorf("unmarshalling info: %w", err)
66         }
67         return &TorrentSpec{
68                 Trackers:    mi.UpvertedAnnounceList(),
69                 InfoHash:    mi.HashInfoBytes(),
70                 InfoBytes:   mi.InfoBytes,
71                 DisplayName: info.Name,
72                 Webseeds:    mi.UrlList,
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))
77                         }
78                         return
79                 }(),
80         }, err
81 }
82
83 // Panics if there was anything missing from the metainfo.
84 func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
85         ts, err := TorrentSpecFromMetaInfoErr(mi)
86         if err != nil {
87                 panic(err)
88         }
89         return ts
90 }