6 "github.com/nsf/libtorgo/bencode"
12 // Information specific to a single file inside the MetaInfo structure..
13 type FileInfo struct {
14 Length int64 `bencode:"length"`
15 Path []string `bencode:"path"`
18 // MetaInfo is the type you should use when reading torrent files. See Load and
19 // LoadFromFile functions. All the fields are intended to be read-only. If
20 // 'len(Files) == 1', then the FileInfo.Path is nil in that entry.
21 type MetaInfo struct {
24 AnnounceList [][]string
25 CreationDate time.Time
33 // Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
35 func Load(r io.Reader) (*MetaInfo, error) {
38 d := bencode.NewDecoder(r)
39 err := d.Decode(&data)
44 mi.Info = data.Info.Info
45 mi.InfoBytes = data.Info.Bytes
46 mi.InfoHash = data.Info.Hash
47 if len(data.AnnounceList) > 0 {
48 mi.AnnounceList = data.AnnounceList
50 mi.AnnounceList = [][]string{[]string{data.Announce}}
52 mi.CreationDate = time.Unix(data.CreationDate, 0)
53 mi.Comment = data.Comment
54 mi.CreatedBy = data.CreatedBy
55 mi.Encoding = data.Encoding
56 if data.URLList != nil {
57 switch v := data.URLList.(type) {
59 mi.WebSeedURLs = []string{v}
62 ss := make([]string, len(v))
64 ss[i], ok = s.(string)
66 return nil, errors.New("bad url-list data type")
71 return nil, errors.New("bad url-list data type")
77 // Convenience function for loading a MetaInfo from a file.
78 func LoadFromFile(filename string) (*MetaInfo, error) {
79 f, err := os.Open(filename)
88 PieceLength int64 `bencode:"piece length"`
89 Pieces []byte `bencode:"pieces"`
90 Name string `bencode:"name"`
91 Length int64 `bencode:"length,omitempty"`
92 Private bool `bencode:"private,omitempty"`
93 Files []FileInfo `bencode:"files,omitempty"`
96 //----------------------------------------------------------------------------
97 // unmarshal structures
98 //----------------------------------------------------------------------------
100 type torrent_info_ex struct {
106 func (this *torrent_info_ex) UnmarshalBencode(data []byte) error {
107 this.Bytes = make([]byte, 0, len(data))
108 this.Bytes = append(this.Bytes, data...)
111 this.Hash = h.Sum(this.Hash)
112 return bencode.Unmarshal(data, &this.Info)
115 func (this *torrent_info_ex) MarshalBencode() ([]byte, error) {
116 return bencode.Marshal(&this.Info)
119 type torrent_data struct {
120 Info torrent_info_ex `bencode:"info"`
121 Announce string `bencode:"announce"`
122 AnnounceList [][]string `bencode:"announce-list,omitempty"`
123 CreationDate int64 `bencode:"creation date,omitempty"`
124 Comment string `bencode:"comment,omitempty"`
125 CreatedBy string `bencode:"created by,omitempty"`
126 Encoding string `bencode:"encoding,omitempty"`
127 URLList interface{} `bencode:"url-list,omitempty"`