本文整理汇总了Golang中github.com/anacrolix/torrent.Torrent.InfoHash方法的典型用法代码示例。如果您正苦于以下问题:Golang Torrent.InfoHash方法的具体用法?Golang Torrent.InfoHash怎么用?Golang Torrent.InfoHash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/anacrolix/torrent.Torrent
的用法示例。
在下文中一共展示了Torrent.InfoHash方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: upsertTorrent
func (e *Engine) upsertTorrent(tt torrent.Torrent) *Torrent {
ih := tt.InfoHash().HexString()
torrent, ok := e.ts[ih]
if !ok {
torrent = &Torrent{InfoHash: ih}
e.ts[ih] = torrent
}
//update torrent fields using underlying torrent
torrent.Update(tt)
return torrent
}
示例2: add
// Add Torrent/Magnet
func add(w http.ResponseWriter, r *http.Request) {
c := Add{}
if e := json.NewDecoder(r.Body).Decode(&c); e != nil {
httpd.Error(w, e, "Invalid input.")
return
}
if c.User == "" {
httpd.Error(w, nil, "No user.")
return
}
if c.Dir == "" {
httpd.Error(w, nil, "No dir.")
return
}
if strings.Contains(c.Dir, "..") {
httpd.Error(w, nil, "Dir hack.")
return
}
if _, ok := clients[c.User]; !ok {
dlDir := fmt.Sprintf(config.C.Basedir + c.Dir)
if _, e := os.Stat(config.C.Basedir); os.IsNotExist(e) {
if e := os.Mkdir(dlDir, os.ModeDir); e != nil {
config.L.Printf("CRIT: %s", e.Error())
httpd.Error(w, e, "Permission error.")
return
}
}
// https://github.com/anacrolix/torrent/blob/master/config.go#L9
cl, e := torrent.NewClient(&torrent.Config{
DataDir: config.C.Basedir + c.Dir,
// IPBlocklist => http://john.bitsurge.net/public/biglist.p2p.gz
})
if e != nil {
httpd.Error(w, e, "Client init failed")
return
}
clients[c.User] = cl
}
client := clients[c.User]
var (
t torrent.Torrent
e error
)
if len(c.Magnet) > 0 {
t, e = client.AddMagnet(c.Magnet)
if e != nil {
httpd.Error(w, e, "Magnet add failed")
return
}
} else if len(c.Torrent) > 0 {
// strip base64
b, e := base64.StdEncoding.DecodeString(c.Torrent)
if e != nil {
httpd.Error(w, e, "Failed base64 decode torrent input")
return
}
m, e := metainfo.Load(bytes.NewReader(b))
if e != nil {
httpd.Error(w, e, "Failed base64 decode torrent input")
return
}
t, e = client.AddTorrent(m)
if e != nil {
httpd.Error(w, e, "Failed adding torrent")
return
}
} else {
httpd.Error(w, nil, "No magnet nor torrent.")
return
}
// queue
go func() {
<-t.GotInfo()
t.DownloadAll()
}()
// (cl *Client) AddTorrentSpec(spec *TorrentSpec) (T Torrent, new bool, err error) {
// TorrentSpecFromMagnetURI(uri string) (spec *TorrentSpec, err error) {
// TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) (spec *TorrentSpec) {
// (me *Client) AddMagnet(uri string) (T Torrent, err error) {
// (me *Client) AddTorrent(mi *metainfo.MetaInfo) (T Torrent, err error) {
msg := fmt.Sprintf(`{"status": true, "hash": "%s", "text": "Queued."}`, t.InfoHash().HexString())
w.Header().Set("Content-Type", "application/json")
if _, e := w.Write([]byte(msg)); e != nil {
httpd.Error(w, e, "Flush failed")
return
}
}