]> Sergey Matveev's repositories - btrtrc.git/blob - spec.go
Tidy up doc, file names, naming
[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         // The combination of the "xs" and "as" fields in magnet links, for now.
20         Sources []string
21
22         // The chunk size to use for outbound requests. Defaults to 16KiB if not set.
23         ChunkSize int
24         Storage   storage.ClientImpl
25
26         // Whether to allow data download or upload
27         DisallowDataUpload   bool
28         DisallowDataDownload bool
29 }
30
31 func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
32         m, err := metainfo.ParseMagnetUri(uri)
33         if err != nil {
34                 return
35         }
36         spec = &TorrentSpec{
37                 Trackers:    [][]string{m.Trackers},
38                 DisplayName: m.DisplayName,
39                 InfoHash:    m.InfoHash,
40                 Webseeds:    m.Params["ws"],
41                 Sources:     append(m.Params["xs"], m.Params["as"]...),
42                 // TODO: What's the parameter for DHT nodes or bootstrap peers in a magnet link?
43         }
44         return
45 }
46
47 func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
48         info, err := mi.UnmarshalInfo()
49         if err != nil {
50                 panic(err)
51         }
52         return &TorrentSpec{
53                 Trackers:    mi.UpvertedAnnounceList(),
54                 InfoHash:    mi.HashInfoBytes(),
55                 InfoBytes:   mi.InfoBytes,
56                 DisplayName: info.Name,
57                 Webseeds:    mi.UrlList,
58                 DhtNodes: func() (ret []string) {
59                         ret = make([]string, len(mi.Nodes))
60                         for _, node := range mi.Nodes {
61                                 ret = append(ret, string(node))
62                         }
63                         return
64                 }(),
65         }
66 }