Rework torrent adding
This commit is contained in:
parent
362dfecbf2
commit
e4f1f5d55e
51
client.go
51
client.go
|
@ -1524,29 +1524,19 @@ func (me Torrent) ReadAt(p []byte, off int64) (n int, err error) {
|
||||||
return me.cl.TorrentReadAt(me.InfoHash, off, p)
|
return me.cl.TorrentReadAt(me.InfoHash, off, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cl *Client) AddMagnet(uri string) (t Torrent, err error) {
|
func (cl *Client) AddMagnet(uri string) (T Torrent, err error) {
|
||||||
t.cl = cl
|
|
||||||
m, err := ParseMagnetURI(uri)
|
m, err := ParseMagnetURI(uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
cl.AddTorrentFromFile(cl.torrentFileCachePath(m.InfoHash))
|
||||||
cl.mu.Lock()
|
cl.mu.Lock()
|
||||||
defer cl.mu.Unlock()
|
defer cl.mu.Unlock()
|
||||||
t.torrent = cl.torrent(m.InfoHash)
|
T, err = cl.addOrMergeTorrent(m.InfoHash, [][]string{m.Trackers})
|
||||||
if t.torrent != nil {
|
|
||||||
t.addTrackers([][]string{m.Trackers})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
t.torrent, err = newTorrent(m.InfoHash, [][]string{m.Trackers}, cl.halfOpenLimit)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t.DisplayName = m.DisplayName
|
T.DisplayName = m.DisplayName
|
||||||
err = cl.addTorrent(t.torrent)
|
|
||||||
if err != nil {
|
|
||||||
t.Close()
|
|
||||||
}
|
|
||||||
go cl.connectionPruner(t.torrent)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1593,40 +1583,49 @@ func (me *Client) DropTorrent(infoHash InfoHash) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *Client) addTorrent(t *torrent) (err error) {
|
func (me *Client) addOrMergeTorrent(ih InfoHash, announceList [][]string) (T Torrent, err error) {
|
||||||
if _, ok := me.torrents[t.InfoHash]; ok {
|
if _, ok := me.bannedTorrents[ih]; ok {
|
||||||
err = fmt.Errorf("torrent infohash collision")
|
err = errors.New("banned torrent")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
me.torrents[t.InfoHash] = t
|
T.cl = me
|
||||||
|
var ok bool
|
||||||
|
T.torrent, ok = me.torrents[ih]
|
||||||
|
if ok {
|
||||||
|
T.torrent.addTrackers(announceList)
|
||||||
|
} else {
|
||||||
|
T.torrent, err = newTorrent(ih, announceList, me.halfOpenLimit)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
me.torrents[ih] = T.torrent
|
||||||
if !me.disableTrackers {
|
if !me.disableTrackers {
|
||||||
go me.announceTorrentTrackers(t)
|
go me.announceTorrentTrackers(T.torrent)
|
||||||
}
|
}
|
||||||
if me.dHT != nil {
|
if me.dHT != nil {
|
||||||
go me.announceTorrentDHT(t, true)
|
go me.announceTorrentDHT(T.torrent, true)
|
||||||
|
}
|
||||||
|
go me.connectionPruner(T.torrent)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds the torrent to the client.
|
// Adds the torrent to the client.
|
||||||
func (me *Client) AddTorrent(metaInfo *metainfo.MetaInfo) (t Torrent, err error) {
|
func (me *Client) AddTorrent(metaInfo *metainfo.MetaInfo) (t Torrent, err error) {
|
||||||
t.cl = me
|
|
||||||
var ih InfoHash
|
var ih InfoHash
|
||||||
CopyExact(&ih, metaInfo.Info.Hash)
|
CopyExact(&ih, metaInfo.Info.Hash)
|
||||||
t.torrent, err = newTorrent(ih, metaInfo.AnnounceList, me.halfOpenLimit)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
me.mu.Lock()
|
me.mu.Lock()
|
||||||
defer me.mu.Unlock()
|
defer me.mu.Unlock()
|
||||||
err = me.addTorrent(t.torrent)
|
t, err = me.addOrMergeTorrent(ih, metaInfo.AnnounceList)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if !t.torrent.haveInfo() {
|
||||||
err = me.setMetaData(t.torrent, metaInfo.Info.Info, metaInfo.Info.Bytes)
|
err = me.setMetaData(t.torrent, metaInfo.Info.Info, metaInfo.Info.Bytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue