FedP2P/metainfo/metainfo.go

97 lines
2.7 KiB
Go
Raw Permalink Normal View History

package metainfo
import (
"io"
"net/url"
"os"
"time"
"github.com/anacrolix/torrent/bencode"
)
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"`
2021-11-12 07:45:47 +08:00
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
d := bencode.NewDecoder(r)
err := d.Decode(&mi)
if err != nil {
return nil, err
}
return &mi, nil
}
// Convenience function for loading a MetaInfo from a file.
func LoadFromFile(filename string) (*MetaInfo, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return Load(f)
}
func (mi MetaInfo) UnmarshalInfo() (info Info, err error) {
err = bencode.Unmarshal(mi.InfoBytes, &info)
return
}
func (mi MetaInfo) HashInfoBytes() (infoHash Hash) {
return HashBytes(mi.InfoBytes)
}
// Encode to bencoded form.
func (mi MetaInfo) Write(w io.Writer) error {
return bencode.NewEncoder(w).Encode(mi)
}
// 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()
mi.Encoding = "UTF-8"
}
2016-03-28 17:38:30 +08:00
// Creates a Magnet from a MetaInfo. Optional infohash and parsed info can be provided.
2022-06-14 16:23:00 +08:00
func (mi MetaInfo) Magnet(infoHash *Hash, info *Info) (m Magnet) {
2021-03-25 06:56:04 +08:00
m.Trackers = append(m.Trackers, mi.UpvertedAnnounceList().DistinctValues()...)
if info != nil {
2022-03-18 06:15:50 +08:00
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
2016-03-28 18:57:04 +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}}
}
return nil
}