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/utils/upload/generate.go
kilo 213780f0eb
Small fixes (#1715)
* Update main.css

* saving settings now clear old cookies of current domain

* Update main.js

* Update classic.css

* Update settings.go

* Update main.css

* Update commentlist.jet.html

* Update generate.go

* Update default_config.yml

* Update structs.go

* Update default_config.yml

* Update find.go

* Update find.go

* Update stats.go

* Update interaction.go
2017-11-08 09:32:06 +01:00

84 lignes
1,9 Kio
Go

package upload
import (
"strconv"
"errors"
"fmt"
"os"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/utils/log"
"github.com/anacrolix/dht"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/bencode"
)
var queue []string
var client *torrent.Client
func initClient() error {
clientConfig := torrent.Config{
DHTConfig: dht.ServerConfig{
StartingNodes: dht.GlobalBootstrapAddrs,
},
ListenAddr: ":" + strconv.Itoa(config.Get().Torrents.GenerationClientPort),
}
cl, err := torrent.NewClient(&clientConfig)
if err != nil {
log.Errorf("error creating client: %s", err)
return err
}
client = cl
return nil
}
// GenerateTorrent generates a torrent file in the specified directory in config.yml from a magnet URI
func GenerateTorrent(magnet string) error {
if client == nil {
err := initClient()
if err != nil {
return err
}
}
if magnet == "" || len(config.Get().Torrents.FileStorage) == 0 {
return errors.New("Magnet Empty or FileStorage not configured")
}
if len(queue) > 0 {
for _, m := range queue {
if m == magnet {
return errors.New("Magnet being generated already")
}
}
}
queue = append(queue, magnet)
t, err := client.AddMagnet(magnet)
if err != nil {
log.Errorf("error adding magnet to client: %s", err)
return err
}
go func() {
<-t.GotInfo()
mi := t.Metainfo()
t.Drop()
file := fmt.Sprintf("%s%c%s.torrent", config.Get().Torrents.FileStorage, os.PathSeparator, t.InfoHash().String())
f, err := os.Create(file)
if err != nil {
log.Errorf("error creating torrent metainfo file: %s", err)
return
}
defer f.Close()
err = bencode.NewEncoder(f).Encode(mi)
if err != nil {
log.Errorf("error writing torrent metainfo file: %s", err)
return
}
for k, m := range queue {
if m == magnet {
queue = append(queue[:k], queue[k+1:]...)
}
}
log.Infof("New torrent file generated in: %s", file)
}()
return nil
}