]> Sergey Matveev's repositories - btrtrc.git/blob - spec.go
Only allow chunk size to be set for new Torrents
[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
41 func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
42         m, err := metainfo.ParseMagnetUri(uri)
43         if err != nil {
44                 return
45         }
46         spec = &TorrentSpec{
47                 Trackers:    [][]string{m.Trackers},
48                 DisplayName: m.DisplayName,
49                 InfoHash:    m.InfoHash,
50                 Webseeds:    m.Params["ws"],
51                 Sources:     append(m.Params["xs"], m.Params["as"]...),
52                 PeerAddrs:   m.Params["x.pe"], // BEP 9
53                 // TODO: What's the parameter for DHT nodes?
54         }
55         return
56 }
57
58 func TorrentSpecFromMetaInfoErr(mi *metainfo.MetaInfo) (*TorrentSpec, error) {
59         info, err := mi.UnmarshalInfo()
60         if err != nil {
61                 return nil, fmt.Errorf("unmarshalling info: %w", err)
62         }
63         return &TorrentSpec{
64                 Trackers:    mi.UpvertedAnnounceList(),
65                 InfoHash:    mi.HashInfoBytes(),
66                 InfoBytes:   mi.InfoBytes,
67                 DisplayName: info.Name,
68                 Webseeds:    mi.UrlList,
69                 DhtNodes: func() (ret []string) {
70                         ret = make([]string, 0, len(mi.Nodes))
71                         for _, node := range mi.Nodes {
72                                 ret = append(ret, string(node))
73                         }
74                         return
75                 }(),
76         }, nil
77 }
78
79 func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
80         ts, err := TorrentSpecFromMetaInfoErr(mi)
81         if err != nil {
82                 panic(err)
83         }
84         return ts
85 }