]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/metainfo.go
Panic if NewHashFromHex gets a bad string
[btrtrc.git] / metainfo / metainfo.go
1 package metainfo
2
3 import (
4         "io"
5         "os"
6         "time"
7
8         "github.com/anacrolix/torrent/bencode"
9 )
10
11 type MetaInfo struct {
12         InfoBytes    bencode.Bytes `bencode:"info"`
13         Announce     string        `bencode:"announce,omitempty"`
14         AnnounceList [][]string    `bencode:"announce-list,omitempty"`
15         Nodes        []Node        `bencode:"nodes,omitempty"`
16         CreationDate int64         `bencode:"creation date,omitempty"`
17         Comment      string        `bencode:"comment,omitempty"`
18         CreatedBy    string        `bencode:"created by,omitempty"`
19         Encoding     string        `bencode:"encoding,omitempty"`
20         URLList      interface{}   `bencode:"url-list,omitempty"`
21 }
22
23 // Information specific to a single file inside the MetaInfo structure.
24 type FileInfo struct {
25         Length int64    `bencode:"length"`
26         Path   []string `bencode:"path"`
27 }
28
29 // Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
30 // failure.
31 func Load(r io.Reader) (*MetaInfo, error) {
32         var mi MetaInfo
33         d := bencode.NewDecoder(r)
34         err := d.Decode(&mi)
35         if err != nil {
36                 return nil, err
37         }
38         return &mi, nil
39 }
40
41 // Convenience function for loading a MetaInfo from a file.
42 func LoadFromFile(filename string) (*MetaInfo, error) {
43         f, err := os.Open(filename)
44         if err != nil {
45                 return nil, err
46         }
47         defer f.Close()
48         return Load(f)
49 }
50
51 func (mi MetaInfo) UnmarshalInfo() (info Info, err error) {
52         err = bencode.Unmarshal(mi.InfoBytes, &info)
53         return
54 }
55
56 func (mi MetaInfo) HashInfoBytes() (infoHash Hash) {
57         return HashBytes(mi.InfoBytes)
58 }
59
60 // Encode to bencoded form.
61 func (mi MetaInfo) Write(w io.Writer) error {
62         return bencode.NewEncoder(w).Encode(mi)
63 }
64
65 // Set good default values in preparation for creating a new MetaInfo file.
66 func (mi *MetaInfo) SetDefaults() {
67         mi.Comment = "yoloham"
68         mi.CreatedBy = "github.com/anacrolix/torrent"
69         mi.CreationDate = time.Now().Unix()
70         // mi.Info.PieceLength = 256 * 1024
71 }
72
73 // Creates a Magnet from a MetaInfo.
74 func (mi *MetaInfo) Magnet(displayName string, infoHash Hash) (m Magnet) {
75         for _, tier := range mi.AnnounceList {
76                 for _, tracker := range tier {
77                         m.Trackers = append(m.Trackers, tracker)
78                 }
79         }
80         if m.Trackers == nil && mi.Announce != "" {
81                 m.Trackers = []string{mi.Announce}
82         }
83         m.DisplayName = displayName
84         m.InfoHash = infoHash
85         return
86 }