10 "github.com/anacrolix/torrent/bencode"
13 type MetaInfo struct {
14 InfoBytes bencode.Bytes `bencode:"info,omitempty"` // BEP 3
15 Announce string `bencode:"announce,omitempty"` // BEP 3
16 AnnounceList AnnounceList `bencode:"announce-list,omitempty"` // BEP 12
17 Nodes []Node `bencode:"nodes,omitempty,ignore_unmarshal_type_error"` // BEP 5
18 // Where's this specified? Mentioned at
19 // https://wiki.theory.org/index.php/BitTorrentSpecification: (optional) the creation time of
20 // the torrent, in standard UNIX epoch format (integer, seconds since 1-Jan-1970 00:00:00 UTC)
21 CreationDate int64 `bencode:"creation date,omitempty,ignore_unmarshal_type_error"`
22 Comment string `bencode:"comment,omitempty"`
23 CreatedBy string `bencode:"created by,omitempty"`
24 Encoding string `bencode:"encoding,omitempty"`
25 UrlList UrlList `bencode:"url-list,omitempty"` // BEP 19 WebSeeds
28 // Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
30 func Load(r io.Reader) (*MetaInfo, error) {
32 d := bencode.NewDecoder(r)
40 // Convenience function for loading a MetaInfo from a file.
41 func LoadFromFile(filename string) (*MetaInfo, error) {
42 f, err := os.Open(filename)
52 func (mi MetaInfo) UnmarshalInfo() (info Info, err error) {
53 err = bencode.Unmarshal(mi.InfoBytes, &info)
57 func (mi MetaInfo) HashInfoBytes() (infoHash Hash) {
58 return HashBytes(mi.InfoBytes)
61 // Encode to bencoded form.
62 func (mi MetaInfo) Write(w io.Writer) error {
63 return bencode.NewEncoder(w).Encode(mi)
66 // Set good default values in preparation for creating a new MetaInfo file.
67 func (mi *MetaInfo) SetDefaults() {
68 mi.CreatedBy = "github.com/anacrolix/torrent"
69 mi.CreationDate = time.Now().Unix()
72 // Creates a Magnet from a MetaInfo. Optional infohash and parsed info can be provided.
73 func (mi MetaInfo) Magnet(infoHash *Hash, info *Info) (m Magnet) {
74 m.Trackers = append(m.Trackers, mi.UpvertedAnnounceList().DistinctValues()...)
76 m.DisplayName = info.BestName()
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}}