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 interface{} `bencode:"url-list,omitempty"`
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"`
29 // Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
31 func Load(r io.Reader) (*MetaInfo, error) {
33 d := bencode.NewDecoder(r)
41 // Convenience function for loading a MetaInfo from a file.
42 func LoadFromFile(filename string) (*MetaInfo, error) {
43 f, err := os.Open(filename)
51 func (mi MetaInfo) UnmarshalInfo() (info Info, err error) {
52 err = bencode.Unmarshal(mi.InfoBytes, &info)
56 func (mi MetaInfo) HashInfoBytes() (infoHash Hash) {
57 return HashBytes(mi.InfoBytes)
60 // Encode to bencoded form.
61 func (mi MetaInfo) Write(w io.Writer) error {
62 return bencode.NewEncoder(w).Encode(mi)
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
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)
80 if m.Trackers == nil && mi.Announce != "" {
81 m.Trackers = []string{mi.Announce}
83 m.DisplayName = displayName