]> Sergey Matveev's repositories - btrtrc.git/blob - spec.go
Add support for the x.pe magnet link parameter
[btrtrc.git] / spec.go
1 package torrent
2
3 import (
4         "github.com/anacrolix/torrent/metainfo"
5         "github.com/anacrolix/torrent/storage"
6 )
7
8 // Specifies a new torrent for adding to a client. There are helpers for magnet URIs and torrent
9 // metainfo files.
10 type TorrentSpec struct {
11         // The tiered tracker URIs.
12         Trackers  [][]string
13         InfoHash  metainfo.Hash
14         InfoBytes []byte
15         // The name to use if the Name field from the Info isn't available.
16         DisplayName string
17         Webseeds    []string
18         DhtNodes    []string
19         PeerAddrs   []string
20         // The combination of the "xs" and "as" fields in magnet links, for now.
21         Sources []string
22
23         // The chunk size to use for outbound requests. Defaults to 16KiB if not set.
24         ChunkSize int
25         Storage   storage.ClientImpl
26
27         // Whether to allow data download or upload
28         DisallowDataUpload   bool
29         DisallowDataDownload bool
30 }
31
32 func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
33         m, err := metainfo.ParseMagnetUri(uri)
34         if err != nil {
35                 return
36         }
37         spec = &TorrentSpec{
38                 Trackers:    [][]string{m.Trackers},
39                 DisplayName: m.DisplayName,
40                 InfoHash:    m.InfoHash,
41                 Webseeds:    m.Params["ws"],
42                 Sources:     append(m.Params["xs"], m.Params["as"]...),
43                 PeerAddrs:   m.Params["x.pe"], // BEP 9
44                 // TODO: What's the parameter for DHT nodes?
45         }
46         return
47 }
48
49 func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
50         info, err := mi.UnmarshalInfo()
51         if err != nil {
52                 panic(err)
53         }
54         return &TorrentSpec{
55                 Trackers:    mi.UpvertedAnnounceList(),
56                 InfoHash:    mi.HashInfoBytes(),
57                 InfoBytes:   mi.InfoBytes,
58                 DisplayName: info.Name,
59                 Webseeds:    mi.UrlList,
60                 DhtNodes: func() (ret []string) {
61                         ret = make([]string, len(mi.Nodes))
62                         for _, node := range mi.Nodes {
63                                 ret = append(ret, string(node))
64                         }
65                         return
66                 }(),
67         }
68 }