package metainfo
import (
- "crypto/sha1"
"io"
+ "net/url"
"os"
+ "time"
- "github.com/anacrolix/libtorgo/bencode"
+ "github.com/anacrolix/torrent/bencode"
)
-// Information specific to a single file inside the MetaInfo structure.
-type FileInfo struct {
- Length int64 `bencode:"length"`
- Path []string `bencode:"path"`
+type MetaInfo struct {
+ InfoBytes bencode.Bytes `bencode:"info,omitempty"` // BEP 3
+ Announce string `bencode:"announce,omitempty"` // BEP 3
+ AnnounceList AnnounceList `bencode:"announce-list,omitempty"` // BEP 12
+ Nodes []Node `bencode:"nodes,omitempty"` // BEP 5
+ // Where's this specified? Mentioned at
+ // https://wiki.theory.org/index.php/BitTorrentSpecification: (optional) the creation time of
+ // the torrent, in standard UNIX epoch format (integer, seconds since 1-Jan-1970 00:00:00 UTC)
+ CreationDate int64 `bencode:"creation date,omitempty,ignore_unmarshal_type_error"`
+ Comment string `bencode:"comment,omitempty"`
+ CreatedBy string `bencode:"created by,omitempty"`
+ Encoding string `bencode:"encoding,omitempty"`
+ UrlList UrlList `bencode:"url-list,omitempty"` // BEP 19 WebSeeds
}
// Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
return Load(f)
}
-// The info dictionary.
-type Info struct {
- PieceLength int64 `bencode:"piece length"`
- Pieces []byte `bencode:"pieces"`
- Name string `bencode:"name"`
- Length int64 `bencode:"length,omitempty"`
- Private bool `bencode:"private,omitempty"`
- Files []FileInfo `bencode:"files,omitempty"`
+func (mi MetaInfo) UnmarshalInfo() (info Info, err error) {
+ err = bencode.Unmarshal(mi.InfoBytes, &info)
+ return
}
-// The info dictionary with its hash and raw bytes exposed, as these are
-// important to Bittorrent.
-type InfoEx struct {
- Info
- Hash []byte
- Bytes []byte
+func (mi MetaInfo) HashInfoBytes() (infoHash Hash) {
+ return HashBytes(mi.InfoBytes)
}
-var (
- _ bencode.Marshaler = InfoEx{}
- _ bencode.Unmarshaler = &InfoEx{}
-)
+// Encode to bencoded form.
+func (mi MetaInfo) Write(w io.Writer) error {
+ return bencode.NewEncoder(w).Encode(mi)
+}
-func (this *InfoEx) UnmarshalBencode(data []byte) error {
- this.Bytes = make([]byte, 0, len(data))
- this.Bytes = append(this.Bytes, data...)
- h := sha1.New()
- _, err := h.Write(this.Bytes)
- if err != nil {
- panic(err)
- }
- this.Hash = h.Sum(nil)
- return bencode.Unmarshal(data, &this.Info)
+// Set good default values in preparation for creating a new MetaInfo file.
+func (mi *MetaInfo) SetDefaults() {
+ mi.CreatedBy = "github.com/anacrolix/torrent"
+ mi.CreationDate = time.Now().Unix()
}
-func (this InfoEx) MarshalBencode() ([]byte, error) {
- if this.Bytes != nil {
- return this.Bytes, nil
+// Creates a Magnet from a MetaInfo. Optional infohash and parsed info can be provided.
+func (mi MetaInfo) Magnet(infoHash *Hash, info *Info) (m Magnet) {
+ m.Trackers = append(m.Trackers, mi.UpvertedAnnounceList().DistinctValues()...)
+ if info != nil {
+ m.DisplayName = info.BestName()
}
- return bencode.Marshal(&this.Info)
+ if infoHash != nil {
+ m.InfoHash = *infoHash
+ } else {
+ m.InfoHash = mi.HashInfoBytes()
+ }
+ m.Params = make(url.Values)
+ m.Params["ws"] = mi.UrlList
+ return
}
-type MetaInfo struct {
- Info InfoEx `bencode:"info"`
- Announce string `bencode:"announce"`
- AnnounceList [][]string `bencode:"announce-list,omitempty"`
- CreationDate int64 `bencode:"creation date,omitempty"`
- Comment string `bencode:"comment,omitempty"`
- CreatedBy string `bencode:"created by,omitempty"`
- Encoding string `bencode:"encoding,omitempty"`
- URLList interface{} `bencode:"url-list,omitempty"`
+// Returns the announce list converted from the old single announce field if
+// necessary.
+func (mi *MetaInfo) UpvertedAnnounceList() AnnounceList {
+ if mi.AnnounceList.OverridesAnnounce(mi.Announce) {
+ return mi.AnnounceList
+ }
+ if mi.Announce != "" {
+ return [][]string{{mi.Announce}}
+ }
+ return nil
}