]> Sergey Matveev's repositories - btrtrc.git/blob - spec.go
[webseed] Add a custom URL encoder for webseeds
[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         Webseeds    []string
23         DhtNodes    []string
24         PeerAddrs   []string
25         // The combination of the "xs" and "as" fields in magnet links, for now.
26         Sources []string
27
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.
30         ChunkSize pp.Integer
31         // TODO: Move into a "new" Torrent opt type.
32         Storage storage.ClientImpl
33
34         DisableInitialPieceCheck bool
35
36         // Whether to allow data download or upload
37         DisallowDataUpload   bool
38         DisallowDataDownload bool
39
40         // Custom encoder for webseed URLs
41         // Leave nil to use the default (url.QueryEscape)
42         EncodeWebSeedUrl func(string) string
43 }
44
45 func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
46         m, err := metainfo.ParseMagnetUri(uri)
47         if err != nil {
48                 return
49         }
50         spec = &TorrentSpec{
51                 Trackers:    [][]string{m.Trackers},
52                 DisplayName: m.DisplayName,
53                 InfoHash:    m.InfoHash,
54                 Webseeds:    m.Params["ws"],
55                 Sources:     append(m.Params["xs"], m.Params["as"]...),
56                 PeerAddrs:   m.Params["x.pe"], // BEP 9
57                 // TODO: What's the parameter for DHT nodes?
58         }
59         return
60 }
61
62 // The error will be from unmarshalling the info bytes. The TorrentSpec is still filled out as much
63 // as possible in this case.
64 func TorrentSpecFromMetaInfoErr(mi *metainfo.MetaInfo) (*TorrentSpec, error) {
65         info, err := mi.UnmarshalInfo()
66         if err != nil {
67                 err = fmt.Errorf("unmarshalling info: %w", err)
68         }
69         return &TorrentSpec{
70                 Trackers:    mi.UpvertedAnnounceList(),
71                 InfoHash:    mi.HashInfoBytes(),
72                 InfoBytes:   mi.InfoBytes,
73                 DisplayName: info.Name,
74                 Webseeds:    mi.UrlList,
75                 DhtNodes: func() (ret []string) {
76                         ret = make([]string, 0, len(mi.Nodes))
77                         for _, node := range mi.Nodes {
78                                 ret = append(ret, string(node))
79                         }
80                         return
81                 }(),
82         }, err
83 }
84
85 // Panics if there was anything missing from the metainfo.
86 func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
87         ts, err := TorrentSpecFromMetaInfoErr(mi)
88         if err != nil {
89                 panic(err)
90         }
91         return ts
92 }