Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/service/torrent/filesizeFetcher/operation.go

61 lignes
1,3 Kio
Go
Brut Vue normale Historique

2017-05-13 19:58:48 +02:00
package filesizeFetcher;
import (
"github.com/anacrolix/torrent/metainfo"
"github.com/ewhal/nyaa/config"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/util"
"errors"
"time"
"strings"
)
type FetchOperation struct {
fetcher *FilesizeFetcher
torrent model.Torrent
done chan int
}
type Result struct {
operation *FetchOperation
err error
info *metainfo.Info
}
func NewFetchOperation(fetcher *FilesizeFetcher, dbEntry model.Torrent) (op *FetchOperation) {
op = &FetchOperation{
fetcher: fetcher,
torrent: dbEntry,
done: make(chan int, 1),
2017-05-13 19:58:48 +02:00
}
return
}
// Should be started from a goroutine somewhere
func (op *FetchOperation) Start(out chan Result) {
2017-05-13 21:07:39 +02:00
defer op.fetcher.wg.Done()
2017-05-13 19:58:48 +02:00
magnet := util.InfoHashToMagnet(strings.TrimSpace(op.torrent.Hash), op.torrent.Name, config.Trackers...)
downloadingTorrent, err := op.fetcher.torrentClient.AddMagnet(magnet)
if err != nil {
out <- Result{op, err, nil}
return
}
timeoutTicker := time.NewTicker(time.Second * time.Duration(op.fetcher.timeout))
select {
case <-downloadingTorrent.GotInfo():
downloadingTorrent.Drop()
out <- Result{op, nil, downloadingTorrent.Info()}
2017-05-13 21:07:39 +02:00
break
2017-05-13 19:58:48 +02:00
case <-timeoutTicker.C:
downloadingTorrent.Drop()
out <- Result{op, errors.New("Timeout"), nil}
2017-05-13 21:07:39 +02:00
break
2017-05-13 19:58:48 +02:00
case <-op.done:
downloadingTorrent.Drop()
2017-05-13 21:07:39 +02:00
break
2017-05-13 19:58:48 +02:00
}
}