2014-06-28 17:40:39 +08:00
|
|
|
package metainfo
|
2012-06-28 04:26:56 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
2015-10-29 22:21:09 +08:00
|
|
|
"time"
|
2014-12-02 13:28:22 +08:00
|
|
|
|
2015-04-27 12:55:01 +08:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2012-06-28 04:26:56 +08:00
|
|
|
)
|
|
|
|
|
2016-08-26 18:29:05 +08:00
|
|
|
type MetaInfo struct {
|
2020-05-01 03:23:20 +08:00
|
|
|
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
|
2020-05-13 12:33:19 +08:00
|
|
|
// 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
|
2016-08-26 18:29:05 +08:00
|
|
|
}
|
|
|
|
|
2012-07-02 03:05:10 +08:00
|
|
|
// 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
|
2012-06-28 04:26:56 +08:00
|
|
|
d := bencode.NewDecoder(r)
|
2014-07-13 15:36:06 +08:00
|
|
|
err := d.Decode(&mi)
|
2012-06-28 04:26:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-07-02 03:05:10 +08:00
|
|
|
return &mi, nil
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
|
|
|
|
2012-07-02 03:05:10 +08:00
|
|
|
// Convenience function for loading a MetaInfo from a file.
|
|
|
|
func LoadFromFile(filename string) (*MetaInfo, error) {
|
2012-06-28 04:26:56 +08:00
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2012-06-30 06:23:02 +08:00
|
|
|
return Load(f)
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
|
|
|
|
2016-09-20 10:25:40 +08:00
|
|
|
func (mi MetaInfo) UnmarshalInfo() (info Info, err error) {
|
|
|
|
err = bencode.Unmarshal(mi.InfoBytes, &info)
|
2015-10-29 22:21:09 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-26 18:29:05 +08:00
|
|
|
func (mi MetaInfo) HashInfoBytes() (infoHash Hash) {
|
|
|
|
return HashBytes(mi.InfoBytes)
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
2015-10-29 22:21:09 +08:00
|
|
|
|
|
|
|
// Encode to bencoded form.
|
2016-08-26 18:29:05 +08:00
|
|
|
func (mi MetaInfo) Write(w io.Writer) error {
|
2015-10-29 22:21:09 +08:00
|
|
|
return bencode.NewEncoder(w).Encode(mi)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set good default values in preparation for creating a new MetaInfo file.
|
|
|
|
func (mi *MetaInfo) SetDefaults() {
|
2020-12-09 13:27:50 +08:00
|
|
|
mi.Comment = ""
|
2015-10-29 22:21:09 +08:00
|
|
|
mi.CreatedBy = "github.com/anacrolix/torrent"
|
|
|
|
mi.CreationDate = time.Now().Unix()
|
2016-08-26 18:29:05 +08:00
|
|
|
// mi.Info.PieceLength = 256 * 1024
|
2015-10-29 22:21:09 +08:00
|
|
|
}
|
2016-03-28 17:38:30 +08:00
|
|
|
|
2016-05-05 20:40:38 +08:00
|
|
|
// Creates a Magnet from a MetaInfo.
|
2016-08-26 18:29:05 +08:00
|
|
|
func (mi *MetaInfo) Magnet(displayName string, infoHash Hash) (m Magnet) {
|
2017-02-15 17:05:28 +08:00
|
|
|
for t := range mi.UpvertedAnnounceList().DistinctValues() {
|
|
|
|
m.Trackers = append(m.Trackers, t)
|
2016-07-22 15:16:11 +08:00
|
|
|
}
|
2016-08-26 18:29:05 +08:00
|
|
|
m.DisplayName = displayName
|
|
|
|
m.InfoHash = infoHash
|
2016-04-04 11:48:39 +08:00
|
|
|
return
|
2016-03-28 18:57:04 +08:00
|
|
|
}
|
2017-02-15 17:05:28 +08:00
|
|
|
|
|
|
|
// 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 != "" {
|
2019-08-19 10:15:54 +08:00
|
|
|
return [][]string{{mi.Announce}}
|
2017-02-15 17:05:28 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|