8 "github.com/anacrolix/torrent/bencode"
11 type MetaInfo struct {
12 InfoBytes bencode.Bytes `bencode:"info,omitempty"` // BEP 3
13 Announce string `bencode:"announce,omitempty"` // BEP 3
14 AnnounceList AnnounceList `bencode:"announce-list,omitempty"` // BEP 12
15 Nodes []Node `bencode:"nodes,omitempty"` // BEP 5
16 // Where's this specified? Mentioned at
17 // https://wiki.theory.org/index.php/BitTorrentSpecification: (optional) the creation time of
18 // the torrent, in standard UNIX epoch format (integer, seconds since 1-Jan-1970 00:00:00 UTC)
19 CreationDate int64 `bencode:"creation date,omitempty,ignore_unmarshal_type_error"`
20 Comment string `bencode:"comment,omitempty"`
21 CreatedBy string `bencode:"created by,omitempty"`
22 Encoding string `bencode:"encoding,omitempty"`
23 UrlList UrlList `bencode:"url-list,omitempty"` // BEP 19
26 // Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
28 func Load(r io.Reader) (*MetaInfo, error) {
30 d := bencode.NewDecoder(r)
38 // Convenience function for loading a MetaInfo from a file.
39 func LoadFromFile(filename string) (*MetaInfo, error) {
40 f, err := os.Open(filename)
48 func (mi MetaInfo) UnmarshalInfo() (info Info, err error) {
49 err = bencode.Unmarshal(mi.InfoBytes, &info)
53 func (mi MetaInfo) HashInfoBytes() (infoHash Hash) {
54 return HashBytes(mi.InfoBytes)
57 // Encode to bencoded form.
58 func (mi MetaInfo) Write(w io.Writer) error {
59 return bencode.NewEncoder(w).Encode(mi)
62 // Set good default values in preparation for creating a new MetaInfo file.
63 func (mi *MetaInfo) SetDefaults() {
65 mi.CreatedBy = "github.com/anacrolix/torrent"
66 mi.CreationDate = time.Now().Unix()
67 // mi.Info.PieceLength = 256 * 1024
70 // Creates a Magnet from a MetaInfo. Optional infohash and parsed info can be provided.
71 func (mi *MetaInfo) Magnet(infoHash *Hash, info *Info) (m Magnet) {
72 for t := range mi.UpvertedAnnounceList().DistinctValues() {
73 m.Trackers = append(m.Trackers, t)
76 m.DisplayName = info.Name
79 m.InfoHash = *infoHash
81 m.InfoHash = mi.HashInfoBytes()
83 //m.Params = make(url.Values)
84 //m.Params["ws"] = mi.UrlList
88 // Returns the announce list converted from the old single announce field if
90 func (mi *MetaInfo) UpvertedAnnounceList() AnnounceList {
91 if mi.AnnounceList.OverridesAnnounce(mi.Announce) {
92 return mi.AnnounceList
94 if mi.Announce != "" {
95 return [][]string{{mi.Announce}}