]> Sergey Matveev's repositories - btrtrc.git/blobdiff - metainfo/metainfo.go
Buffer metainfo loads from files
[btrtrc.git] / metainfo / metainfo.go
index 68cbc75b1dae179b7dcebfc7d7b3606914d91ad1..93f9103b968b09f661cb3aa9a9a6618a8102cdb6 100644 (file)
@@ -1,76 +1,39 @@
 package metainfo
 
 import (
-       "crypto/sha1"
-       "errors"
-       "github.com/nsf/libtorgo/bencode"
+       "bufio"
        "io"
+       "net/url"
        "os"
        "time"
-)
 
-// Information specific to a single file inside the MetaInfo structure..
-type FileInfo struct {
-       Length int64    `bencode:"length"`
-       Path   []string `bencode:"path"`
-}
+       "github.com/anacrolix/torrent/bencode"
+)
 
-// MetaInfo is the type you should use when reading torrent files. See Load and
-// LoadFromFile functions. All the fields are intended to be read-only. If
-// 'len(Files) == 1', then the FileInfo.Path is nil in that entry.
 type MetaInfo struct {
-       Info
-       InfoHash     []byte
-       AnnounceList [][]string
-       CreationDate time.Time
-       Comment      string
-       CreatedBy    string
-       Encoding     string
-       WebSeedURLs  []string
-       InfoBytes    []byte
+       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,ignore_unmarshal_type_error"` // 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
 // failure.
 func Load(r io.Reader) (*MetaInfo, error) {
        var mi MetaInfo
-       var data torrent_data
        d := bencode.NewDecoder(r)
-       err := d.Decode(&data)
+       err := d.Decode(&mi)
        if err != nil {
                return nil, err
        }
-
-       mi.Info = data.Info.Info
-       mi.InfoBytes = data.Info.Bytes
-       mi.InfoHash = data.Info.Hash
-       if len(data.AnnounceList) > 0 {
-               mi.AnnounceList = data.AnnounceList
-       } else {
-               mi.AnnounceList = [][]string{[]string{data.Announce}}
-       }
-       mi.CreationDate = time.Unix(data.CreationDate, 0)
-       mi.Comment = data.Comment
-       mi.CreatedBy = data.CreatedBy
-       mi.Encoding = data.Encoding
-       if data.URLList != nil {
-               switch v := data.URLList.(type) {
-               case string:
-                       mi.WebSeedURLs = []string{v}
-               case []interface{}:
-                       var ok bool
-                       ss := make([]string, len(v))
-                       for i, s := range v {
-                               ss[i], ok = s.(string)
-                               if !ok {
-                                       return nil, errors.New("bad url-list data type")
-                               }
-                       }
-                       mi.WebSeedURLs = ss
-               default:
-                       return nil, errors.New("bad url-list data type")
-               }
-       }
        return &mi, nil
 }
 
@@ -81,48 +44,55 @@ func LoadFromFile(filename string) (*MetaInfo, error) {
                return nil, err
        }
        defer f.Close()
-       return Load(f)
+       var buf bufio.Reader
+       buf.Reset(f)
+       return Load(&buf)
 }
 
-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
 }
 
-//----------------------------------------------------------------------------
-// unmarshal structures
-//----------------------------------------------------------------------------
+func (mi MetaInfo) HashInfoBytes() (infoHash Hash) {
+       return HashBytes(mi.InfoBytes)
+}
 
-type torrent_info_ex struct {
-       Info
-       Hash  []byte
-       Bytes []byte
+// Encode to bencoded form.
+func (mi MetaInfo) Write(w io.Writer) error {
+       return bencode.NewEncoder(w).Encode(mi)
 }
 
-func (this *torrent_info_ex) UnmarshalBencode(data []byte) error {
-       this.Bytes = make([]byte, 0, len(data))
-       this.Bytes = append(this.Bytes, data...)
-       h := sha1.New()
-       h.Write(this.Bytes)
-       this.Hash = h.Sum(this.Hash)
-       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 *torrent_info_ex) MarshalBencode() ([]byte, error) {
-       return bencode.Marshal(&this.Info)
+// 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()
+       }
+       if infoHash != nil {
+               m.InfoHash = *infoHash
+       } else {
+               m.InfoHash = mi.HashInfoBytes()
+       }
+       m.Params = make(url.Values)
+       m.Params["ws"] = mi.UrlList
+       return
 }
 
-type torrent_data struct {
-       Info         torrent_info_ex `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
 }