Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

get the proper magnet and infohash for upload

Cette révision appartient à :
Andrew Zhao 2017-05-06 17:48:24 -07:00
Parent 4340579bcd
révision eeab38b90d
2 fichiers modifiés avec 23 ajouts et 5 suppressions

Voir le fichier

@ -1,17 +1,22 @@
package router
import (
"encoding/hex"
"errors"
"fmt"
"github.com/ewhal/nyaa/util"
"github.com/ewhal/nyaa/util/metainfo"
"github.com/zeebo/bencode"
"net/http"
"net/url"
"strings"
)
// UploadForm serializing HTTP form for torrent upload
type UploadForm struct {
Name string
Magnet string
Infohash string
Category string
Description string
}
@ -85,8 +90,23 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error {
}
// generate magnet
f.Magnet = util.InfoHashToMagnet(torrent.Infohash(), f.Name)
binInfoHash := torrent.Infohash()
f.Infohash = hex.EncodeToString(binInfoHash[:])
f.Magnet = util.InfoHashToMagnet(f.Infohash, f.Name)
} else {
magnetUrl, parseErr := url.Parse(f.Magnet)
if parseErr != nil {
return metainfo.ErrInvalidTorrentFile
}
fmt.Println(magnetUrl)
exactTopic := magnetUrl.Query().Get("xt")
if !strings.HasPrefix(exactTopic, "urn:btih:") {
return metainfo.ErrInvalidTorrentFile
} else {
f.Infohash = strings.ToUpper(strings.TrimPrefix(exactTopic, "urn:btih:"))
}
}
return nil
}

Voir le fichier

@ -1,15 +1,13 @@
package util
import (
"encoding/hex"
"fmt"
"net/url"
)
// convert a binary infohash to a magnet uri given a display name and tracker urls
func InfoHashToMagnet(ih [20]byte, name string, trackers ...url.URL) (str string) {
str = hex.EncodeToString(ih[:])
str = fmt.Sprintf("magnet:?xt=urn:btih:%s", str)
func InfoHashToMagnet(ih string, name string, trackers ...url.URL) (str string) {
str = fmt.Sprintf("magnet:?xt=urn:btih:%s", ih)
if len(name) > 0 {
str += fmt.Sprintf("&dn=%s", name)
}