]> Sergey Matveev's repositories - btrtrc.git/blob - spec.go
fixup! [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         "github.com/anacrolix/torrent/webseed"
10 )
11
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.
17         Trackers [][]string
18         // TODO: Move into a "new" Torrent opt type.
19         InfoHash  metainfo.Hash
20         InfoBytes []byte
21         // The name to use if the Name field from the Info isn't available.
22         DisplayName string
23         Webseeds    []string
24         DhtNodes    []string
25         PeerAddrs   []string
26         // The combination of the "xs" and "as" fields in magnet links, for now.
27         Sources []string
28
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.
31         ChunkSize pp.Integer
32         // TODO: Move into a "new" Torrent opt type.
33         Storage storage.ClientImpl
34
35         DisableInitialPieceCheck bool
36
37         // Whether to allow data download or upload
38         DisallowDataUpload   bool
39         DisallowDataDownload bool
40
41         // Custom encoder for webseed URLs
42         // Leave nil to use the default (url.QueryEscape)
43         DefaultWebseedEscapePath webseed.PathEscaper
44 }
45
46 func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
47         m, err := metainfo.ParseMagnetUri(uri)
48         if err != nil {
49                 return
50         }
51         spec = &TorrentSpec{
52                 Trackers:    [][]string{m.Trackers},
53                 DisplayName: m.DisplayName,
54                 InfoHash:    m.InfoHash,
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?
59         }
60         return
61 }
62
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()
67         if err != nil {
68                 err = fmt.Errorf("unmarshalling info: %w", err)
69         }
70         return &TorrentSpec{
71                 Trackers:    mi.UpvertedAnnounceList(),
72                 InfoHash:    mi.HashInfoBytes(),
73                 InfoBytes:   mi.InfoBytes,
74                 DisplayName: info.Name,
75                 Webseeds:    mi.UrlList,
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))
80                         }
81                         return
82                 }(),
83         }, err
84 }
85
86 // Panics if there was anything missing from the metainfo.
87 func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
88         ts, err := TorrentSpecFromMetaInfoErr(mi)
89         if err != nil {
90                 panic(err)
91         }
92         return ts
93 }