8 "github.com/anacrolix/torrent/bencode"
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 []string `bencode:"url-list,omitempty"`
23 // Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
25 func Load(r io.Reader) (*MetaInfo, error) {
27 d := bencode.NewDecoder(r)
35 // Convenience function for loading a MetaInfo from a file.
36 func LoadFromFile(filename string) (*MetaInfo, error) {
37 f, err := os.Open(filename)
45 func (mi MetaInfo) UnmarshalInfo() (info Info, err error) {
46 err = bencode.Unmarshal(mi.InfoBytes, &info)
50 func (mi MetaInfo) HashInfoBytes() (infoHash Hash) {
51 return HashBytes(mi.InfoBytes)
54 // Encode to bencoded form.
55 func (mi MetaInfo) Write(w io.Writer) error {
56 return bencode.NewEncoder(w).Encode(mi)
59 // Set good default values in preparation for creating a new MetaInfo file.
60 func (mi *MetaInfo) SetDefaults() {
61 mi.Comment = "yoloham"
62 mi.CreatedBy = "github.com/anacrolix/torrent"
63 mi.CreationDate = time.Now().Unix()
64 // mi.Info.PieceLength = 256 * 1024
67 // Creates a Magnet from a MetaInfo.
68 func (mi *MetaInfo) Magnet(displayName string, infoHash Hash) (m Magnet) {
69 for _, tier := range mi.AnnounceList {
70 for _, tracker := range tier {
71 m.Trackers = append(m.Trackers, tracker)
74 if m.Trackers == nil && mi.Announce != "" {
75 m.Trackers = []string{mi.Announce}
77 m.DisplayName = displayName